什么是 Spring Cloud Gateway
Spring Cloud Gateway 是 Spring 官方基於 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術開發的網關,Spring Cloud Gateway 旨在為微服務架構提供一種簡單而有效的統一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態系中的網關,目標是替代 Netflix ZUUL,其不僅提供統一的路由方式,並且基於 Filter 鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。
#Spring Cloud Gateway 功能特征
- 基於 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
- 動態路由
- Predicates 和 Filters 作用於特定路由
- 集成 Hystrix 斷路器
- 集成 Spring Cloud DiscoveryClient
- 易於編寫的 Predicates 和 Filters
- 限流
- 路徑重寫
#Spring Cloud Gateway 工程流程
客戶端向 Spring Cloud Gateway 發出請求。然后在 Gateway Handler Mapping 中找到與請求相匹配的路由,將其發送到 Gateway Web Handler。Handler 再通過指定的過濾器鏈來將請求發送到我們實際的服務執行業務邏輯,然后返回。
過濾器之間用虛線分開是因為過濾器可能會在發送代理請求之前(pre
)或之后(post
)執行業務邏輯。
#POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.snake</groupId>
<artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-gateway</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-gateway</name>
<url>http://www.snake.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- Commons Begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- Commons Begin -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.funtl.hello.spring.cloud.gateway.GatewayApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要增加了 org.springframework.cloud:spring-cloud-starter-gateway
依賴
#特別注意
- Spring Cloud Gateway 不使用 Web 作為服務器,而是 使用 WebFlux 作為服務器,Gateway 項目已經依賴了
starter-webflux
,所以這里 千萬不要依賴 starter-web - 由於過濾器等功能依然需要 Servlet 支持,故這里還需要依賴
javax.servlet:javax.servlet-api
#Application
package com.snake.hello.spring.cloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
#application.yml
spring:
application:
# 應用名稱
name: spring-gateway
cloud:
# 使用 Naoos 作為服務注冊發現
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 使用 Sentinel 作為熔斷器
sentinel:
transport:
port: 8721
dashboard: localhost:8080
# 路由網關配置
gateway:
# 設置與服務注冊發現組件結合,這樣可以采用服務名的路由策略
discovery:
locator:
enabled: true
# 配置路由規則
routes:
# 采用自定義路由 ID(有固定用法,不同的 id 有不同的功能,詳見:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
- id: NACOS-CONSUMER
# 采用 LoadBalanceClient 方式請求,以 lb:// 開頭,后面的是注冊在 Nacos 上的服務名
uri: lb://nacos-consumer
# Predicate 翻譯過來是“謂詞”的意思,必須,主要作用是匹配用戶的請求,有很多種用法
predicates:
# Method 方法謂詞,這里是匹配 GET 和 POST 請求
- Method=GET,POST
- id: NACOS-CONSUMER-FEIGN
uri: lb://nacos-consumer-feign
predicates:
- Method=GET,POST
server:
port: 9000
# 目前無效
feign:
sentinel:
enabled: true
# 目前無效
management:
endpoints:
web:
exposure:
include: "*"
# 配置日志級別,方別調試
logging:
level:
org.springframework.cloud.gateway: debug
注意:請仔細閱讀注釋
#測試訪問
依次運行 Nacos 服務、NacosProviderApplication
、NacosConsumerApplication
、NacosConsumerFeignApplication
、GatewayApplication
打開瀏覽器訪問:http://localhost:9000/nacos-consumer/echo/app/name 瀏覽器顯示
Hello Nacos Discovery nacos-consumer i am from port 8082
打開瀏覽器訪問:http://localhost:9000/nacos-consumer-feign/echo/hi 瀏覽器顯示
Hello Nacos Discovery Hi Feign i am from port 8082
注意:請求方式是 http://路由網關IP:路由網關Port/服務名/**
至此說明 Spring Cloud Gateway 的路由功能配置成功