我正在尝试使用 Zuul 和 Consul 创建一个 Spring 云微服务应用程序。
我的项目中有 2 个组件:
使用 Zuul 的 api-gateway 微服务
Hello world 微服务(一个简单的 hello world Rest Webservice)
这是API网关的代码:
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.M3</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring.cloud.consul.version>1.0.0.BUILD-SNAPSHOT</spring.cloud.consul.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<!-- Setup Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<!-- Spring Cloud starter -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>${spring.cloud.consul.version}</version>
</dependency>
</dependencies>
应用程序.yml
zuul:
routes:
hello1:
path: /hello1/**
serviceId: microservice-example
logging:
level:
org.springframework: INFO
com.netflix: DEBUG
引导程序.yml
spring:
application:
name: edge-server
cloud:
consul:
config:
enabled: true
host: localhost
port: 8500
这是hello微服务的代码:
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
@RestController
public class Application {
@RequestMapping(value="/hello1",method = RequestMethod.GET)
public String hello() {
System.out.print("hello1");
return "Hello1";
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
bootstrap.yml:spring:应用程序:名称:微服务示例配置文件:活动:本机
cloud:
consul:
config:
enabled: true
host: localhost
port: 8500
但是,当我测试我的服务时,浏览器会显示以下消息:
这是边缘服务器应用程序的堆栈跟踪:
2015-11-25 16:49:31.780 DEBUG 9876 --- [nio-8080-exec-7] c.n.zuul.http.HttpServletRequestWrapper : Path = null
2015-11-25 16:49:31.781 DEBUG 9876 --- [nio-8080-exec-7] c.n.zuul.http.HttpServletRequestWrapper : Transfer-Encoding = null
2015-11-25 16:49:31.781 DEBUG 9876 --- [nio-8080-exec-7] c.n.zuul.http.HttpServletRequestWrapper : Content-Encoding = null
2015-11-25 16:49:31.781 DEBUG 9876 --- [nio-8080-exec-7] c.n.zuul.http.HttpServletRequestWrapper : Content-Length header = -1
2015-11-25 16:49:31.782 DEBUG 9876 --- [nio-8080-exec-7] c.n.loadbalancer.ZoneAwareLoadBalancer : Zone aware logic disabled or there is only one zone
2015-11-25 16:49:31.782 DEBUG 9876 --- [nio-8080-exec-7] c.n.loadbalancer.LoadBalancerContext : microservice-example using LB returned Server: FR09248851D:1234 for request
2015-11-25 16:49:31.783 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.niws.client.http.RestClient : RestClient sending new Request(GET: ) http://FR09248851D:1234
2015-11-25 16:49:31.783 DEBUG 9876 --- [nio-8080-exec-7] c.n.http4.MonitoredConnectionManager : Get connection: {}->http://FR09248851D:1234, timeout = 2000
2015-11-25 16:49:31.783 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NamedConnectionPool : [{}->http://FR09248851D:1234] total kept alive: 1, total issued: 0, total allocated: 1 out of 200
2015-11-25 16:49:31.783 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NamedConnectionPool : Getting free connection [{}->http://FR09248851D:1234][null]
2015-11-25 16:49:31.783 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NFHttpClient : Stale connection check
2015-11-25 16:49:31.784 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NFHttpClient : Attempt 1 to execute request
2015-11-25 16:49:31.791 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NFHttpClient : Connection can be kept alive indefinitely
2015-11-25 16:49:31.796 DEBUG 9876 --- [nio-8080-exec-7] c.n.http4.MonitoredConnectionManager : Released connection is reusable.
2015-11-25 16:49:31.796 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NamedConnectionPool : Releasing connection [{}->http://FR09248851D:1234][null]
2015-11-25 16:49:31.796 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NamedConnectionPool : Pooling connection [{}->http://FR09248851D:1234][null]; keep alive indefinitely
2015-11-25 16:49:31.796 DEBUG 9876 --- [nio-8080-exec-7] com.netflix.http4.NamedConnectionPool : Notifying no-one, there are no waiting threads
2015-11-25 16:49:35.721 DEBUG 9876 --- [service-example] c.netflix.loadbalancer.BaseLoadBalancer : LoadBalancer: PingTask executing [1] servers configured
2015-11-25 16:49:35.885 DEBUG 9876 --- [clean up thread] com.netflix.http4.ConnectionPoolCleaner : Connection pool clean up started for client microservice-example
2015-11-25 16:49:35.885 DEBUG 9876 --- [clean up thread] c.n.http4.MonitoredConnectionManager : Closing expired connections