SpringCloudGateway

参考资料
SpringCloud中替代Zuul的存在

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

注意不能添加starter-web依赖,否则会冲突

基本配置(基于yml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server:
port: 8999
spring:
application:
name: gateway-server
cloud:
gateway:
routes:
- id: neo_route
## uri: http://www.keeep.top
# 代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://ruoyi-system
## 带有/system的请求都会转发到http://www.keeep.top中
predicates:
- Path=/system/**
## 比如请求的接口是/system/a/1,那么经过StripPrefix=1的拦截,则最后到http://www.keeep.top的请求为/a/1,如果StripPrefix=2,则为/1
filters:
- StripPrefix=1
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/

进阶配置(基于yml)

限流

1
2
3
4
5
<!-- spring data redis reactive 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
redis:
host: localhost
port: 6379
password:
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: lb://ruoyi-system
predicates:
- Path=/system/**
filters:
- StripPrefix=1
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 1 # 令牌桶每秒填充速率
redis-rate-limiter.burstCapacity: 2 # 令牌桶总容量
key-resolver: "#{@pathKeyResolver}" # 使用 SpEL 表达式按名称引用 bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ruoyi.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
* 限流规则配置类
*/
@Configuration
public class KeyResolverConfiguration
{
@Bean
public KeyResolver pathKeyResolver()
{
return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
}
}

通过时间匹配

1
2
3
4
5
## 在某某时间之后/之前/之间发生转发
predicates:
- After=2018-01-20T06:06:06+08:00[Asia/Shanghai]
- Before=2018-01-20T06:06:06+08:00[Asia/Shanghai]
- Between=2018-01-20T06:06:06+08:00[Asia/Shanghai], 2019-01-20T06:06:06+08:00[Asia/Shanghai]

通过Cookie匹配

1
2
3
## 匹配Cookie为[name,ymt]的请求,注意的是不能设置多个Cookie
predicates:
- Cookie=name,ymt

测试:curl localhost:8999 --cookie "name=ymt"成功转发

通过 Header 属性匹配

1
2
3
## 匹配Header为[name=X-Request-Id,value=任意数字]的请求,可以为正则表达
predicates:
- Header=X-Request-Id,\d+

测试:curl http://localhost:8080 -H "X-Request-Id:666666"

通过HOST匹配

1
2
predicates:
- Host=**.ityouknow.com

测试:curl http://localhost:8080 -H "Host: www.ityouknow.com"

测试:curl http://localhost:8080 -H "Host: md.ityouknow.com"

通过请求方式匹配

1
2
predicates:
- Method=GET

测试:curl -X POST http://localhost:8080

通过请求路径匹配

1
2
predicates:
- Path=/foo/{segment}

测试:curl http://localhost:8080/foo/xx

通过请求参数匹配

1
2
3
## 请求参数包含smile的请求进行转发,注意的是这只有GET请求可以拦截,POST就算包涵smile参数,请求也是拦截不到的
predicates:
- Query=smile

测试curl localhost:8080?smile=x&id=2

1
2
3
## 这样只要当请求中包含 keep 属性并且参数值是以 pu 开头的长度为三位的字符串才会进行匹配和路由
predicates:
- Query=keep,pu.

测试:curl localhost:8080?keep=pub

通过请求 ip 地址进行匹配

1
2
predicates:
- RemoteAddr=192.168.0.186/24

测试:curl localhost:8080

多种拦截配合

1
2
3
4
predicates:
- Query=smile
- Method=GET
- Cookie=name,ymt

有多个条件的存在,必须全部满足之后才会进行转发

测试:curl -X GET "localhost:8999?&smile=1" --cookie "name=ymt"

赏个🍗吧
0%