Spring Cloud Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2和 Project Reactor等技术。Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等。
Spring Cloud Gateway 具有如下特性:
Spring Cloud Gateway的重要
和Zuul的对比:
Spring Cloud Gateway 支持两种不同的配置路由的方式:编码式和yml文件配置
创建Spring Boot项目,添加Spring Cloud Gateway依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
</dependencies>
</dependencyManagement>
项目创建后,通过配置RouteLocator实现请求转发
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
RouteLocator routeLocator(RouteLocatorBuilder builder){
return builder.routes().route("test_route", r -> r.path("/get").uri("http://httpbin.org")).build();
}
}
配置完成后,重启项目,请求http://localhost:8080/get

对应properties配置示例
spring.cloud.gateway.routes[0].id=test_route
spring.cloud.gateway.routes[0].uri=http://httpbin.org
spring.cloud.gateway.routes[0].predicates[0]=Path=/get
对应YML配置示例
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Path=/get
在Gateway项目上添加依赖,注册Gateway到Eureka上。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置文件中添加注册中心配置,并开启自动代理
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Path=/get
discovery:
locator:
enabled: true # 开启自动代理
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka
logging:
level:
org.springframework.cloud.gateway: debug
启动Eureka服务,启动Gateway和provider(服务提供者)服务,注册Gateway和provider服务到Eureka注册中心上。

provider服务的端口号为1113,Gateway的端口为8080.下面通过Gateway访问provider服务的hello接口

Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。Spring Cloud Gateway包括许多内置的Route Predicate工厂( 包括时间匹配,请求方式匹配,请求路径匹配,参数匹配等)。所有这些Predicate都与HTTP请求的不同属性匹配。多个Route Predicate工厂可以进行组合,下面我们来介绍一些常用的Route Predicate。
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- After=2021-01-01T01:01:01+08:00[Asia/Shanghai]
表示请求时间在 2021-01-01T01:01:01+08:00[Asia/Shanghai] 时间之后才会被路由。
除了 After 之外,还有两个关键字:
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Method=GET
配置表示只给GET请求路由
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Path=/2019/0612/{segment}
表示路径满足 /2019/0612/ 这个规则,都会被进行转发
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Query=name
表示请求中一定要有 name 参数才会进行转发,否则不会进行转发,也可以指定参数和参数的值。
例如参数的key为name,value必须要以java开始:
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Query=name,java.*
上面多种匹配方式也可以组合使用
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Query=name,java.*
- Path=/2019/0612/{segment}
- Method=GET
带有指定Cookie的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Cookie=username
带有指定Cookie的请求会匹配该路由。如下带有请求头为"X-Request-Id:test123"的请求可以匹配该路由。
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Header=X-Request-Id
带有指定Host的请求会匹配该路由。如下带有Host:www.baidu.com的请求会匹配到该路由
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- Host=www.**.com
从指定远程地址发起的请求可以匹配该路由。
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://httpbin.org
predicates:
- RemoteAddr=192.168.1.1/24
使用权重来路由相应请求,以下表示有80%的请求会被路由到localhost:8201,20%会被路由到localhost:8202。
spring:
cloud:
gateway:
routes:
- id:weight_high
uri:http://localhost:8201
predicates:
- Weight=group1,8
- id: weight_low
uri: http://localhost:8202
predicates:
- Weight=group1,2
Spring Cloud Gateway中的过滤器分为两大类:GatewayFilter和GlobalFilter。
使用AddRequestParameter过滤器,给/test_get请求自动额外添加参数
spring:
cloud:
gateway:
routes:
- id: test_route
uri: lb://provider
filters:
- AddRequestParameter=name,robot
predicates:
- Path=/test_get
uri: lb://provider的作用是开启负载均衡功能,provider服务提供接口/test_get

浏览器请求Gateway,实现转发到provider的test_get接口,并自动添加上参数。

请求test_get接口,已有参数的话会额外加上过滤器配置的参数。

StripPrefix GatewayFilter是对指定数量的路径前缀进行去除的过滤器。
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://localhost:8201
predicates:
- Path=/user-service/**
filters:
- StripPrefix=2
StripPrefix=2配置会把以/user-service/开头的请求的路径去除两位,即请求
http://localhost:9201/user-service/a/user/1
相当于请求该地址:
http://localhost:8201/user/1
PrefixPath GatewayFilter会对原有路径进行增加操作的过滤器。
spring:
cloud:
gateway:
routes:
- id: test_route
uri: http://localhost:8201
predicates:
- Path=/user-service/**
filters:
- PrefixPath=/user
配置请求前缀为/user-service/的请求添加/user路径前缀,即请求
http://localhost:9201/user-service/1
相当于请求该地址:
http://localhost:8201/user-service/user/1
Hystrix 过滤器允许我们将断路器功能添加到网关路由中,使服务免受级联故障的影响,并提供服务降级处理。
首先在pom.xml中添加Hystrix的相关依赖,开启断路器功能
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
添加相关服务降级的处理类
@RestController
public class FallbackController {
@GetMapping("/fallback")
public Object fallback() {
Map<String,Object> result = new HashMap<>();
result.put("data",null);
result.put("message","get request fallback!");
result.put("code",500);
return result;
}
}
在配置文件中添加相关配置:当路由出错时会转发到服务降级处理的控制器上。
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://localhost:8201
predicates:
- Method=GET
filters:
- name: Hystrix
args:
name: fallback
fallbackUri: forward:/fallback
请求http://localhost:8080/1发现已经返回了服务降级的处理信息,因为http://localhost:8201/没有提供该接口。

RequestRateLimiter 过滤器可以用于限流,使用RateLimiter实现来确定是否允许当前请求继续进行,如果请求太大默认会返回HTTP 429-太多请求状态。
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
添加限流策略的配置类,提供策略ipKeyResolver根据访问IP进行限流。
@Configuration
public class RedisRateLimiterConfig {
@Bean
public KeyResolver ipKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
}
在yml配置文件中添加Redis和RequestRateLimiter的配置,这里配置对所有的GET请求都进行了按IP来限流的操作。
spring:
redis:
host: localhost
password: 123
port: 6379
cloud:
gateway:
routes:
- id: test_route
uri: lb://provider
filters:
- AddRequestParameter=name,robot
predicates:
- Path=/test_get
- id: hystrix_route
uri: http://localhost:8201
predicates:
- Method=GET
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/fallback
- id: requestratelimiter_route
uri: http://localhost:1113
predicates:
- Method=GET
filters:
- name: RequestRateLimiter
args:
#每秒允许处理的请求数量
redis-rate-limiter.replenishRate: 1
#每秒最大处理的请求数量
redis-rate-limiter.burstCapacity: 2
#限流策略,对应策略的Bean
key-resolver: "#{@ipKeyResolver}"
discovery:
locator:
enabled: true # 开启自动代理
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka
logging:
level:
org.springframework.cloud.gateway: debug
启动provider服务,注册到注册中心http://localhost:1111/eureka上,提供hello接口。频繁多次浏览器访问:http://localhost:8080/hello ,会返回状态码为429的错误;
