參考文檔:Spring Gateway官方文檔 , 玹霖的博客
1.Spring Gateway簡(jiǎn)介
Spring Cloud Gateway
是Spring官方基于Spring 5.0贤旷,Spring Boot 2.0和Project Reactor等技術(shù)開發(fā)的網(wǎng)關(guān)骏庸,Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單而有效的統(tǒng)一的API路由管理方式污秆。Spring Cloud Gateway作為Spring Cloud生態(tài)系中的網(wǎng)關(guān)杏节,目標(biāo)是替代Netflix ZUUL改化,其不僅提供統(tǒng)一的路由方式,并且基于Filter鏈的方式提供了網(wǎng)關(guān)基本的功能宇挫,例如:安全稼跳,監(jiān)控/埋點(diǎn),和限流等阶剑。
2.Spring Gateway工作原理
3.Spring Gateway配置和使用
由于項(xiàng)目使用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>
請(qǐng)注意:不要引入spring-boot-starter-web
包,會(huì)導(dǎo)致Gateway啟動(dòng)拋出異常
Spring Gateway支持兩種方式提供路由服務(wù)牧愁,其一是配置文件啟用素邪,其二則是通過代碼達(dá)到目的
-
application.yml
配置實(shí)現(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默認(rèn)轉(zhuǎn)發(fā)是全路徑的,設(shè)置StripPrefix=1
表示從二級(jí)url路徑轉(zhuǎn)發(fā)猪半,即http://localhost:port/activity/test
將會(huì)轉(zhuǎn)發(fā)到http://{activity}/test
- 代碼實(shí)現(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();
}