eureka-server-standalone
: 提供注册中心的服务zuul-gateway
:提供zuul网关功能,注册到eureka
服务中心zuul-consumer
:提供接口调用,具有hystrix
熔断功能,和ribbon
负载均衡功能,注册到eureka
服务中心zuul-gateway
项目ZuulGatewayApplication.java
中@EnableZuulProxy
@SpringCloudApplication
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}
加入
@EnableZuulProxy
表示此项目启动zuul代理功能
加入@SpringCloudApplication
表示此项目启动断路由,eureka
注册功能
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-zuulartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
引入
zuul
和eureka-client
依赖,其中zuul
包含了hystrix
,actuator
,ribbon
等功能,见下图
application.yml
中spring:
application:
name: Zuul-Gateway
server:
port: 9001
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: '*'
management.endpoints.web.exposure.include
:打开actuator
的监控端口,,默认只打开了health
,info
的监控监控点*
代表全部打开,此举是用于查看/routes
接口,返回此zuul
代理了多少服务,以及路由规则;如下:
启动验证
1.依次启动eureka-server-standalone
端口为8760, zuul-gateway
端口为9001
2.zuu
项目有对外暴露的actuator
端点
/routes
:获取zuul项目的代理服务列表
/routes/details
:zuul项目的代理服务详情
3.访问http://localhost:9001/actuator/routes
,可见如下,只代理了自己的服务
{ "/zuul-gateway/**": "zuul-gateway"}
4.也可以查看详情localhost:9001/actuator/routes/details
{
"/zuul-gateway/**": {
"id": "zuul-gateway",
"fullPath": "/zuul-gateway/**",
"location": "zuul-gateway",
"path": "/**",
"prefix": "/zuul-gateway",
"retryable": false,
"customSensitiveHeaders": false,
"prefixStripped": true
}
}
zuul-consumer
项目提供接口调用,注册到eureka
服务中心,具有hystrix
熔断功能,和ribbon
负载均衡功能
在启动类中ZuulConsumerApplication.java
中
@SpringCloudApplication
@RestController
public class ZuulConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulConsumerApplication.class, args);
}
@Qualifier("eurekaRegistration")
@Autowired
private Registration registration;
@GetMapping("getInstanceServiceIdAndPort")
public String getInstanceServiceIdAndPort(){
String serviceId = registration.getServiceId();
int port = registration.getPort();
return serviceId+":"+port;
}
@GetMapping("getInstanceServiceIdAndPortWithThrowError")
@HystrixCommand(fallbackMethod = "rollBack")
public String getInstanceServiceIdAndPortWithThrowError(){
int port = registration.getPort();
throw new RuntimeException("go away!!"+"and port is : "+port);
}
public String rollBack(Throwable e){
return "throw error is : "+e.getMessage();
}
}
实现
getInstanceServiceIdAndPort
:用于提供获取本机实例和端口的接口服务
实现getInstanceServiceIdAndPortWithThrowError
:模拟熔断回退
注入Registration
:获取本地的一些信息
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
application.yml
中spring:
application:
name: Zuul-Consumer
server:
port: 9003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: '*'
management.endpoints.web.exposure.include
:打开actuator
的监控端口,默认只打开了health
,info
的监控监控点*
代表全部打开
依次启动eureka-server-standalone
端口为8760, zuul-gateway
端口为9001, zuul-consumer
启动2个实例端口分别为9002,9003
访问http://localhost:9001/actuator/routes
,可见如下,代理了自己的服务zuul-gateway
的同时也代理了 zuul-consumer
{
"/zuul-gateway/**": "zuul-gateway",
"/zuul-consumer/**": "zuul-consumer"
}
也可以查看详情localhost:9001/actuator/routes/details
{
"/zuul-gateway/**": {
"id": "zuul-gateway",
"fullPath": "/zuul-gateway/**",
"location": "zuul-gateway",
"path": "/**",
"prefix": "/zuul-gateway",
"retryable": false,
"customSensitiveHeaders": false,
"prefixStripped": true
},
"/zuul-consumer/**": {
"id": "zuul-consumer",
"fullPath": "/zuul-consumer/**",
"location": "zuul-consumer",
"path": "/**",
"prefix": "/zuul-consumer",
"retryable": false,
"customSensitiveHeaders": false,
"prefixStripped": true
}
}
验证代理的ribbon
负载均衡,和hystrix
熔断保护功能
1.默认情况下zuul
有自己的路由规则
- 会将
/zuul-consumer/**
转发到zuul-consumer
服务的接口上,通过调用/routes
也可以清晰的看到
2.访问代理接口http://localhost:9001/zuul-consumer/getInstanceServiceIdAndPort
返回端口轮询切换,代表ribbon负载均衡正常工作
- Zuul-Consumer:9002
- Zuul-Consumer:9003
3.访问代理接口http://localhost:9001/zuul-consumer/getInstanceServiceIdAndPortWithThrowError
,返回降级信息代表hystrix
降级正常运行
throw error is : go away!!and port is : 9002
path
和url
地址路由到具体服务zuul-gateway
的配置文件application.yml
中,添加zuul:
routes:
user-defined-zuul-a:
path: /zuul-api-a/**
url: http://localhost:9002
user-defined-zuul-b:
path: /zuul-api-b/**
url: http://localhost:9003
user-defined-zuul-a
和user-defined-zuul-b
是自定义key,/zuul-api-a/**
的请求会被路由到http://localhost:9002
服务/routes/details
可见代理详情{
"/zuul-api/**": {
"id": "zuul",
"fullPath": "/zuul-api/**",
"location": "http://localhost:9002",
"path": "/**",
"prefix": "/zuul-api",
"retryable": false,
"customSensitiveHeaders": false,
"prefixStripped": true
},
"/zuul-gateway/**": {
"id": "zuul-gateway",
"fullPath": "/zuul-gateway/**",
"location": "zuul-gateway",
"path": "/**",
"prefix": "/zuul-gateway",
"retryable": false,
"customSensitiveHeaders": false,
"prefixStripped": true
},
"/zuul-consumer/**": {
"id": "zuul-consumer",
"fullPath": "/zuul-consumer/**",
"location": "zuul-consumer",
"path": "/**",
"prefix": "/zuul-consumer",
"retryable": false,
"customSensitiveHeaders": false,
"prefixStripped": true
}
}
http://localhost:9001/zuul-api-b/getInstanceServiceIdAndPort
,返回代理信息
Zuul-Consumer:9003
直接使用path
和serviceId
服务路由到具体服务,具有ribbon负载均衡和hystrix熔断功能
在 zuul-gateway
的配置文件application.yml
中,添加
zuul:
routes:
user-defined-zuul-c:
path: /zuul-api-c/**
serviceId : Zuul-Consumer
user-defined-zuul-c
是自定义key,/zuul-api-c/**
的请求会被路由到服务名称是Zuul-Consumer
的微服务- 访问
/routes
可见代理
{
"/zuul-api-c/**": "Zuul-Consumer",
"/zuul-gateway/**": "zuul-gateway",
"/zuul-consumer/**": "zuul-consumer"
}
上述配置可简写为
zuul:
routes:
Zuul-Consumer: /zuul-api-c/**
- 直接使用
servcieId:url
的形式
"/zuul-gateway/**": "zuul-gateway",
"/zuul-consumer/**": "zuul-consumer"
- 当我们访问zuul网关的
zuul-gateway/hello
就会路由到具体的微服务/hello
接口上
Zuul-Gateway
网关,ZuulConsumer
接口服务,EurekaServerStandalone
注册中心,访问zuul的/actuator/routes
端点接口,得到如下代理路由:"/zuul-gateway/**": "zuul-gateway",
"/zuul-consumer/**": "zuul-consumer"
zuul-gateway
的配置文件application.yml
中,添加忽略配置,忽略zuul-gateway
微服务zuul:
ignoredServices: 'zuul-gateway'
/actuator/routes
,可见 zuul-gateway
不再被代理{
"/zuul-consumer/**": "zuul-consumer"
}
- 设置为
zuul.ignored-services=*
的时候将关闭所有默认路由配置规则
zuul:
routes:
Zuul-Consumer: /zuul-api-c/**
Zuul-Consumer2: /zuul-api-c/a/**
/zuul-api-c/a/xx
,不会转发到Zuul-Consumer2
服务上,会在Zuul-Consumer
查找相关接口,若没有,返回404
错误码zuul.prefix
属性给全体路由添加前缀zuul:
prefix: /api
访问actuator/routes
可见所有代理都添加了前缀{
"/api/zuul-consumer2/**": "zuul-consumer2",
"/api/zuul-gateway/**": "zuul-gateway",
"/api/zuul-consumer/**": "zuul-consumer"
}
zuul
不仅可以将请求转发到其他服务身上,也可以将请求转发到自己本身
在zuul配置文件中,添加如下配置
zuul:
routes:
api:
path: /api-d/**
url: forward:/
url: forward:/
:代表跳转到本zuul服务的/
路径下所有接口
在Zuul-Gateway添加接口服务
@EnableZuulProxy
@SpringCloudApplication
@RestController
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
@Autowired
private Registration registration;
@GetMapping("hello")
public String getInstanceServiceIdAndPort(){
String serviceId = registration.getServiceId();
int port = registration.getPort();
return serviceId+":"+port;
}
}
验证,访问http://localhost:9001/api-d/hello
返回
Zuul-Gateway:9001
在使用Zuul网关的时候你可能会遇到Cookie丢失的情况,这是因为默认情况下Zuul会过滤掉HTTP请求头中的一些敏感信息,不会向后传播,默认值为:
private Set<String> sensitiveHeaders = new LinkedHashSet(Arrays.asList("Cookie", "Set-Cookie", "Authorization"));
这些敏感信息通过下面的配置设定,设置全局
zuul:
sensitive-headers: Cookie,Set-Cookie,Authorization
设置某一个代理服务
zuul:
routes:
users:
path: /myusers/**
sensitiveHeaders: Cookie,Set-Cookie,Authorization
url: https://downstream
关闭头部过滤,sensitiveHeaders
设置为空就可以了
zuul:
routes:
users:
path: /myusers/**
sensitiveHeaders:
url: https://downstream
重定向host处理设置问题,使用如下配置解决
zuul:
add-host-header: true
上一篇:Spring Cloud Hystrix聚集监控信息,Turbine详解(Finchley版本)