1、Zuul路由網關是什麼?
答:Zuul包含了對請求的路由和過濾兩個最主要的功能,其中路由功能負責將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口的基礎而過濾器功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎。Zuul和Eureka進行整合,將Zuul自身註冊爲Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的消息,也即以後的訪問微服務都是通過Zuul跳轉後獲得。
注意:Zuul服務最終還是會註冊進Eureka。Zuul提供了代理、路由、過濾三大功能。
2、新建模塊Module,microservicecloud-zuul-gateway-9527,修改pom.xml配置文件,如下所示:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.bie.springcloud</groupId> 8 <artifactId>microservicecloud</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <artifactId>microservicecloud-zuul-gateway-9527</artifactId> 12 13 <dependencies> 14 <!-- zuul路由網關 --> 15 <dependency> 16 <groupId>org.springframework.cloud</groupId> 17 <artifactId>spring-cloud-starter-zuul</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-starter-eureka</artifactId> 22 </dependency> 23 <!-- actuator監控 --> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-actuator</artifactId> 27 </dependency> 28 <!-- hystrix容錯 --> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-hystrix</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-config</artifactId> 36 </dependency> 37 <!-- 日常標配 --> 38 <dependency> 39 <groupId>com.bie.springcloud</groupId> 40 <artifactId>microservicecloud-api</artifactId> 41 <version>${project.version}</version> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-jetty</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-web</artifactId> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-starter-test</artifactId> 54 </dependency> 55 <!-- 熱部署插件 --> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>springloaded</artifactId> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-devtools</artifactId> 63 </dependency> 64 </dependencies> 65 66 </project>
修改模塊microservicecloud-zuul-gateway-9527的application.yml配置文件,如下所示:
1 server: 2 port: 9527 # 端口號 3 4 spring: 5 application: 6 name: microservicecloud-zuul-gateway # 微服務的名稱 7 8 eureka: 9 client: # 客戶端註冊進eureka服務列表內 10 service-url: 11 # defaultZone: http://localhost:7001/eureka 12 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka 13 instance: 14 instance-id: gateway-9527.com # 將eureka-server註冊中心的服務,顯示你想看的名稱 15 prefer-ip-address: true # 訪問路徑可以顯示IP地址 16 17 18 info: # 微服務info內容顯示詳細信息 19 app.name: microservicecloud-zuul-gateway-9527 # 應用名稱 20 company.name: www.baidu.com # 公司地址 21 build.artifactId: $project.artifactId$ # 構建項目artifactId 22 build.version: $project.version$ # 構建項目版本號
修改模塊microservicecloud-zuul-gateway-9527的主啓動類,如下所示:
1 package com.bie; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 7 /** 8 * 9 * 10 * @author biehl 11 * 12 */ 13 @SpringBootApplication 14 @EnableZuulProxy // zuul服務網關相關的 15 public class MicroServiceCloudZuul9527Application { 16 17 public static void main(String[] args) { 18 SpringApplication.run(MicroServiceCloudZuul9527Application.class, args); 19 } 20 21 }
啓動三個節點的Eureka Server註冊中心,啓動microservicecloud-provider-dept-8001服務提供者模塊,啓動你的microservicecloud-zuul-gateway-9527路由模塊。開始進行測試如下所示:
啓動路由訪問如下所示:http://127.0.0.1:9527/microservicecloud-provider-dept/dept/get/1
2、Zuul路由訪問映射規則。
1 server: 2 port: 9527 # 端口號 3 4 spring: 5 application: 6 name: microservicecloud-zuul-gateway # 微服務的名稱 7 8 eureka: 9 client: # 客戶端註冊進eureka服務列表內 10 service-url: 11 # defaultZone: http://localhost:7001/eureka 12 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka 13 instance: 14 instance-id: gateway-9527.com # 將eureka-server註冊中心的服務,顯示你想看的名稱 15 prefer-ip-address: true # 訪問路徑可以顯示IP地址 16 17 18 # Zuul路由訪問映射規則。 19 zuul: 20 # prefix: /zuul # 設置統一公共前綴 21 # ignored-services: microservicecloud-provider-dept # 忽略原真實服務名稱。單個可以使用具體的名稱,多個可以使用"*"進行忽略 22 ignored-services: "*" 23 routes: 24 mydept.serviceId: microservicecloud-provider-dept # 真實微服務應用名稱 25 mydept.path: /mydept/** # 使用mydept替代microservicecloud-provider-dept進行路由訪問 26 prefix: /zuul 27 28 29 info: # 微服務info內容顯示詳細信息 30 app.name: microservicecloud-zuul-gateway-9527 # 應用名稱 31 company.name: www.baidu.com # 公司地址 32 build.artifactId: $project.artifactId$ # 構建項目artifactId 33 build.version: $project.version$ # 構建項目版本號
效果如下所示:
作者:別先生
博客園:https://www.cnblogs.com/biehongli/
如果您想及時得到個人撰寫文章以及著作的消息推送,可以掃描上方二維碼,關注個人公衆號哦。