一 代码位置
二 测试
1 启动Eurek
2 启动client-a服务
3 启动client-b服务
4 修改启动配置,启动zuul-server
spring:
profiles:
active: example1
测试下面路径
zuul:
routes:
client-a: /client/**
结果输出: client-a-2
它是一个从/client/**到client-a服务的一个映射规则
5 修改启动配置,启动zuul-server
spring:
profiles:
active: example2
测试下面配置
zuul:
routes:
client-a:
结果输出: client-a-3
它是一个从/client-a/**到client-a服务的一个映射规则
6 修改启动配置,启动zuul-server
spring:
profiles:
active: example4
测试下面配置
zuul:
routes:
client-b:
path: /client/**
serviceId: client-b
client-a:
path: /client/**
serviceId: client-a
结果输出: client-a-9
如果同一个映射对应多个服务,按照加载顺序,最后加载的映射规则会把之前的映射规则覆盖掉。
7 修改启动配置,启动
启动zuul-server
spring:
profiles:
active: example5
测试下面配置
########################## 敏感头设置 ##########################
zuul:
prefix: /pre
routes:
client-a:
path: /client/**
# sensitiveHeaders: Cookie,Set-Cookie,Authorization
sensitiveHeaders: Set-Cookie,Authorization
serviceId: client-a
# stripPrefix: false
测试结果为:client-a-12
8 修改启动配置,启动 zuul-server
spring:
profiles:
active: example7
测试下面配置
########################## 使用url替代serviceId路由 ##########################
zuul:
routes:
client-a:
path: /client/**
url: http://localhost:7070 #client-a的地址
输出结果为: client-a-12
9 修改启动配置,启动 zuul-server
spring:
profiles:
active: example8
测试下面配置
########################## 脱离eureka让zuul结合ribbon实现路由负载均衡 ##########################
zuul:
routes:
ribbon-route:
path: /ribbon/**
serviceId: ribbon-route
ribbon:
eureka:
enabled: false #禁止Ribbon使用Eureka
ribbon-route:
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Ribbon LB Strategy
listOfServers: localhost:7070,localhost:7071 #client services for Ribbon LB
结果分别为:
client-b-12
client-b-12
client-a-12
client-b-12
client-a-12
client-b-12
client-a-12
结果随机输出
10 修改启动配置,启动
zuul-server
spring:
profiles:
active: example9
测试下面配置
########################## foward跳转本地url ##########################
zuul:
routes:
client-a:
path: /client/**
url: forward:/client
结果输出: 本地跳转:7