Spring Cloud Gateway GatewayFilter的使用

一、GatewayFilter的作用

路由过滤器允许我们以某种方式修改进来的Request和出去的Response。Spring Cloud Gateway内置很多的 GatewayFilter

二、Spring Cloud Gateway内置的 GatewayFilter

1、AddRequestHeader

1、描述

1、用于向下游服务 添加 请求头,
2、支持 uri variables

2、参数

1、name:向下游服务传递请求头的 key
2、value:向下游服务传递请求头的 value

3、示例

1、方式一、添加一个固定的请求头

spring:
cloud:
nacos:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- AddRequestHeader=x-token,xxxxx

表示会向下游服务传递一个 x-token 的请求头,值是 xxxxx

2、配合 uri variables 添加动态请求头

spring:
cloud:
gateway:
routes:
- id: product-provider-02
uri: lb://product-provider
predicates:
- Path=/product/findOne/{productId}
filters:
- AddRequestHeader=x-token,xxxxx-{productId}

表示会向下游服务传递一个 x-token 的请求头,值是 xxxxx-匹配上的productId的值

2、AddRequestParameter

1、描述

用于向下游服务 添加一个请求参数

2、参数

1、name:添加的参数 key
2、value:添加的参数 value,可以支持 Path或Host 中的 uri variables

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- AddRequestParameter=username,zhangsan

向下游服务增加一个 username = zhangsan 的请求参数

3、AddResponseHeader

1、描述

向下游的响应中增加一个 响应头。

2、参数

1、name:添加的响应头的 key
2、value:添加的响应头的 value,可以支持 Path或Host 中的 uri variables

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- AddResponseHeader=encryption,false

向下游服务响应增加一个 encryption = false 的响应头

4、DedupeResponseHeader

1、描述

移除重复的请求头

2、参数

1、name:需要移除的重复的响应头,多个以 空格 分隔
2、strategy:重复时,移除的策略,默认是RETAIN_FIRST,即保留第一个头

  1. RETAIN_FIRST:保留第一个值
  2. RETAIN_LAST:保留最后一个值
  3. RETAIN_UNIQUE:保留所有的不重复的值

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- DedupeResponseHeader=x-token Access-Control-Allow-Credentials Access-Control-Allow-Origin,RETAIN_FIRST

移除上方指定的重复的响应头,并且保留第一个出现的。

5、MapRequestHeader

1、描述

fromHeader 参数的值 追加到 toHeader 参数中。

  1. fromHeader 在 header 中不存在,那么没有什么影响。
  2. fromHeader 存在
  3.  toHeader 存在,那么往配置中 toHeader 对应的 header 中追加 fromHeader对应的值
  4. toHeader 不存在,那么往 header 中增加一个header ,key: 配置中的toHeader的值,value: fromHeader 在header中的值

2、参数

1、fromHeader:从请求参数中获取header的值
2、toHeader:向header中设置一个 header, key是toheader的值,value 是 fromHeader的值

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- MapRequestHeader=from-header,x-token

即会向 request 中增加一个 key 为 x-token 的header ,值为 header 中 from-header 对应的值。(存在多种情况,参考描述

6、Prefix

1、描述

为匹配到的路由,在转发到下游服务时,增加一个前缀prefix

2、参数

1、prefix:需要增加的路径前缀

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/findOne
filters:
- PrefixPath=/product

访问 http://网关ip:port/findOne ⇒ 转发到下游服务地址 http://product-provider/product/findOne 增加了一个前缀。

7、PreserveHostHeader

1、描述

它表示在Spring Cloud Gateway转发请求的时候,保持客户端的Host信息不变,然后将它传递到下游服务器中。

2、参数

没有参数

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- PreserveHostHeader

8、RemoveRequestHeader

1、描述

移除请求头中的参数

2、参数

1、name:需要移除的请求头

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- RemoveRequestHeader=x-token

9、RemoveResponseHeader

1、描述

移除响应头

2、参数

1、name:需要移除的响应头

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- RemoveResponseHeader=x-token

9、RemoveRequestParameter

1、描述

移除请求参数,往下游服务传递时,此参数就没有来

2、参数

1、name:需要移除的请求参数

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- RemoveRequestParameter=password

10、RewritePath

1、描述

根据正则表达式,执行路径重写

2、参数

1、regexp:匹配的正则表达式
2、replacement:需要替换成的字符串
注意:
1、在yml配置中 $ 需要写成 $\
2、路径替换规则是: path.replaceAll(regexp,replacement)

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/admin/product/findOne
filters:
- RewritePath=/admin(?<segment>/?.*), $\{segment} # 当访问/admin/product/findOne 将会替换成 /product/findOne

页面上访问 /admin/product/findOne ⇒ 到达下游服务的路径是 /product/findOne

11、StripPrefix

1、描述

移除路径前缀,比如访问: /admin/aa/bb/cc 实际的下游服务器地址是 /bb/cc 则可以使用这个实现

2、参数

1、parts:请求的路径按照/分隔后,需要跳过的部分,从1开始。

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/admin/product/findOne
filters:
- StripPrefix=1 # 当访问/admin/product/findOne 将会替换成 /product/findOne

页面上访问 /admin/product/findOne ⇒ 到达下游服务的路径是 /product/findOne

12、RequestSize

1、描述

配置请求体的大小,当请求体过大时,将会返回 413 Payload Too Large

2、参数

1、maxSize:请求体的大小

3、示例

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- name: RequestSize
args:
maxSize: 1B

此处需要通过 filters[index].args.maxSize 配置,否则不生效。

13、ModifyRequestBody

1、描述

修改传递到下游服务 RequestBody 的值,比如我们所有的经过网关的服务,到达下游服务时,都需要将 用户当前的用户名和数据权限传递下去,此时就可以使用这个。

2、需求:

修改原始服务的参数,增加usernameroles参数传递到下游服务。

3、路由配置,只可通过 Java 代码来配置

@Configuration
public class RouteConfig { @Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("product-provider", predicateSpec -> predicateSpec.path("/product/modifyRequestBody")
.filters(gatewayFilterSpec -> gatewayFilterSpec.modifyRequestBody(String.class, Map.class, MediaType.APPLICATION_JSON_VALUE, (exchange, s) -> {
Map<String, Object> params = new HashMap<>(16);
params.put("old", s);
params.put("username", "v_huan");
params.put("roles", "ROLE_ADMIN");
return Mono.just(params);
})).uri("lb://product-provider")).build();
}
}

14、ModifyResponseBody

1、 描述

修改下游服务返回的ResponseBody的值,和上方的RequestBody类似,此处不在处理。

15、更多的 GatewayFilterFactory

在代码中查看 GatewayFilterFactory 的子类即可。

三、配置默认拦截器

1、描述

默认拦截器,对所有的路由都会生效。

2、配置方法

配置文件中使用 spring.cloud.gateway.default-filters 配置

3、示例

spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Red, Default-Blue
- PrefixPath=/httpbin

四、全局过滤器

当某个请求被路由匹配时,那么所有的全局过滤器(GlobalFilter)和路由匹配到的 GatewayFilter会组合成一个过滤器链,排序规则是通过 Spring 的 Ordered 来排序。

GlobalFilterprepost2个执行阶段,优先级越高 pre 阶段执行越早, post阶段执行越迟。

编写一个全局过滤器需要实现 GlobalFilter 接口。
注意:

GlobalFilter的用来在未来的版本中可能会发生改变。

1、ForwardRoutingFilter

ForwardRoutingFilter 用来处理 forward:///localendpoint 请求。

2、LoadBalancerClientFilter

LoadBalancerClientFilter 用来处理 lb://service-name 这样的请求

spring:
cloud:
gateway:
routes:
- id: myRoute
uri: lb://service
predicates:
- Path=/service/**


设置 spring.cloud.loadbalancer.ribbon.enabled=false

3、The Websocket Routing Filter

处理 websocket 请求

spring:
cloud:
gateway:
routes:
# SockJS route
- id: websocket_sockjs_route
uri: http://localhost:3001
predicates:
- Path=/websocket/info/**
# Normal Websocket route
- id: websocket_route
uri: [ws|wss]://localhost:3001
predicates:
- Path=/websocket/**

4、更多全局过滤器

https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#global-filters

五、自定义全局过滤器

  1. 实现 GlobalFilter 接口和 Ordered 接口
  2. 加入到 Spring 管理
  3. 代码实现
@Slf4j
@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String path = exchange.getRequest().getPath().toString();
log.info("进入了全局过滤器,path:[{}]", path); // 修改 request
ServerHttpRequest request = exchange.getRequest().mutate().header("x-token", "123456").build(); return chain.filter(exchange.mutate().request(request).build());
} /**
* 指定全局过滤器执行的顺序
*
* @return
*/
@Override
public int getOrder() {
return 0;
}
}

六、编写一个 GatewayFilter

1、实现 GatewayFilterFactory 或者继承 AbstractGatewayFilterFactory<C>
2、继承AbstractGatewayFilterFactory<C> 类时,构造方法中需要调用 super(C.class)
3、重写 shortcutFieldOrder方法,定义配置文件中字段的位置
4、编写的过滤器类必须要以 GatewayFilterFactory结尾
5、自定义过滤器的名字为,GatewayFilterFactory前面的字符,比如 TokenGatewayFilterFactory,那么过滤器名字是 Token
5、功能实现:过滤器实现,从请求中获取一个参数的值,打印出来

1、Java代码实现

@Component
@Slf4j
public class TokenGatewayFilterFactory extends AbstractGatewayFilterFactory<TokenGatewayFilterFactory.Config> { public TokenGatewayFilterFactory() {
super(Config.class);
} @Override
public List<String> shortcutFieldOrder() {
return Lists.newArrayList("tokenName");
} @Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
log.info("每次路由匹配到会执行");
String tokenName = config.getTokenName();
log.info("从配置文件中获取到的 tokenName 的值=[{}].", tokenName);
String value = exchange.getRequest().getQueryParams().getFirst(tokenName);
log.info("从请求中获取到的token value 是:[{}]", value);
return chain.filter(exchange);
};
} @Data
public static class Config {
private String tokenName;
}
}

2、配置文件中的写法

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
filters:
- Token=gateway-token

七、其它的配置

1、获取当前命令的路由

 // 1、获取当前匹配上的路由
Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
String routeId = route.getId();
Map<String, Object> metadata = route.getMetadata();
log.info("routeId:[" + routeId + "]" + "metadata:[" + metadata + "]");

2、过滤器的执行顺序

3、配置超时时间

1、配置全局超时时间(注意单位)

spring:
cloud:
gateway:
httpclient:
# 全局的连接超时,单位毫秒
connect-timeout: 1000
# 全局的响应超时,需要符合java.time.Duration规范
response-timeout: 3s

2、配置每个路由的超时时间(注意单位)

spring:
cloud:
gateway:
routes:
- id: product-provider-01
uri: lb://product-provider
predicates:
- Path=/product/findOne
metadata:
# 单个路由配置响应超时,单位毫秒
response-timeout: 200
# 单个路由配置连接超时,单位毫秒
connect-timeout: 200

4、输出gateway调试日志

1、输出 netty 的 access log

需要使用设置如下参数,注意是 JVM 的参数

-Dreactor.netty.http.server.accessLogEnabled=true

2、设置日志级别 debug or trace

logging:
level:
org.springframework.cloud.gateway: trace
org.springframework.http.server.reactive: trace
org.springframework.web.reactive: trace
org.springframework.boot.autoconfigure.web: trace
reactor.netty: trace

八、网关错误的处理

实现 WebExceptionHandler 接口,在里面出现错误逻辑。需要注意 指定该接口的 order 否则可能被别的处理了,自己的不生效。
返回完成信号:表示错误处理完成
返回错误信息:交由下个处理器处理。
eg:

/**
* 异常处理器,需要注意 @Order 指定执行的顺序
*
* @author huan.fu 2020/11/15 - 14:47
*/
@Component
@Slf4j
@Order(Integer.MIN_VALUE)
public class GatewayExceptionHandler implements WebExceptionHandler { @Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
log.info("发生了异常", ex);
String errorMsg = "{\"code\":500,\"msg\":\"服务器内部错误\"}"; ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBuffer dataBuffer = response.bufferFactory().wrap(errorMsg.getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer));
}
}

九、完整代码

https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/gateway-filter

十、参考链接

https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#gatewayfilter-factories

Spring Cloud Gateway GatewayFilter的使用的更多相关文章

  1. Spring Cloud Gateway 之 AddRequestHeader GatewayFilter Factory

    今天我们来学习下GatewayFilter Factory,中文解释就是过滤器工厂. 官方文档对GatewayFilter Factory的介绍: Route filters allow the mo ...

  2. Spring Cloud Gateway(九):网关过滤器 GatewayFilter

    本文基于 spring cloud gateway 2.0.1 1.简介 GatewayFilter 网关过滤器用于拦截并链式处理web请求,可以实现横切的与应用无关的需求,比如:安全.访问超时的设置 ...

  3. 从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析

    API 网关 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题 ...

  4. 简单尝试Spring Cloud Gateway

    简单尝试Spring Cloud Gateway 简介 Spring Cloud Gateway是一个API网关,它是用于代替Zuul而出现的.Spring Cloud Gateway构建于Sprin ...

  5. spring cloud gateway的stripPrefix配置

    序 本文主要研究下spring cloud gateway的stripPrefix配置 使用zuul的配置 zuul: routes: demo: sensitiveHeaders: Access-C ...

  6. api网关揭秘--spring cloud gateway源码解析

    要想了解spring cloud gateway的源码,要熟悉spring webflux,我的上篇文章介绍了spring webflux. 1.gateway 和zuul对比 I am the au ...

  7. spring cloud gateway - RequestRateLimiter

    1. Official website 5.7 RequestRateLimiter GatewayFilter Factory The RequestRateLimiter GatewayFilte ...

  8. Spring Cloud Gateway服务网关

    原文:https://www.cnblogs.com/ityouknow/p/10141740.html Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gatewa ...

  9. spring cloud gateway之filter篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理,在路由 ...

随机推荐

  1. 快速搭建SSM基本项目

    快速搭建SSM项目基本手脚架 Maven构建项目 一般我们使用Maven来管理我们的项目: 导入相关依赖配置pom.xml: <?xml version="1.0" enco ...

  2. ❤️【Android精进之路-01】定计划,重行动来学Android吧❤️

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. Android精进之路第一篇,确定安卓学习计划. 干货满满,建议收藏,需要用到时常看看.小伙伴们如有问题及需要,欢迎踊跃留言哦~ ~ ~. 前言 ...

  3. Jmeter系列(3)- 常用断言之响应断言

    断言的作用 确定请求是有效还是无效的 添加断言 面板模块介绍 Apply to 作用:指定断言作用范围 Main sample and sub-sample:作用于主main sample和子sub- ...

  4. Jenkins持续集成体系 | 最完整的介绍及资料

    这篇文章是来给大家普及Jenkins知识的, Jenkins能解决什么问题, 有哪些应用场景, 为何要掌握Jenkins, 掌握Jenkins后有哪些好处, 弄懂Jenkins需要掌握哪些知识 不知道 ...

  5. python日志loguru

    文档:https://loguru.readthedocs.io/en/stable/overview.html#installation pip install loguru 使用 基本使用 ##终 ...

  6. 国庆出游神器:魔幻黑科技换天造物,让vlog秒变科幻大片!

    摘要:国庆旅游景点人太多,拍出来的照片全是人人人.车车车,该怎么办?不妨试试这个黑科技,让你的出游vlog秒变科幻大片. 本文分享自华为云社区<国庆出游神器,魔幻黑科技换天造物,让vlog秒变科 ...

  7. Sentry 监控 - Distributed Tracing 分布式跟踪

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  8. 使用 WPF + Chrome 内核实现 在线客服系统 的复合客服端程序

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 免费使用 & 私有化部署免费下载:https://docs.sh ...

  9. 在Vue&Element前端项目中,使用FastReport + pdf.js生成并展示自定义报表

    在我的<FastReport报表随笔>介绍过各种FastReport的报表设计和使用,FastReport报表可以弹性的独立设计格式,并可以在Asp.net网站上.Winform端上使用, ...

  10. HTML模板标签解析

    HTML基本模板 1 <!DOCTYPE html> 2 <html lang="zh-CN"> 3 <head> 4 <meta cha ...