SpringCloud Zuul Eureka
用maven的弊端就是各个依赖包版本要求比较高,有一个依赖包的版本不兼容就会导致各种启动加载的问题。
Demo中Zuul,Eureka都是被Spring封过的,有利于初学者快速搭建环境。
对于Spring高版本目前尝试的结果是会到导致 spring-cloud-starter-eureka 有兼容问题。
1.3.5版本是测试过。可以进行serviceId转发功能。如果Zuul只是做为URL转发,可以不用导入Eureka的依赖包。
下面是maven项目使用的pom.xml
aspectjweaver 1.8.6 版本是为了解决1.3.5版本启动Zuul时JoinPoint 找不到的问题。
NoClassDefFoundError: org/aspectj/lang/JoinPoint
Zuul pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zuul-demo</groupId>
<artifactId>zuul-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Eureka client pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springcloud-demo</groupId>
<artifactId>springcloud-demo</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>demo.EurekaSampleServer</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Eureka server pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springcloud-demo</groupId>
<artifactId>springcloud-demo</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>demo.EurekaSampleController</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在图中可以看出,Zuul也需要被当成一个service注册到Eureka中,否则会出现Forwarding Error错误,因为在Zuul的配置中,无法找到serviceId对应的URL信息。

当Zuul启动好之后,可以看到如下的log:
2017-11-10 15:57:13.560 INFO 4952 --- [nio-1111-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 52 ms
2017-11-10 15:57:45.773 INFO 4952 --- [nio-1111-exec-6] c.n.e.registry.AbstractInstanceRegistry : Registered instance COMPUTE-SERVICE/HOSTNAME:compute-service:2222 with status UP (replication=false)
2017-11-10 15:57:46.554 INFO 4952 --- [nio-1111-exec-7] c.n.e.registry.AbstractInstanceRegistry : Registered instance COMPUTE-SERVICE/HOSTNAME:compute-service:2222 with status UP (replication=true)
2017-11-10 15:59:00.547 INFO 4952 --- [nio-1111-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance API-GATEWAY/HOSTNAME:api-gateway:8000 with status UP (replication=false)
2017-11-10 15:59:01.065 INFO 4952 --- [nio-1111-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance API-GATEWAY/HOSTNAME:api-gateway:8000 with status UP (replication=true)
说明当前的Zuul已经被注册到Eureka中,所以在Zuul的配置中可以通过serviceId找到对应在Eureka中的service,从而转发到微服务中。
Zuul在Spring中的配置信息,application.properties:
zuul.routes.compute-service.path=/compute-service/**
zuul.routes.compute-service.serviceId=compute-service
#zuul.routes.compute-service.url=http://192.168.10.100:2222
spring.application.name=api-gateway
server.port=8000
#eureka.client.serviceUrl.defaultZone=${EUREKA_URL:http://user:password@localhost:5000}/eureka/
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
Eureka client在Spring中的配置信息,application.properties:
spring.application.name=compute-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
Eureka server在Spring中的配置信息,application.properties:
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
该配置中disable了Eureka把自身作为client注册到Eureka server中。
最后,先启动Eureka server, 启动client 最后Zuul。当都启动完成时,在Eureka server的画面可以看到注册的service信息http://localhost:1111/
访问Zuul的URL,http://localhost:8000/compute-service/ 内部转发到compute-service上。
Eureka Client代码:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Configuration
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaSampleController {
@RequestMapping("/")
public String home() {
return "Hello World";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(EurekaSampleController.class, args);
}
}
Eureka Server:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaSampleServer {
public static void main(String[] args) throws Exception {
SpringApplication.run(EurekaSampleServer.class, args);
}
}
Zuul:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
@EnableZuulProxy
//@SpringCloudApplication
@SpringBootApplication
public class ZuulDemoApplication {
@Bean
public SimpleFilter simpleFilter() {
return new SimpleFilter();
}
public static void main(String[] args) {
SpringApplication.run(ZuulDemoApplication.class, args);
}
}