1 zuul簡介
Zuul 的官方介紹是 “Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.”
大致是說zuul是設備和網站到Netflix后台應用程序的所有的請求的前門,是一個邊緣化應用程序,它的創建是為了實現動態路由,監控,彈性,和安全性, 它還能夠根據需要將請求路由到多個Amazon Auto Scaling組。
其實zuul主要實現的功能就是API Gateway(api網關)的功能,為什么使用api gateway:
1 客戶端會多次請求不同的微服務,導致客戶端復雜度增加,使用網關時客戶端只與網關交互,降低客戶端的調用邏輯的復雜度,同時網關也可以實現認證邏輯簡化內部服務的之間相互調用的復雜度。
2 對不同客戶端的支持及數據的聚合,如一個網站有web端,手機端,頁面所需的數據有同有異,可以將數據整合或者裁剪,減少客戶端的請求次數,比如BFF架構。
3可以更好的對項目的微服務封裝,可將項目的微服務統一封裝在一個內網環境中,只通過網關提供服務,同時網關也可以對安全,認證,監控,防御單獨強化。
2 Demo大致介紹
2.1 eureka-server 用來提供eureka的服務
2.2 eureka-consumer 用來飾演服務消費者
2.3 zuul 用來提供路由服務

3 在zuul服務的pom.xml中添加相關依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
4 在yml文件中做相關配置
spring: application: name: zuul eureka: instance: lease-expiration-duration-in-seconds: 30 lease-renewal-interval-in-seconds: 10 prefer-ip-address: true ip-address: 127.0.0.1 instance-id: dragon.com client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:7001/eureka/ server: port: 7003 zuul: # 隱藏所有微服務名稱(即使用微服務名稱無法訪問到服務) ignored-services: "*" # 服務前綴名,想要訪問項目之前要加上此路徑 prefix: /fengyuntec routes: # 想要被替換的服務名稱(con. 自己取得名字) con.serviceId: eureka-consumer # 替換后訪問的名稱 con.path: /con/**
5 在主類上添加注解
@SpringBootApplication @EnableZuulProxy public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
