傳統(tǒng)路由配置
所謂的傳統(tǒng)路由配置方式就是在不依賴與服務(wù)發(fā)現(xiàn)機(jī)制的情況下,通過在配置文件中具體制定每個路由表達(dá)式實(shí)例的映射關(guān)系來實(shí)現(xiàn)api網(wǎng)關(guān)對外部請求的路由。
沒有Eureka等服務(wù)治理框架的幫助,我們需要根據(jù)服務(wù)實(shí)例的數(shù)量采用不同方式的配置來實(shí)現(xiàn)路由規(guī)則:
單實(shí)例配置:
通過zuul.routes.<route>.path
與zuul.routes.<route>.url
參數(shù)對的方式進(jìn)行配置,比如:
zuul.routes.user-service.path=/user-service/**
zuul.routes.user-service.url=http://localhost:8080/
如果一個請求http://localhost:6069/user-service/hello
就被請求轉(zhuǎn)發(fā)到http://localhost:8080/hello
地址。
demo
啟動一個簡單的web服務(wù),不注冊eureka,訪問路徑
http://localhost:8080/user/index
啟動zuul服務(wù)
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
routes:
user-service:
path: /user-service/**
url: http://localhost:8080/
代理之后可以訪問:
http://localhost:6069/user-service/user/index
多實(shí)例配置:
通過zuul.routes.<route>.path
與zuul.routes.<route>.serviceId
參數(shù)的方式進(jìn)行配置,比如:
zuul.routes.user-service.path=/user-service/**
zuul.routes.user-service.serviceId=user-service
ribbon.eureka.enabled=false
user-service.ribbon.listOfServers=http://localhost:8080/,http://localhost:8081/
當(dāng)請求為求http://localhost:6069/user-service/
就被轉(zhuǎn)發(fā)到http://localhost:8080/,http://localhost:8081/
兩個實(shí)例地址上了。serviceId
用戶手工命名的服務(wù)名稱,配合ribbon.listOfServers
參數(shù)實(shí)現(xiàn)服務(wù)與實(shí)例的維護(hù)。由于存在多個實(shí)例,api網(wǎng)關(guān)在進(jìn)行路由轉(zhuǎn)發(fā)需要實(shí)現(xiàn)負(fù)載均衡策略,這里還需要spring cloud ribbon配合。由于spring cloud zuul中自帶了ribbon的依賴。
不論是單實(shí)例還是多實(shí)例的配置方式,我們都需要為每一對映射關(guān)系指定一個名稱,也就是上面配置的<route>
,每一個<route>
對應(yīng)了一條路由規(guī)則。每條路由規(guī)則都需要指定path屬性來定義一個用來匹配客戶端請求的路徑表達(dá)式,并通過url
或serviceId
屬性置頂請求表達(dá)式映射具體實(shí)例地址或服務(wù)名。
demo
啟動user服務(wù)(不注冊到eureka),使用8080和8081接口
http://localhost:8080/user/index
http://localhost:8081/user/index
啟動zuul,zuul項(xiàng)目的配置如下:
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
routes:
users:
path: /user-service/**
serviceId: user-service
ribbon:
eureka:
enabled: false
user-service:
ribbon:
listOfServers: http://localhost:8080/,http://localhost:8081/
訪問:
http://localhost:6069/user-service/user/index
通過日志看出的確是負(fù)載均衡了。
注意:
These simple url-routes don’t get executed as a HystrixCommand
nor can you loadbalance multiple URLs with Ribbon. To achieve this, specify a service-route and configure a Ribbon client for the serviceId (this currently requires disabling Eureka support in Ribbon: see above for more information), e.g.
這些使用簡單的url-routes
不能夠使用HystrixCommand
并且不能使用Ribbon對多URL實(shí)現(xiàn)路由。如果想去使用這一點(diǎn),特定的指定服務(wù)理并且配置ribbon去負(fù)載均衡:可以查看above for more information 。
參考資料
官網(wǎng)Embedded Zuul Reverse Proxy
服務(wù)路由配置
spring cloud zuul
通過與spring cloud eureka
的整合,實(shí)現(xiàn)了對服務(wù)實(shí)例的自動化維護(hù),所以使用服務(wù)路由配置的時候,不需要向傳統(tǒng)路由配置方式那樣為serviceId指定具體服務(wù)實(shí)例地址,只需要通過zuul.routes.<route>.path
與zuul.routes.<route>.serviceId
參數(shù)對的方式進(jìn)行配置即可。
zuul.routes.user-service.path=/users/**
zuul.routes.user-service.serviceId=user-service
除了path和serviceId鍵值對的配置方式之外,還有一種簡單的配置:zuul.routes.<serviceId>=<path>
,其中<serviceId>
用來指定路由的具體服務(wù)名,<path>
用來配置匹配的請求表達(dá)式,
zuul.routes.user-service=/users/**
zuul
巧妙的整合了Eureka
來實(shí)現(xiàn)面向服務(wù)的路由。實(shí)際上,我們可以直接將api網(wǎng)關(guān)也看作Eureka
服務(wù)治理下的一個普通的微服務(wù)應(yīng)用。它除了會將自己注冊到Eureka
服務(wù)注冊中心上之外,也會從注冊中心獲取所有服務(wù)以及他們的實(shí)例清單。在Eureka
的幫助下,api網(wǎng)關(guān)服務(wù)本身就已經(jīng)維護(hù)了系統(tǒng)中所有serviceId與實(shí)例地址的映射關(guān)系。當(dāng)有外部請求到達(dá)api網(wǎng)關(guān)的時候,根據(jù)請求的url路徑找到最佳匹配的path,api網(wǎng)關(guān)就可以知道要將請求路由到哪個具體的serviceId
上去。由于api網(wǎng)關(guān)中已經(jīng)知道serviceId
對應(yīng)服務(wù)實(shí)例的地址清單,那么只需要通過ribbon
的負(fù)載均衡策略,直接在這些清單中選擇一個具體的實(shí)例進(jìn)行轉(zhuǎn)發(fā)就能完成路由工作了。
服務(wù)路由的默認(rèn)規(guī)則
雖然通過Eureka
與zuul
的整合已經(jīng)為我們省去了維護(hù)服務(wù)實(shí)例清單的大量配置工作,剩下來只需要再維護(hù)請求路徑的匹配表達(dá)式與服務(wù)名映射關(guān)系即可。
但是實(shí)際的運(yùn)用過程中發(fā)現(xiàn),大部分的路由規(guī)則機(jī)會都會采用服務(wù)名作為外部請求的前綴,比如下面的列子,其中path路徑的前綴使用了user-service
,而對應(yīng)的服務(wù)名也是user-service
。
zuul.routes.user-service.path=/user-service/**
zuul.routes.user-service.serviceId=user-service
其實(shí)zuul已經(jīng)自動的幫我們實(shí)現(xiàn)以服務(wù)名作為前綴的映射,我們不需要去配置它。
但是,有一些服務(wù)我們不需要對外開發(fā)也被外部訪問到了。這個時候我們可以使用zuul.ignore-services
參數(shù)來設(shè)置一個服務(wù)名匹配表達(dá)式來定義不自動創(chuàng)建路由的規(guī)則。zuul
在自動創(chuàng)建服務(wù)路由的時候會根據(jù)該表達(dá)式來進(jìn)行判斷,如果服務(wù)名匹配表達(dá)式,那么zuul
將跳過該服務(wù),不為其創(chuàng)建路由規(guī)則。比如,設(shè)置為zuul.ignored-services=*
的時候,zuul
將對所有的服務(wù)都不自動創(chuàng)建路由規(guī)則。在這種情況下,我們就要在配置文件中為需要路由的服務(wù)添加路由規(guī)則(可以使用path
與serviceId
組合的配置方式,也可以使用更簡潔的zuul.routes.<serviceId>=<path>
配置方式),只有在配置文件中出現(xiàn)的映射規(guī)則會被創(chuàng)建路由,而從Eureka
中獲取的其他服務(wù),zuul
將不會為他們創(chuàng)建路由規(guī)則。
之前的博客講過,可以參考下面的博客:
zuul學(xué)習(xí)一:spring cloud zuul的快速入門
官網(wǎng)Embedded Zuul Reverse Proxy
自定義路由映射關(guān)系
我們在構(gòu)建微服服務(wù)系統(tǒng)的進(jìn)行業(yè)務(wù)邏輯開發(fā)的時候,為了兼容外部不同版本的客戶端程序(盡量不強(qiáng)迫用戶升級客戶端),一般都會采用開閉原則來進(jìn)行設(shè)計(jì)與開發(fā)。這使得系統(tǒng)在迭代過程中,有時候需要我們?yōu)橐唤M互相配合的微服務(wù)定義一個版本標(biāo)記來方便管理它們的版本關(guān)系,根據(jù)這個標(biāo)記我們可以很容易的知道這些服務(wù)需要一起啟動并配合使用。比如:userservice-v1
,userservice-v2
,orderservice-v1
,orderservice-v2
等等。默認(rèn)情況下,zuul自動為服務(wù)創(chuàng)建的路由表達(dá)式會采用服務(wù)名作為前綴,比如針對上面的userservice-v1
和userservice-v2
,它會產(chǎn)生/userservice-v1
和/userservice-v2
兩個路徑表達(dá)式來映射,這樣生成出來的表示式規(guī)則單一,不利于管理。通常的做法就是為這些不同的版本的微服務(wù)應(yīng)用生成以版本號作為路由前綴定義規(guī)則的路由規(guī)則,比如/v1/userservice/
。這時候,通過這樣具有版本號前綴的url路徑,我們就可以很同意的通過路徑表達(dá)式來歸類和管理這些具有版本信息的微服務(wù)了。
我們可以使用zuul中自定義服務(wù)與路由映射關(guān)系的功能,創(chuàng)建類似于/v1/userserivce/**
的路由匹配原則。
@Bean
public PatternServiceRouteMapper serviceRouteMapper(){
return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)","${version}/${name}");
}
PatternServiceRouteMapper
對象可以讓開發(fā)者通過正則表達(dá)式來自定義服務(wù)與路由映射的生成關(guān)系。構(gòu)造函數(shù)第一個參數(shù)是用來匹配服務(wù)名稱是否符合該自定義規(guī)則的正則表達(dá)式,第二個參數(shù)是定義根據(jù)服務(wù)名中定義的內(nèi)容轉(zhuǎn)換出的路徑表達(dá)式規(guī)則。當(dāng)開發(fā)者在api網(wǎng)關(guān)中定義了PatternServiceRouteMapper
實(shí)現(xiàn)之后,只需符合第一個參數(shù)定義規(guī)則的服務(wù)名,都會優(yōu)先使用該實(shí)現(xiàn)構(gòu)建出的表達(dá)式,如果沒有匹配上的服務(wù)規(guī)則則還是會使用默認(rèn)的路由映射規(guī)則,記采用完整服務(wù)名作為前綴的路徑表達(dá)式。
demo
定義users服務(wù),注冊到eureka上為users-v1,符合上述的服務(wù)名稱將版本號,啟動eureka和users服務(wù)
訪問user服務(wù)
http://192.168.1.57:8080/user/index
啟動zuul:
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class);
}
@Bean
public PatternServiceRouteMapper serviceRouteMapper(){
return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)","${version}/${name}");
}
}
配置文件:
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
訪問:
http://192.168.1.57:6069/v1/users/user/index
訪問的方式符合我們說的版本號/服務(wù)名/
參考資料
官網(wǎng)Embedded Zuul Reverse Proxy
路徑匹配
不論是使用傳統(tǒng)配置方式還是服務(wù)路由的配置方式,我們都需要為每個路由定義匹配表達(dá)式,也就是上面的oath參數(shù),在zuul中,路由匹配的路徑表達(dá)式采用ant風(fēng)格定義。
通配符 | 說明 |
---|---|
? | 匹配任意單個字符 |
* | 匹配任意數(shù)量的字符 |
** | 匹配任意數(shù)量的自負(fù),支持多級目錄 |
ant風(fēng)格的路徑表達(dá)式使用起來非常簡單,
通配符 | 說明 |
---|---|
? | 匹配任意單個字符 |
* | 匹配任意數(shù)量的字符 |
** | 匹配任意數(shù)量的自負(fù),支持多級目錄 |
url路徑 | 說明 |
---|---|
/user-service/? | 可以匹配/user-service/之后的一個人和字符的路徑,比如/user-service/a,/user-service/b,/user-service/c |
/user-service/* | 可以匹配/user-service/之后拼接的任意字符的路徑,比如說/user-service/a,/user-service/aaa,無法匹配/user-service/a/b |
/user-service/** | 可以匹配/user-service/*包含的內(nèi)容之外,還可以匹配/user-service/a/b的多級目錄 |
但是隨著版本的迭代,對user-service
服務(wù)做了一些功能拆分,將原本屬于user-service
服務(wù)的某些功能拆分到user-service-ext
中去,而這些拆分的外部調(diào)用url路徑希望能夠復(fù)合/user-service/ext/**
。這個時候
zuul.routes.user-service.path=/user-service/**
zuul.routes.user-service.serviceId=user-service
zuul.routes.user-service-ext.path=/user-service/ext/**
zuul.routes.user-service-ext.serviceId=user-service-ext
此時,調(diào)用user-service-ext
服務(wù)的url路徑實(shí)際上會同時被/user-service/**
和/user-service/ext/**
兩個表示式所匹配。在邏輯上,api網(wǎng)關(guān)優(yōu)先選擇/user-service/ext/**
路由,然后再去匹配/user-service/**
路由才能實(shí)現(xiàn)上述需求,但是如果使用上面的配置方式,實(shí)際上是無法保證這樣的路由優(yōu)先順序的。
從下面的路由匹配算法中,我們可以看到它在使用路由規(guī)則匹配的請求路徑的時候是通過線性便利的方法,在請求路徑獲取到第一個匹配的路由規(guī)則之后就返回并結(jié)束匹配過程。所以當(dāng)存在多個匹配的路由規(guī)則時,匹配結(jié)果完全取決于路由規(guī)則的保存順序。
由于properties的配置內(nèi)容無法保證有序,所以當(dāng)出現(xiàn)這樣的情況的時候,為了保證路由的優(yōu)先順序,我們需要使用yml文件來配置,以實(shí)現(xiàn)有序的路由規(guī)則
zuul:
routes:
user-service-ext:
path: /user-service/ext/**
serviceId: user-service-ext
user-service:
path: /user-service/**
serviceId: user-service
關(guān)于這邊的說法,官網(wǎng)給的介紹
If you need your routes to have their order preserved you need to use a YAML file as the ordering will be lost using a properties file.
忽略表達(dá)式
通過path參數(shù)定義的ant表達(dá)式已經(jīng)能夠完成api網(wǎng)關(guān)上的路由規(guī)則配置功能,但是為了更細(xì)粒度和更為靈活地配置理由規(guī)則,zuul還提供了一個忽略表達(dá)式參數(shù)zuul.ignored-patterns
。該參數(shù)可以用來設(shè)置不希望被api網(wǎng)關(guān)進(jìn)行路由的url表達(dá)式。
比如我們啟動user-service服務(wù),訪問
http://192.168.1.57:6069/user/home
可以使用api網(wǎng)關(guān)路由
http://192.168.1.57:6069/user-service/user/home
在zuul-service中配置
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
ignoredPatterns: /**/home/**
routes:
user-service:
path: /user-service/**
serviceId: user-service
order-service:
path: /pay-service/**
serviceId: pay-service
logging:
level:
com.netflix: debug
發(fā)現(xiàn)url中包括home的已經(jīng)不能被正確路由了。zuul.ignoredPatterns=//home/ 的使用方法。
控制臺上輸出:
另外,該參數(shù)在使用時還需要注意它的范圍并不是針對某個路由,而是對所有路由。所以在設(shè)置的時候需要全面考慮url規(guī)則,防止忽略了不該被忽略的url路徑。
參考資料
官網(wǎng)Embedded Zuul Reverse Proxy
路由前綴
為了方便地為路由規(guī)則增加前綴信息,zuul提供了zuul.prefix
參數(shù)來進(jìn)行設(shè)置。比如,希望為網(wǎng)關(guān)上的路由規(guī)則增加/api前綴,那么我們可以在配置文件中增加配置:zuul.prefix=/api
。另外,對于代理前綴會默認(rèn)從路徑中移除,我們可以通過設(shè)置zuul.strip-prefix=false
(默認(rèn)為true,默認(rèn)為true時前綴生效,比如http://192.168.5.3:6069/zhihao/users/user/index
)來關(guān)閉該移除代理前綴的動作。
demo
啟動user和order服務(wù)
http://192.168.1.57:8080/user/index
http://192.168.1.57:9090/order/index
啟動zuul服務(wù),zuul中配置了前綴
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
routes:
user-service:
path: /user-service/**
serviceId: user-service
order-service:
path: /pay-service/**
serviceId: pay-service
prefix: /zhihao
http://192.168.1.57:6069/zhihao/order-service/order/index
http://192.168.1.57:6069/zhihao/user-service/user/index
stripPrefix的使用
修改zuul的配置:
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
routes:
user-service:
path: /user-service/**
serviceId: user-service
order-service:
path: /pay-service/**
serviceId: pay-service
prefix: /zhihao
strip-prefix: false
logging:
level:
com.netflix: debug
增加了zuul.strip-prefix: false
和配置了zuul的日志級別。
再去訪問之前的代理:
http://192.168.1.57:6069/zhihao/user-service/user/index
發(fā)現(xiàn)訪問不了,控制臺上打印出日志,訪問到user服務(wù)的/zhihao/user/index了
2017-08-12 16:50:11.656 DEBUG 1407 --- [nio-6069-exec-8] c.n.loadbalancer.LoadBalancerContext
: user-service using LB returned Server: 192.168.1.57:8080 for request /zhihao/user/index
此時正確的姿勢是在user服務(wù)中增加server.context-path=/zhihao,再去訪問就正確了:
http://192.168.1.57:6069/zhihao/user-service/user/index
同時order服務(wù)也訪問不了,也要在order服務(wù)加server.context-path=/zhihao
,其實(shí)zuul.prefix=/zhihao
和zuul.strip-prefix=false
表示所有的服務(wù)都要跳過服務(wù)配置在真實(shí)請求求加上/zhihao。
也有針對當(dāng)個服務(wù)的
zuul服務(wù)的配置文件:
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
routes:
user-service:
path: /user-service/**
stripPrefix: false
serviceId: user-service
order-service:
path: /pay-service/**
serviceId: pay-service
prefix: /zhihao
logging:
level:
com.netflix: debug
http://192.168.1.57:6069/zhihao/order-service/order/index
http://192.168.1.57:6069/zhihao/user-service/user/index
前者后者都沒有配置context-path
,前者能夠訪問,后者不能訪問,發(fā)現(xiàn)因?yàn)楹笳吲渲昧?code>zuul.routes.user-service.stripPrefix=false,發(fā)現(xiàn)后者真正訪問到的的服務(wù)是/user-service/user/index
,此時要修改user-service增加server.context-path=/user-service
才能正確地訪問到服務(wù)。
坑:
user-service的配置改成下面這樣
spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
prefer-ip-address: true
server:
port: 6069
zuul:
routes:
user-service:
path: /zhihao-userservice/**
serviceId: user-service
order-service:
path: /pay-service/**
serviceId: pay-service
prefix: /zhihao
logging:
level:
com.netflix: debug
http://192.168.1.57:6069/zhihao/order-service/order/index
還是正確路由,
http://192.168.1.57:6069/zhihao/zhihao-userservice/user/index
不能正確路由。
這時zuul的一個bug,當(dāng)路由表達(dá)式前綴是以zhihao開頭,與路由表達(dá)式一樣是/zhihao開頭的話就會產(chǎn)生錯誤的映射關(guān)系。
我使用的是Camden.SR7版本也存在這樣的問題。
參考資料
官網(wǎng)Embedded Zuul Reverse Proxy
本地跳轉(zhuǎn)
在zuul實(shí)現(xiàn)的api網(wǎng)關(guān)路由功能中,還支持forward形式的服務(wù)端跳轉(zhuǎn)配置。實(shí)現(xiàn)方式非常簡單,只需要通過使用path與url的配置方式就能完成,通過url中使用forward來指定需要跳轉(zhuǎn)的服務(wù)器資源路徑。
在zuul-service服務(wù)中定義一個controller,
@RestController
public class HelloController {
@RequestMapping("/local/hello")
public String hello(){
return "hello world local";
}
}
配置文件配置:
zuul:
routes:
user-service:
path: /zhihao-userservice/users/**
serviceId: user-service
pay-service:
path: /pays/**
serviceId: pay-service
zuul-service:
path: /api-b/**
serviceId: forward:/local
訪問http://192.168.5.3:6069/api-b/hello
就跳轉(zhuǎn)到了網(wǎng)關(guān)的/local/hello上了。
cookie與頭信息
默認(rèn)情況下,spring cloud zuul
在請求路由時,會過濾掉http請求頭信息中一些敏感信息,防止它們被傳遞到下游的外部服務(wù)器。默認(rèn)的敏感頭信息通過zuul.sensitiveHeaders
參數(shù)定義,默認(rèn)包括cookie,set-Cookie,authorization
三個屬性。所以,我們在開發(fā)web項(xiàng)目時常用的cookie在spring cloud zuul
網(wǎng)關(guān)中默認(rèn)時不傳遞的,這就會引發(fā)一個常見的問題,如果我們要將使用了spring security
,shiro
等安全框架構(gòu)建的web應(yīng)用通過spring cloud zuul
構(gòu)建的網(wǎng)關(guān)來進(jìn)行路由時,由于cookie信息無法傳遞,我們的web應(yīng)用將無法實(shí)現(xiàn)登錄和鑒權(quán)。為了解決這個問題,配置的方法有很多。
- 通過設(shè)置全局參數(shù)為空來覆蓋默認(rèn)值,具體如下:
zuul.sensitiveHeaders=
這種方法不推薦,雖然可以實(shí)現(xiàn)cookie的傳遞,但是破壞了默認(rèn)設(shè)置的用意。在微服務(wù)架構(gòu)的api網(wǎng)關(guān)之內(nèi),對于無狀態(tài)的restful api請求肯定時要遠(yuǎn)多于這些web類應(yīng)用請求的,甚至還有一些架構(gòu)設(shè)計(jì)會將web類應(yīng)用和app客戶端一樣歸為api網(wǎng)關(guān)之外的客戶端應(yīng)用。
- 通過指定路由的參數(shù)來設(shè)置,方法有下面二種。
方法一:對指定路由開啟自定義敏感頭。
方法二:將指定路由的敏感頭設(shè)置為空。
zuul:
routes:
users:
path: /myusers/**
sensitiveHeaders:
url: https://downstream
將具體的服務(wù)的sensitiveHeaders(頭信息設(shè)置為空)
比較推薦使用這二種方法,僅對指定的web應(yīng)用開啟對敏感信息的傳遞,影響范圍小,不至于引起其他服務(wù)的信息泄露問題。
參考資料
Zuul Http Client
Cookies and Sensitive Headers
The Routes Endpoint
Spring Cloud實(shí)戰(zhàn)小貼士:Zuul處理Cookie和重定向
本博客代碼
代碼地址