上一篇進行Netflix Zuul 1.0 與 gateway的對比。今天來介紹一下 zuul的搭建及應用spring
工程建立 cloud-gateway-zuul。仍是基於以前的工程
pom文件導入bootstrap
<parent> <artifactId>spring-cloud-alibaba-basis</artifactId> <groupId>com.xian.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-gateway-zuul</artifactId> <name>網關服務zuul</name> <dependencies> <!-- 註冊中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 配置中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-config</artifactId> </dependency> <!-- fengin 支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- zuul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies>
建立GatewayZuulApplication啓動類segmentfault
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; /** * <Description> * * @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); } }
建立 bootstrap.ymlapp
spring: profiles: active: dev application: name: gateway-zuul-server cloud: nacos: config: server-addr: 47.99.209.72:8848 file-extension: yaml zuul: host: # 目標主機的最大鏈接數,默認值爲200 max-total-connections: 1000 # 每一個主機的初始鏈接數,默認值爲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 # 全部路由的默認Hystrix隔離模式(ExecutionIsolationStrategy)爲SEMAPHORE。若是此隔離模式是首選,則zuul.ribbonIsolationStrategy能夠更改成THREAD ribbon-isolation-strategy: thread # 這個屬性意思,指定忽略的服務列表 * 表明忽略全部服務 ignored-services: '*' # 字段比較敏感,不但願傳遞給下游微服務。 設置空沒有要忽略的敏感字段。所有傳給下游服務 sensitive-headers: X-ABC ribbon: eager-load: # 強制加載,不設置會進行懶加載。spring 第一次請求會很是慢 enabled: true
這個倆個參數是zuul的優化後的屬性值,若是想有適合的配置,還須要根據業務狀況而定curl
由於咱們有倆個業務服務 一個服務提供者 一個是服務消費者咱們配置倆個服務的分別路由 discovery-server、client-commonspring-boot
ribbon.eager-load.enabled true 強制加載 false 默認懶加載微服務
true日誌打印效果 false 將不打印這段日誌優化
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@256589a1ui
將三個服務所有啓動。服務提供者和服務消費者還有zuul 服務 在控制檯 輸入命令 curl http://localhost:9083/client/client/test ![file](https://image-static.segmentfault.com/222/670/222670908-5db86371749bb_articlex) 咱們看到打印效果,請求經過網關服務成功轉發到了咱們的下游服務上。並返回 - ribbon-isolation-strategy - ignored-services - sensitiveHeaders 以上幾個參數、還有zuul服務的路由攔截器的使用,將在下一篇講解。 如何喜歡能夠關注分享本公衆號。 ![file](https://image-static.segmentfault.com/460/307/460307384-5db8357f93718_articlex) 版權聲明:本文爲博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接和本聲明。轉載請附帶公衆號二維碼