参考:https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_global_filters
Combined Global Filter and GatewayFilter Ordering | 对过滤器执行顺序进行排序 |
Forward Routing Filter | 用于本地forward,也就是将请求在Gateway服务内进行转发,而不是转发到下游服务 |
LoadBalancerClient Filter | 整合Ribbon实现负载均衡 |
Netty Routing Filter | 使用Netty的 HttpClient 转发http、https请求 |
Netty Write Response Filter | 将代理响应写回网关的客户端侧 |
RouteToRequestUrl Filter | 将从request里获取的原始url转换成Gateway进行请求转发时所使用的url |
Websocket Routing Filter | 使用Spring Web Socket将转发 Websocket 请求 |
Gateway Metrics Filter | 整合监控相关,提供监控指标 |
Marking An Exchange As Routed | 防止重复的路由转发 |
Combined Global Filter and GatewayFilter Ordering
示例代码:
@Bean
@Order(-1)
public GlobalFilter a() {
return (exchange, chain) -> {
log.info("first pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("third post filter");
}));
};
}
@Bean
@Order(0)
public GlobalFilter b() {
return (exchange, chain) -> {
log.info("second pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("second post filter");
}));
};
}
@Bean
@Order(1)
public GlobalFilter c() {
return (exchange, chain) -> {
log.info("third pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("first post filter");
}));
};
}
返回结果:
first pre filter
second pre filter
third pre filter
first post filter
second post filter
third post filter
Forward Routing Filter
PS:所谓 url scheme 简单来说就是 url 中的协议部分,例如http、https、ws等。自定义的 scheme 通常用于标识该url的行为,例如app开发中通常使用url scheme来跳转页面
LoadBalancerClient Filter
这个Filter是用来整合Ribbon的,其核心就是解析 scheme 为lb的 url,以此获取微服务的名称,然后再通过Ribbon获取实际的调用地址。
示例配置:
spring:
cloud:
gateway:
routes:
- id: myRoute
uri: lb://service
predicates:
- Path=/service/**
Netty Routing Filter
Netty Write Response Filter
RouteToRequestUrl Filter
Websocket Routing Filter
示例配置:
spring:
cloud:
gateway:
routes:
# SockJS route
- id: websocket_sockjs_route
uri: http://localhost:3001
predicates:
- Path=/websocket/info/**
# Normwal Websocket route
- id: websocket_route
uri: ws://localhost:3001
predicates:
- Path=/websocket/**
Gateway Metrics Filter
PS:Prometheus是一款监控工具,Grafana是一款监控可视化工具;Spring Boot Actuator可与这两款工具进行整合。