上篇文章實作了網關最基本的路由使用,現在了解下網關路由的相關配置。
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向下層服務傳遞