【SpringCloud】Gateway網關入門(十六)


Gateway介紹

  Spring Cloud Gateway是Spring Cloud的一個全新項目,基於Spring 5,Spring Boot 2和 Project Reactor等技術開發的網關,它旨在為微服務框架提供一種簡單有效的統一的API路由管理方式,以及基於Filter方式提供一些強大的過濾功能,例如:熔斷、限流、重試等

  Spring Cloud Gateway作為spring Cloud 生態系統中的網關,目標是替代Zuul,在Spring Cloud 2.0以上版本中,沒有對新版本Zuul 2.0以上最新高性能版本進行集成,仍然還是Zuul 1.x非Reactor模式的老版本,而為了提升網關的性能,Spring Cloud Gateway是基於WebFlux框架實現的,而WebFlux框架底層則使用了高性能的Reactor模式通信框架Netty。

  Spring Cloud Gateway功能:

  1、基於Spring Framework 5,Project Reactor和Spring Boot 2.0構建

  2、能夠匹配任何請求屬性上的路由。

  3、斷言和過濾器特定於路由。

  4、Hystrix斷路器集成。

  5、Spring Cloud DiscoveryClient集成

  6、易於編寫的斷言和過濾器

  7、請求速率限制

  8、路徑改寫

  Spring Cloud 架構圖

  

  由此可以知道網關在整個SpringCloud 中的位置

Gateway工作流程

  工作流程圖

  

  客戶端向Spring Cloud Gateway發出請求。如果網關處理程序(Gateway Handler Mapping)映射確定請求與路由匹配,則將其發送到網關Web處理程序(Gateway Web Handler.)。該處理程序通過特定於請求的過濾器鏈來運行請求。過濾器由虛線分隔的原因是,過濾器可以在發送代理請求之前和之后運行邏輯。所有“pre”過濾器邏輯均被執行。然后發出代理請求。發出代理請求后,將運行“post”過濾器邏輯。

  Filter,在“pre”類型的過濾器可以做參數校驗、權限校驗、流量監控、日志輸出、協議轉換等,在“post”類型的過濾器中可以做響應內容、響應頭的修改,日志的輸出、流量監控等,有非常重要的作用

Gateway使用

  springcloud gateway案例演示,需要搭建一個springcloud項目,參考:【SpringCloud】快速入門(一)

  springcloud項目,包含一個Eureka注冊中心,一個服務提供者,一個服務消費者

  

  1、在以上springcloud的項目基礎上,新建模塊Spring Cloud Gateway(springcloud-gateway-gateway9527)

  2、在Gateway模塊中引入Gateway依賴和Eureka依賴

 1 <!-- Spring Cloud Gateway -->
 2 <dependency>
 3     <groupId>org.springframework.cloud</groupId>
 4     <artifactId>spring-cloud-starter-gateway</artifactId>
 5 </dependency>
 6 
 7 <!-- eureka client -->
 8 <dependency>
 9     <groupId>org.springframework.cloud</groupId>
10     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11 </dependency>

    完整依賴如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-gateway-gateway9527</artifactId>
13     <build>
14         <plugins>
15             <plugin>
16                 <groupId>org.apache.maven.plugins</groupId>
17                 <artifactId>maven-compiler-plugin</artifactId>
18                 <configuration>
19                     <source>8</source>
20                     <target>8</target>
21                 </configuration>
22             </plugin>
23         </plugins>
24     </build>
25 
26     <dependencies>
27 
28         <!-- Spring Cloud Gateway -->
29         <dependency>
30             <groupId>org.springframework.cloud</groupId>
31             <artifactId>spring-cloud-starter-gateway</artifactId>
32         </dependency>
33 
34         <!-- eureka client -->
35         <dependency>
36             <groupId>org.springframework.cloud</groupId>
37             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
38         </dependency>
39 
40         <!-- spring boot -->
41         <!-- gateway 不需要web模塊 -->
42 <!--        <dependency>-->
43 <!--            <groupId>org.springframework.boot</groupId>-->
44 <!--            <artifactId>spring-boot-starter-web</artifactId>-->
45 <!--        </dependency>-->
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-actuator</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>org.springframework.boot</groupId>
52             <artifactId>spring-boot-devtools</artifactId>
53             <scope>runtime</scope>
54             <optional>true</optional>
55         </dependency>
56 
57         <dependency>
58             <groupId>org.projectlombok</groupId>
59             <artifactId>lombok</artifactId>
60             <optional>true</optional>
61         </dependency>
62         <dependency>
63             <groupId>org.springframework.boot</groupId>
64             <artifactId>spring-boot-starter-test</artifactId>
65             <scope>test</scope>
66         </dependency>
67 
68     </dependencies>
69 
70 </project>
pom.xml

    ⚠️注意:不能引入模塊SpringBoot的Web模塊(spring-boot-starter-web),否則啟動報錯,如下:

    

  3、編輯appliction.yml文件

 1 # 端口
 2 server:
 3   port: 9527
 4 
 5 spring:
 6   application:
 7     name: cloud-gateway-gateway
 8   cloud:
 9     gateway:
10       routes:
11         # 路由的ID,沒有固定規則,但要求唯一,建議配合服務名
12         - id: payment_routh
13           # 匹配后提供服務的路由地址
14           uri: http://localhost:8001
15           # 斷言,路徑相匹配的進行路由
16           predicates:
17             - Path=/payment/get/**
18 
19 eureka:
20   client:
21     register-with-eureka: true
22     fetch-registry: true
23     service-url:
24       defaultZone: http://localhost:8761/eureka

  4、測試

    1)啟動項(springcloud-gateway-gateway9527),注冊到Eureka注冊中心

    2)訪問地址:http://localhost:8001/payment/get/1,證明支付模塊服務正常

    3)訪問地址:http://localhost:9527/payment/get/1,驗證網關已生效

      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM