Zuul 包含了對請求的路由和過濾兩個最重要的功能:web
其中路由功能服務將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口的基礎。而過濾的功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎。spring
Zuul 和 Eureka 進行整合,將 Zuul 自身註冊爲 Eureka 服務治理下的應用,同時從 Eureka 中得到其它微服務的消息,以後的微服務訪問都是經過 Zuul 轉發。apache
Zuul 提供了代理、路由、過濾三大功能。api
一、新建名爲 "microservicecloud-zuul-gateway-9527" 的子工程,依賴以下:app
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>microservicecloud</artifactId> <groupId>zze.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-zuul-gateway-9527</artifactId> <dependencies> <!--Eureka 客戶端依賴 ps : Zuul 也要註冊到 Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--Zuul 依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <!--監控--> <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>zze.springcloud</groupId> <artifactId>microservicecloud-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> </project>
二、配置以下:maven
server: port: 9527 spring: application: name: microservicecloud-zuul-gateway-9527 eureka: client: service-url: defaultZone: http://www.eurekaserver1.com:7001/eureka,http://www.eurekaserver2.com:7002/eureka,http://www.eurekaserver3.com:7003/eureka instance: instance-id: microservicecloud-zuul-gateway-9527 prefer-ip-address: true
三、新建主啓動類,使用註解開啓 Zuul 代理:ide
package zze.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy // 開啓 Zuul 代理 public class Application_9527 { public static void main(String[] args) { SpringApplication.run(Application_9527.class, args); } }
四、測試:spring-boot
一、啓動 700一、700二、7003 Eureka 集羣 二、啓動 "microservicecloud-provider-dept-8001" Provider 服務 三、啓動 "microservicecloud-zuul-gateway-9527" Zuul 代理服務 四、訪問 http://localhost:8001/dept/list 正常返回數據 五、訪問 http://localhost:9527/microservicecloud-provider-dept/dept/list 經過 Zuul 轉發獲取到 microservicecloud-provider-dept 服務的數據
即 Zuul 完成了路由轉發 轉發規則爲:Zuul服務地址/註冊到Eureka的服務名稱/對應服務訪問路徑 Zuul服務地址:localhost:9527 註冊到Eureka的服務名稱:microservicecloud-provider-dept 對應服務訪問路徑:/dept/list
修改 "microservicecloud-zuul-gateway-9527" 子工程的配置文件,新增服務映射關係:微服務
# 路由映射配置 zuul: routes: dept.serviceId: microservicecloud-provider-dept dept.path: /mydept/** # 訪問路徑匹配該規則的請求都會被轉發到 microservicecloud-provider-dept 服務
重啓 Zuul 服務,此時也能夠經過 localhost:9527/mydept/dept/list 訪問到 microservicecloud-provider-dept 服務,而且原來 localhost:9527/microservicecloud-provider-dept/dept/list 微服務全名這種方式也不受影響能正常訪問。測試
若是要忽略原來經過微服務全名方式轉發的方式,那麼能夠添加以下配置:
zuul.ignored-services: microservicecloud-provider-dept # microservicecloud-provider-dept 爲忽略的服務名稱,還可經過 * 代指全部的服務名稱,即禁用全部服務以服務名稱方式訪問轉發
還能夠添加統一前綴,只須要添加以下屬性:
zuul.prefix: /zze # 此時訪問路徑就變爲 localhost:9527/zze/mydept/dept/list