版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yingziisme/article/details/90357292
filter与自定义filter
过滤器可以在路由请求之前对请求进行处理,也可以在请求响应之后对响应进行处理
在请求路由之前可以做比如参数校验,鉴权,日志记录,协议转换,请求参数修改,路径修改等
在请求响应之后可以做比如记录响应消息,修改响应,修改响应头等
Filter分为Gateway Filter和Global Filter
Gateway Filter 是可以指定对某个路由进行过滤操作,自带的Gateway Filter如下
AddRequestHeader | 作用 |
---|---|
AddRequestHeader | 添加请求header |
AddRequestParameter | 添加请求参数 |
AddResponseHeader | 添加响应header |
DedupeResponseHeader | 响应头去重 |
Hystrix GatewayFilters | 断路器的网关 |
PrefixPath | 路径匹配 |
PreserveHostHeader | 发送原始host |
RequestRateLimiter | 限流 |
RedirectTo | 重定向 |
RemoveHopByHopHeadersFilter | 移除响应header,默认有Connection,Keep-Alive,Proxy-Authenticate,Proxy-Authorization,TE,Trailer,Transfer-Encoding,Upgrade |
RemoveRequestHeader | 移除请求header |
RemoveResponseHeader | 移除响应header |
RewritePath | 路径重写 |
RewriteResponseHeader | 重写响应header |
SaveSession | 保存session |
SetPath | 模板匹配路径修改 |
SetResponseHeader | 设置响应header,会覆盖所有原有的header |
SetStatus | 设置httpStatus响应码 |
StripPrefix | 去掉前缀 |
Retry | 重试次数 |
RequestSize | 请求大小限制 |
ModifyRequestBody | 修改请求体 |
ModifyResponseBody | 修改响应体 |
routes:
- id: spring-cloud-client-demo
url: lb://spring-cloud-client-demo
predicates:
- Path=/client/**
filters:
- AddRequestHeader=X-Request-Foo, Bar
- StripPrefix=1
使用Postman发送请求
GET http://localhost:10001/client/demo/get?param=aaa
可以看到请求被转发到spring-cloud-client-demo的/demo/get?param=aaa
同时查看请求头打印,可以看到添加的请求头
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: accept, */*
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: postman-token, d90bc432-9861-4170-8c21-a66be561446f
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: accept-encoding, gzip, deflate
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: cache-control, no-cache
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: x-request-foo, Bar
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: user-agent, PostmanRuntime/7.4.0
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: forwarded, proto=http;host="localhost:10001";for="0:0:0:0:0:0:0:1:62526"
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: x-forwarded-for, 0:0:0:0:0:0:0:1
2019-05-18 11:03:17.501 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: x-forwarded-proto, http
2019-05-18 11:03:17.502 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: x-forwarded-prefix, /client
2019-05-18 11:03:17.502 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: x-forwarded-port, 10001
2019-05-18 11:03:17.502 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: x-forwarded-host, localhost:10001
2019-05-18 11:03:17.502 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: host, 192.168.56.1:8801
2019-05-18 11:03:17.502 INFO 13612 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : header: content-length, 0
Global Filter是全局的过滤器,配置之后对所有路由都有效
过滤器 | 说明 |
---|---|
LoadBalancerClientFilter | 负载均衡过滤器 |
Netty Routing Filter | 默认使用netty的底层 |
RouteToRequestUrlFilter | 新的请求路由 |
Websocket Routing Filter | websocket路由 |
Gateway Metrics Filter | 路由监控,配合spring-boot-starter-actuator |
微服务使用·负载均衡用
uri: lb://spring-cloud-client-demo
websocket
uri: lb:ws://spring-cloud-client-demo
直接路由
uri:http:/xxxx.com
@Slf4j
@Component
public class MyFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("this is a pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("this is a post filter");
}));
}
@Override
public int getOrder() {
return -1;
}
}
注:
再写一个filter测试一下
@Slf4j
@Component
public class MyFilter2 implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("this is a pre filter2");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("this is a post filter2");
}));
}
@Override
public int getOrder() {
return -2;
}
}
执行一个请求查看日志信息
2019-05-18 12:17:22.874 INFO 12996 --- [ctor-http-nio-3] com.mt.demo.gateway.filter.MyFilter2 : this is a pre filter2
2019-05-18 12:17:22.874 INFO 12996 --- [ctor-http-nio-3] com.mt.demo.gateway.filter.MyFilter : this is a pre filter
2019-05-18 12:17:23.191 INFO 12996 --- [ctor-http-nio-3] com.mt.demo.gateway.filter.MyFilter : this is a post filter
2019-05-18 12:17:23.191 INFO 12996 --- [ctor-http-nio-3] com.mt.demo.gateway.filter.MyFilter2 : this is a post filter2