Spring Cloud Gateway 的使用
内容概览
一. Springboot 对应的 Springcloud 版本如下~
官网地址 :https://spring.io/projects/spring-cloud
各位大佬 请注意下~ ,最后一句英文还说 不再支持 Dalston, Edgware, Finchley,
Greenwich 这四个版本啦
image-20210215210801486
二. 使用
pom 文件
Hoxton.SR8
org.springframework.cloud
spring-cloud-dependencies
${spring.cloud-version}
pom
import
yaml 文件
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: http://localhost:8080/
predicates:
- Path=/gateway/a
filters:
# 去掉请求路径中的 第一个 如 /gateway/a 变成/a
- StripPrefix=1
# 重新设置这个路由地址
- SetPath=/debug
三. 组成
官方文档
Route
The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.
对应该类(可对比上面 yaml 文件):
image-20210118161805469
Predicate
This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.
当匹配时会拦截该请求
Filter
These are instances of Spring Framework GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.
可以配置添加更多过滤器~
四. 工作原理
由于 gateway 包中 使用了 WEBFLUX , 不能和这个 WEBMVC 同时存在,所以要移除 spring-boot-starter-web 包
image-20210118164727955
工作原理图(来自官网):就一堆过滤器
image-20210118162444635
五. Route Predicate Factories(★★★★★)
Route Predicate Factories:https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#gateway-request-predicates-factories
可以看到这里有 11 种匹配模式~
image-20210216185916746
匹配模式大全(重点!)
这里的匹配满足正则表达式
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
# 匹配在这个时间之后的
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
# 匹配在这个时间之后的
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
# 匹配这个时间中间的
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
# 匹配 cookie 为 chocolate=ch.p 时拦截该请求
- Cookie=chocolate, ch.p
# 当 header 中有 X-Request-Id 该请求头时,且它的值匹配该正则时 拦截
- Header=X-Request-Id, \d+
# 匹配该 host 地址~
- Host=**.somehost.org,**.anotherhost.org
# 匹配该 请求方法
- Method=GET,POST
# 匹配该路径
- Path=/gateway/a
# 匹配该请求参数~ 当请求参数为red 且值为 gree开头的~
- Query=red, gree.
# 匹配 网址为 192.168.1.1 - 192.168.1.254
- RemoteAddr=192.168.1.1/24
根据权重匹配
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
六. Gateway Filter Factories(★★★★★)
Gateway Filter Factories : https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#gatewayfilter-factories
数了下~ 官网居然多达 30 个 filter 给你使用
image-20210216194958056
image-20210216195335403
另外一篇博文中再写写~ , 这里可以先简单参考下 4ye 在上面的 yaml 文件中的配置
七. Global Filters(★★★★★)
Global Filters: https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#global-filters
全局过滤器~😄
image-20210216202020532
自定义过滤器
@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered{
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain){
log.info("custom global filter");
return chain.filter(exchange);
}
@Override
public int getOrder(){
return -1;
}
}