參考文檔:Spring Gateway官方文檔 , 玹霖的博客
1.Spring Gateway簡介
Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技術開發(fā)的網(wǎng)關,Spring Cloud Gateway旨在為微服務架構提供一種簡單而有效的統(tǒng)一的API路由管理方式。Spring Cloud Gateway作為Spring Cloud生態(tài)系中的網(wǎng)關,目標是替代Netflix ZUUL,其不僅提供統(tǒng)一的路由方式,并且基于Filter鏈的方式提供了網(wǎng)關基本的功能,例如:安全,監(jiān)控/埋點,和限流等。
2.Spring Gateway工作原理

Spring Gateway工作結構圖

spring_cloud_gateway_diagram.png
3.Spring Gateway配置和使用
由于項目使用maven模塊化配置,Springboot版本為:2.0.3.RELEASE, SpringCloud版本為:Finchley.RELEASE
- pom配置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
請注意:不要引入spring-boot-starter-web包,會導致Gateway啟動拋出異常
Spring Gateway支持兩種方式提供路由服務,其一是配置文件啟用,其二則是通過代碼達到目的
-
application.yml配置實現(xiàn)
spring:
application:
name: xxxx
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
service-name: xxxx
prefer-ip-address: true
gateway:
discovery:
locator:
enabled: true
routes:
# This route rule used to forward request to activity server
- id: activity-route
uri: lb://activity
predicates:
- Path=/activity/**
filters:
- StripPrefix=1
注意:Gateway默認轉(zhuǎn)發(fā)是全路徑的,設置StripPrefix=1表示從二級url路徑轉(zhuǎn)發(fā),即http://localhost:port/activity/test將會轉(zhuǎn)發(fā)到http://{activity}/test
- 代碼實現(xiàn)
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/activity/**")
.filters(f -> f.stripPrefix(1).filter(new TestGetWayFilter()).addResponseHeader("X-Response-Default-Foo", "Default-Bar"))
.uri("lb://activity")
.order(0)
.id("activity-route")
)
.build();
}