SpringCloud Gateway源码中的适配器模式

SpringCloud gateway中有两种过滤器,路由过滤器(接口为GatewayFilter)和全局过滤器(接口为GlobalFilter;

路由过滤器GatewayFilter的定义:

 1 public interface GatewayFilter {
 2 
 3     /**
 4      * Process the Web request and (optionally) delegate to the next
 5      * {@code WebFilter} through the given {@link GatewayFilterChain}.
 6      * @param exchange the current server exchange
 7      * @param chain provides a way to delegate to the next filter
 8      * @return {@code Mono<Void>} to indicate when request processing is complete
 9      */
10     Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
11 
12 } 

全局过滤器GlobalFilter的定义:

 1 public interface GlobalFilter {
 2 
 3     /**
 4      * Process the Web request and (optionally) delegate to the next
 5      * {@code WebFilter} through the given {@link GatewayFilterChain}.
 6      * @param exchange the current server exchange
 7      * @param chain provides a way to delegate to the next filter
 8      * @return {@code Mono<Void>} to indicate when request processing is complete
 9      */
10     Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
11 
12 }

FilteringWebHandler中会对所有的路由过滤器和全局过滤器进行调用,封装了一个DefaultGatewayFilterChain类,在这个类中统一对路由过滤器和全局过滤器进行调用,但是他们分属不同的接口,这里采用了适配器模式,适配接口GatewayFilterAdapter完成了将GlobalFilter接口适配成GatewayFilter

 1 private static class GatewayFilterAdapter implements GatewayFilter {
 2 
 3     private final GlobalFilter delegate;
 4     
 5     public GatewayFilterAdapter(GlobalFilter delegate) {
 6         this.delegate = delegate;
 7     }
 8     
 9     @Override
10     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
11         return this.delegate.filter(exchange, chain);
12     }
13 }  

这样在FilteringWebHandler中就可以将这两种过滤器存放到一个List中:

 1 public class FilteringWebHandler implements WebHandler {
 2     /**
 3      * 全局过滤器
 4      */
 5     private final List<GatewayFilter> globalFilters;
 6 
 7     public FilteringWebHandler(List<GlobalFilter> globalFilters) {
 8         this.globalFilters = loadFilters(globalFilters);
 9     }
10 
11     private static List<GatewayFilter> loadFilters(List<GlobalFilter> filters) {
12         return filters.stream()
13                 .map(filter -> {
14                     GatewayFilterAdapter gatewayFilter = new GatewayFilterAdapter(filter);
15                     if (filter instanceof Ordered) {
16                         int order = ((Ordered) filter).getOrder();
17                         return new OrderedGatewayFilter(gatewayFilter, order);
18                     }
19                     return gatewayFilter;
20                 }).collect(Collectors.toList());
21     }
22 }

DefaultGatewayFilterChain.filter方法中就可以用同样的方式调用:

 1 private static class DefaultGatewayFilterChain implements GatewayFilterChain {
 2     private int index;
 3     private final List<GatewayFilter> filters;
 4 
 5     public DefaultGatewayFilterChain(List<GatewayFilter> filters) {
 6         this.filters = filters;
 7     }
 8 
 9     @Override
10     public Mono<Void> filter(ServerWebExchange exchange) {
11         if (this.index < filters.size()) {
12             GatewayFilter filter = filters.get(this.index++);
13             return filter.filter(exchange, this);
14         } else {
15             return Mono.empty(); // complete
16         }
17     }
18 } 

posted on 2022-12-30 11:38  小夏coding  阅读(76)  评论(0编辑  收藏  举报

导航