Spring Cloud中怎么實現(xiàn)zuul網(wǎng)關(guān)服務(wù),針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶提供優(yōu)質(zhì)的聯(lián)通服務(wù)器托管服務(wù)
工程創(chuàng)建 cloud-gateway-zuul。還是基于之前的工程 pom文件導(dǎo)入
spring-cloud-alibaba-basis com.xian.cloud 1.0-SNAPSHOT 4.0.0 cloud-gateway-zuul 網(wǎng)關(guān)服務(wù)zuul com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-alibaba-nacos-config org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-netflix-zuul
創(chuàng)建GatewayZuulApplication啟動類
package com.xian.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.openfeign.EnableFeignClients; /** ** * @author xianliru@100tal.com * @version 1.0 * @createDate 2019/10/29 10:52 */ @EnableZuulProxy @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class GatewayZuulApplication { public static void main(String[] args) { SpringApplication.run(GatewayZuulApplication.class,args); } }
創(chuàng)建 bootstrap.yml
spring: profiles: active: dev application: name: gateway-zuul-server cloud: nacos: config: server-addr: 47.99.209.72:8848 file-extension: yaml zuul: host: # 目標(biāo)主機(jī)的最大連接數(shù),默認(rèn)值為200 max-total-connections: 1000 # 每個主機(jī)的初始連接數(shù),默認(rèn)值為20 max-per-route-connections: 200 routes: discovery-server: path: /server/** serviceId: cloud-discovery-server client-common: path: /client/** serviceId: cloud-discovery-client sensitiveHeaders: X-ABC,Authorization # 所有路由的默認(rèn)Hystrix隔離模式(ExecutionIsolationStrategy)為SEMAPHORE。如果此隔離模式是首選,則zuul.ribbonIsolationStrategy可以更改為THREAD ribbon-isolation-strategy: thread # 這個屬性意思,指定忽略的服務(wù)列表 * 代表忽略所有服務(wù) ignored-services: '*' # 字段比較敏感,不希望傳遞給下游微服務(wù)。 設(shè)置空沒有要忽略的敏感字段。全部傳給下游服務(wù) sensitive-headers: X-ABC ribbon: eager-load: # 強制加載,不設(shè)置會進(jìn)行懶加載。spring 第一次請求會非常慢 enabled: true ``` #### 參數(shù) - zuul.host.max-total-connections 目標(biāo)主機(jī)的最大連接數(shù)。 - zuul.host.max-per-route-connections 每個主機(jī)的初始連接數(shù)。 這個倆個參數(shù)是zuul的優(yōu)化后的屬性值,如果想有適合的配置,還需要根據(jù)業(yè)務(wù)情況而定 因為我們有倆個業(yè)務(wù)服務(wù) 一個服務(wù)提供者 一個是服務(wù)消費者我們配置倆個服務(wù)的分別路由 discovery-server、client-common - path 是請求路徑匹配規(guī)則 - serviceId 是我們服務(wù)的spring.application.name 對應(yīng)的值。 - sensitiveHeaders 字段比較敏感,不希望傳遞給下游微服務(wù)。 設(shè)置空沒有要忽略的敏感字段。全部傳給下游服務(wù)這個字段可以是全局設(shè)置也可以是單個服務(wù)配置。 - ribbon-isolation-strategy 所有路由的默認(rèn)Hystrix隔離模式(ExecutionIsolationStrategy)為SEMAPHORE。如果此隔離模式是首選,則zuul.ribbonIsolationStrategy可以更改為THREAD - ignored-services 忽略所有微服務(wù),只路由指定的微服務(wù)。 - ribbon.eager-load.enabled true 強制加載 false 默認(rèn)懶加載 true日志打印效果 false 將不打印這段日志 ``` 2019-10-29 23:47:11.377 INFO 61396 --- [ main] c.netflix.loadbalancer.BaseLoadBalancer : Client: cloud-discovery-server instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=cloud-discovery-server,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null 2019-10-29 23:47:11.382 INFO 61396 --- [ main] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater 2019-10-29 23:47:11.450 INFO 61396 --- [ main] c.netflix.config.ChainedDynamicProperty : Flipping property: cloud-discovery-server.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 2019-10-29 23:47:11.452 INFO 61396 --- [ main] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client cloud-discovery-server initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=cloud-discovery-server,current list of Servers=[192.168.3.6:9012],Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;] },Server stats: [[Server:192.168.3.6:9012; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0] ]}ServerList:com.alibaba.cloud.nacos.ribbon.NacosServerList@33e4b9c4 2019-10-29 23:47:11.576 INFO 61396 --- [ main] c.netflix.config.ChainedDynamicProperty : Flipping property: cloud-discovery-client.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 2019-10-29 23:47:11.577 INFO 61396 --- [ main] c.netflix.loadbalancer.BaseLoadBalancer : Client: cloud-discovery-client instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=cloud-discovery-client,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null 2019-10-29 23:47:11.578 INFO 61396 --- [ main] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater 2019-10-29 23:47:11.639 INFO 61396 --- [ main] c.netflix.config.ChainedDynamicProperty : Flipping property: cloud-discovery-client.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 2019-10-29 23:47:11.640 INFO 61396 --- [ main] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client cloud-discovery-client initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=cloud-discovery-client,current list of Servers=[192.168.3.6:9011],Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;] },Server stats: [[Server:192.168.3.6:9011; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0] ]}ServerList:com.alibaba.cloud.nacos.ribbon.NacosServerList@256589a1 ``` 將三個服務(wù)全部啟動。服務(wù)提供者和服務(wù)消費者還有zuul 服務(wù) 在控制臺 輸入命令 curl http://localhost:9083/client/client/test ![file](/upload/otherpic50/785552.png) 我們看到打印效果,請求通過網(wǎng)關(guān)服務(wù)成功轉(zhuǎn)發(fā)到了我們的下游服務(wù)上。并返回 - ribbon-isolation-strategy - ignored-services - sensitiveHeaders
關(guān)于Spring Cloud中怎么實現(xiàn)zuul網(wǎng)關(guān)服務(wù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。