簡述:外部接口的統一訪問網關. Zuul包含了對請求的路由和過濾兩個最主要的功能: 其中路由功能負責將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口的基礎而過濾器功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎.Zuul和Eureka進行整合,將Zuul自身註冊爲Eureka服務治理下的應用,同時從Eureka中得到其餘微服務的消息,也即之後的訪問微服務都是經過Zuul跳轉後得到。 注意:Zuul服務最終仍是會註冊進Eureka 提供=代理+路由+過濾三大功能
1》、新建cloud-zuul-9527 moduleweb
2》、POMspring
zuul服務也是要整合進eureka中的api
<dependencies> <!-- zuul路由網關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- actuator監控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- hystrix容錯--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 平常標配 --> <dependency> <groupId>com.lee</groupId> <artifactId>cloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
3》、YML新增app
server: port: 9527 spring: application: name: cloud-zuul-gateway eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka instance: instance-id: gateway-9527.com prefer-ip-address: true info: app.name: cloud author.name: lee app.function: 網關 build.artifactId: $project.artifactId$ build.version: $project.version$
4》、HOST配置修改分佈式
127.0.0.1 zuul9527.com
5》、主啓動類ide
package com.lee.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class Zuul_9527_App { public static void main(String[] args) { SpringApplication.run(Zuul_9527_App.class,args); } }
測試:spring-boot
①、啓動3個eureka集羣 ②、啓動cloud-provider-dept-8001服務提供者 ③、一個路由cloud-zuul-9527 ④、訪問: 不用路由:http://localhost:8001/dept/get/1 啓用路由:http://zuul9527.com:9527/cloud-dept/dept/get/1 注意:zuul和feign或ribbon沒有多大關係。feign、ribbon http://localhost7001/consumer/xxx之類的訪問和zuul.com:9521/cloud-dept/xxxx訪問沒有關係。在實際項目中feign\ribbon這些服務的消費方通常也是以服務的提供發而存在的。
3.1>、緣由:微服務
before: http://zuul9527.com:9527/cloud-dept/dept/get/1訪問路徑中cloud-dept微服務名稱容易暴露出來,因此咱們要將其隱藏置換。
修改:YML測試
zuul: routes: mydept.serviceId: microservicecloud-dept #微服務實例名稱 mydept.path: /mydept/** #映射名稱
測試:ui
http://zuul9527.com:9527/mydept/dept/list
3.2>、緣由:
before: http://zuul9527.com:9527/cloud-dept/dept/get/1 還能看到,上面這一步只作了置換,沒作隱藏
修改YML
忽略單個: zuul: ignored-services: cloud-dept 忽略多個: zuul: ignored-services: "*"
測試:
http://zuul9527.com:9527/cloud-dept/dept/get/1
3.3>、緣由:
設置一個公共的前綴名
修改YML
zuul: prefix: /lee
測試:
http://zuul9527.com:9527/lee/mydept/dept/list 若是這樣訪問不了,多是yml的問題,在POM文件中引入 <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.25</version> </dependency> 便可
未完待續。。。。