1 Zuul網關簡介
1)Zuul 是什麼
Zuul包含了對請求的路由和過濾兩個最主要的功能:
其中路由功能負責将外部請求轉發到具體的微服務執行個體上,是實作外部通路統一入口的基礎而過濾器功能則負責對請求的處理過程進行幹預,是實作請求校驗、服務聚合等功能的基礎.
Zuul和Eureka進行整合,将Zuul自身注冊為Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的消息,也即以後的通路微服務都是通過Zuul跳轉後獲得。
注意:Zuul服務最終還是會注冊進Eureka,提供=代理+路由+過濾三大功能
2)Zuul能幹嘛
路由=---------] `zxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxazx55555
過濾
官網資料 https://github.com/Netflix/zuul/wiki/Getting-Started
2 Zuul網關 Demo
一、路由基本配置
1)建立子Module子產品microservicecloud-zuul-gateway-9527
2)pom.xml
<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><!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->
<groupId>com.tang</groupId>
<artifactId>micro-service-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)配置檔案
server.port=9527
spring.application.name=microservicecloud-zuul-gateway
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
eureka.instance.instance-id=gateway-9527.com
eureka.instance.prefer-ip-address=true
info.app.name=atguigu-microcloud
info.company.name=www.atguigu.com
info.build.artifactId=$project.artifactId$
info.build.version=$project.version$
4)hosts修改
127.0.0.1 myzuul.com
5)主啟動類 添加@EnableZuulProxy注解
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
6)啟動
三個eureka叢集
一個服務提供類microservicecloud-provider-dept-8001
一個路由
7)測試
不用路由 http://localhost:8001/dept/get/2
使用路由http://myzuul.com:9527/microservicecloud-dept/dept/get/2
(網關/服務名/服務路徑)
2種方式都可以通路
二、路由通路映射規則
工程microservicecloud-zuul-gateway-9527
1)配置代理路由名稱
zuul.routes.mydept.serviceId=microservicecloud-dept
zuul.routes.mydept.path=/mydept/**
不用路由http://localhost:8001/dept/get/2
使用代理名稱路由http://myzuul.com:9527/mydept/dept/get/2
使用路由http://myzuul.com:9527/microservicecloud-dept/dept/get/2
3種方式都可以通路
2)原真實服務名忽略(避免服務名的暴露)
單個具體,多個可以用"*"
zuul.routes.mydept.serviceId=microservicecloud-dept
zuul.routes.mydept.path=/mydept/**
zuul.ignored-services=microservicecloud-dept
#zuul.ignored-services=* 忽略多個
http://myzuul.com:9527/microservicecloud-dept/dept/get/2通路失效
3)設定統一公共字首
http://myzuul.com:9527/tang/mydept/dept/get/2
zuul.prefix=/tang
zuul.routes.mydept.serviceId=microservicecloud-dept
zuul.routes.mydept.path=/mydept/**
zuul.ignored-services=*
4)最終的配置
server.port=9527
spring.application.name=microservicecloud-zuul-gateway
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
eureka.instance.instance-id=gateway-9527.com
eureka.instance.prefer-ip-address=true
info.app.name=tang-microcloud
info.company.name=www.tang.com
info.build.artifactId=$project.artifactId$
info.build.version=$project.version$
zuul.prefix=/tang
zuul.routes.mydept.serviceId=microservicecloud-dept
zuul.routes.mydept.path=/mydept/**
zuul.ignored-services=*