七、SpringCloud——zuul網關 Demo

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=*

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章