之前我們介紹了 zuul網關服務,今天聊聊spring cloud gateway 作為spring cloud的親兒子網關服務。很多的想法都是參照zuul,為了考慮zuul 遷移到gateway 提供了一個便利的條件。
gateway 他的核心功能也是和zuul 類似。但是他的實現方式與zuul 卻有些不一樣,他的核心是基於 Spring Boot 2.x, Spring WebFlux和Project Reactor 構建的。
- Spring WebFlux 響應式Web框架。
- Spring WebFlux是基於響應式流的,因此可以用來建立異步的、非阻塞的、事件驅動的服務。它采用Reactor作為首選的響應式流的實現庫,不過也提供了對RxJava的支持。
由於響應式編程的特性,Spring WebFlux和Reactor底層需要支持異步的運行環境,比如Netty和Undertow;也可以運行在支持異步I/O的 - Servlet 3.1的容器之上,比如Tomcat(8.0.23及以上)和Jetty(9.0.4及以上)。
- Spring WebFlux是基於響應式流的,因此可以用來建立異步的、非阻塞的、事件驅動的服務。它采用Reactor作為首選的響應式流的實現庫,不過也提供了對RxJava的支持。
- spring-webflux上層支持兩種開發模式:
- 類似於Spring WebMVC的基於注解(@Controller、@RequestMapping)的開發模式;
- Java 8 lambda 風格的函數式開發模式。
- Spring WebFlux也支持響應式的Websocket服務端開發。
所以spring cloud gateway 不是基於阻塞的web 開發。他與傳統的Servlet是存在沖突的。在創建功能的時候要排除掉傳統的Servlet jar包引用
工作原理
客戶端向Spring Cloud Gateway發出請求。如果網關處理程序映射確定請求與路由匹配,則將其發送到網關Web處理程序。該處理程序運行通過特定於請求的篩選器鏈發送請求。篩選器由虛線分隔的原因是,篩選器可以在發送代理請求之前或之后執行邏輯。執行所有“前置”過濾器邏輯,然后發出代理請求。發出代理請求后,將執行“發布”過濾器邏輯。
注意: 在沒有端口的路由中定義的URI將分別將HTTP和HTTPS URI的默認端口分別設置為80和443
- Predicate 斷言:這是一個 Java 8 的 Predicate。輸入類型是一個 ServerWebExchange。我們可以使用它來匹配來自 HTTP 請求的任何內容,例如 headers 或參數。
- Route 路由轉發 它由一個 serverID,一個目標 URI,一組斷言和一組過濾器定義。如果斷言為真,則路由匹配。
- Filter 請求過濾 對web資源進行攔截,做一些處理后再交給處理器處理
修改之前工程的pom 文件總pom 里面我們有一個 spring-boot-starter-web 工程引用,刪除掉。在服務里面單獨依賴。上面已經講述過,傳統Servlet的jar包沖突問題。
在服務消費者和 服務提供者分別添加
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
我們創建工程 cloud-gateway ,修改pom
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
創建 bootstrap.yml
server:
port: 9000
spring:
profiles:
active: dev
application:
name: cloud-gateway-demo
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
cloud:
nacos:
discovery:
server-addr: 47.99.209.72:8848
# ${prefix}-${spring.profile.active}.${file-extension}
config:
server-addr: 47.99.209.72:8848
file-extension: yaml
gateway:
discovery:
locator:
# 是否與服務發現組件進行結合,通過serviceId轉發到具體的服務實例。默認false,
# 為true代表開啟基於服務發現的路由規則。
enabled: true
# 配置之后訪問時無需大寫
lower-case-service-id: true
routes:
- id: cloud-discovery-server
uri: lb://cloud-discovery-server
predicates:
# 路徑匹配,以 api 開頭,直接配置是不生效的,看 filters 配置
- Path=/server/**
filters:
# 前綴過濾,默認配置下,我們的請求路徑是 http://localhost:9000/myshop-service-consumer-item/** 這時會路由到指定的服務
# 此處配置去掉 1 個路徑前綴,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式訪問了
- StripPrefix=1
- id: cloud-discovery-client
uri: lb://cloud-discovery-client
predicates:
# 路徑匹配,以 api 開頭,直接配置是不生效的,看 filters 配置
- Path=/client/**
filters:
# 前綴過濾,默認配置下,我們的請求路徑是 http://localhost:9000/myshop-service-consumer-item/** 這時會路由到指定的服務
# 此處配置去掉 1 個路徑前綴,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式訪問了
- StripPrefix=1
創建main 啟動類
package com.xian.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
*
* @Author: xlr
* @Date: Created in 上午11:08 2019/11/4
*/
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
啟動服務 命令行curl http://localhost:9000/client/client/test
服務已經整合完畢。路由功能轉發已經實現。配置文件的一些字段的說明也在注釋上說明。
下一篇講述一下 Spring Cloud Gateway 斷言
如何喜歡可以關注分享本公眾號。
版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。轉載請附帶公眾號二維碼