上篇文章实现了网关最基本的路由使用,现在了解下网关路由的相关配置。
1、指定服务名称(正常使用这种)
zuul:
routes:
test1: /testOne/**
请求路径的改变:
原始:http://localhost:8501/test1/eureka-clinet1/ribbonTest
现在:http://localhost:8501/testOne/eureka-clinet1/ribbonTest
此处指定服务名test1的服务可以用testOne来作为替换。
2、自定义命名配置---ribbon、hystrix失效(不建议使用)
zuul:
routes:
#customer-test1:自定义名称
customer-test1:
path: /zuul-customer-test1/**
#PS:指定固定的服务接口,url无法实现ribbon和hystrix
url: http://localhost:8072/
请求:http://localhost:8501/zuul-customer-test1/eureka-clinet1/ribbonTest
url中只能配置一个请求地址
3、自定义命名---恢复ribbon+hystrix(放弃eureka,独立使用ribbon)
zuul:
routes:
zuul-customer-test1:
path: /zuul-customer-test1/**
#自定义一个服务id
service-id: no-eureka-test1
#配置这个服务id对应的一组服务信息(就是关闭eureka,手动使用ribbon选择自己配置的服务列表)
no-eureka-test1:
ribbon:
#就是test1的两个服务实例
listOfServers: localhost:8071,localhost:8072
#关闭ribbon中的eureka
ribbon:
eureka:
enabled: false
PS:此处的service-id对应的是自定义的id,不是某个服务的serviceId
4、自定义命名配置指定service-id
zuul:
routes:
#customer-test1:自定义信息
customer-test1:
path: /zuul-customer-test1/**
#eureka中的ApplicationName
service-id: test1
5、忽略服务名称
zuul:
#此时不能通过test1来实现请求
ignored-services:
- test1
routes:
test1: /test-one/**
正常情况我们修改配置后也可按照最基本的方式使用serviceId进行请求,配置了忽略后就无法使用serviceId请求了,只能通过配置的对应路径请求:
原始:http://localhost:8501/test1/eureka-clinet1/ribbonTest 无法请求
配置:http://localhost:8501/test-one/eureka-clinet1/ribbonTest 可以请求
6、正则匹配的全部禁止
zuul:
#此时不能通过test1来实现请求
ignored-services:
- test1
#配置后服务名中出现test(大写可以),ONE就无法请求成功了
ignored-patterns:
- /*test*/**
- /*-ONE/**
routes:
test1: /TEST/**
7、设置前缀配置(正常的网关API多会带有前缀来作出相应标记,如:v1==version 1)
zuul:
#此时不能通过test1来实现请求
ignored-services:
- test1
#配置后服务名中出现test就无法请求成功了(大写可以)
ignored-patterns:
- /*test*/**
- /*-ONE/**
routes:
test1: /TEST/**
prefix: /v1
#真正请求的时候去掉前缀;设置false则会携带前缀一起请求:会请求不到服务
strip-prefix: true
8、过滤指定字段
zuul:
routes:
test1: /TEST/**
#请求头中携带的参数信息可在网关服务获取,具体的业务服务无法获取
sensitive-headers:
- token
请求头
网关代码
控制台
PS:所有参数在网关服务正常获取
业务模块
控制台
PS:此时token信息获取不到,因为在网关中设置了禁止token向下层服务传递