Spring Cloud Gateway过滤器Filter源码分析

Gateway常用过滤器

GatewayFilter:

直接生成过滤器与路由绑定

public class MyGatewayFilter implements GatewayFilter, Ordered {
    
     @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        
    }
    
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
     return builder.routes().route(r ->
             r.path("/aa")
                     //转发路由
                     .uri("http://localhost:8080/provider/test")
                     //注册自定义过滤器
                     .filters(new MyGatewayFilter())
                    //给定id
                    .id("user-service"))
                    .build();
}

过滤器工厂生成过滤器

@Bean
public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthorizeGatewayFilterFactory.Config>{
 public AuthorizeGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("enabled");
    }

    @Override
    public GatewayFilter apply(AuthorizeGatewayFilterFactory.Config config) {
     	return new MyGatewayFilter();
    }
}

GlobalFilter:

@Component
public class MyGloablFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return 0;
    }
}

Gateway过滤器加载流程

在这里插入图片描述

Gateway入口GatewayAutoConfiguration

该类加载了各种需要的容器

如:转化工具

@Bean
	public StringToZonedDateTimeConverter stringToZonedDateTimeConverter() {
		return new StringToZonedDateTimeConverter();
	}
//...........

内置过滤器工厂(仅举4例)

@Bean
	@ConditionalOnEnabledFilter
	public StripPrefixGatewayFilterFactory stripPrefixGatewayFilterFactory() {
		return new StripPrefixGatewayFilterFactory();
	}

	@Bean
	@ConditionalOnEnabledFilter
	public RequestHeaderToRequestUriGatewayFilterFactory requestHeaderToRequestUriGatewayFilterFactory() {
		return new RequestHeaderToRequestUriGatewayFilterFactory();
	}

	@Bean
	@ConditionalOnEnabledFilter
	public RequestSizeGatewayFilterFactory requestSizeGatewayFilterFactory() {
		return new RequestSizeGatewayFilterFactory();
	}

	@Bean
	@ConditionalOnEnabledFilter
	public RequestHeaderSizeGatewayFilterFactory requestHeaderSizeGatewayFilterFactory() {
		return new RequestHeaderSizeGatewayFilterFactory();
	}

内置过滤器等

@Bean
	@ConditionalOnEnabledGlobalFilter
	public RemoveCachedBodyFilter removeCachedBodyFilter() {
		return new RemoveCachedBodyFilter();
	}

	@Bean
	@ConditionalOnEnabledGlobalFilter
	public RouteToRequestUrlFilter routeToRequestUrlFilter() {
		return new RouteToRequestUrlFilter();
	}

	@Bean
	@ConditionalOnEnabledGlobalFilter
	public ForwardRoutingFilter forwardRoutingFilter(ObjectProvider<DispatcherHandler> dispatcherHandler) {
		return new ForwardRoutingFilter(dispatcherHandler);
	}

	@Bean
	@ConditionalOnEnabledGlobalFilter
	public ForwardPathFilter forwardPathFilter() {
		return new ForwardPathFilter();
	}

routeDefinitionLocator类

	@Bean
	@Primary
	public RouteDefinitionLocator routeDefinitionLocator(List<RouteDefinitionLocator> routeDefinitionLocators) {
		return new CompositeRouteDefinitionLocator(Flux.fromIterable(routeDefinitionLocators));
	}

	@Bean
	@ConditionalOnMissingBean
	public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator(GatewayProperties properties) {
		return new PropertiesRouteDefinitionLocator(properties);

GatewayProperties类

其中与加载有关的重要类有GatewayProperties加载配置文件中的配置信息主要是routes信息

@Bean
public GatewayProperties gatewayProperties() {
   return new GatewayProperties();
}

public class GatewayProperties {

	/**
	 * Properties prefix.
	 */
	public static final String PREFIX = "spring.cloud.gateway";

	private final Log logger = LogFactory.getLog(getClass());

	/**
	 * List of Routes.
	 */
	@NotNull
	@Valid
	private List<RouteDefinition> routes = new ArrayList<>();
	/**
	 * List of filter definitions that are applied to every route.
	 */
	private List<FilterDefinition> defaultFilters = new ArrayList<>();

路由信息

RouteDefinition类—>Route类的定义信息(GatewayProperties从配置文件获取封装到RouteDefinition中)

private String id;

	@NotEmpty
	@Valid
	private List<PredicateDefinition> predicates = new ArrayList<>();

	@Valid
	private List<FilterDefinition> filters = new ArrayList<>();

	@NotNull
	private URI uri;

	private Map<String, Object> metadata = new HashMap<>();

	private int order = 0;

Route类

private final String id;

private final URI uri;

private final int order;

private final AsyncPredicate<ServerWebExchange> predicate;

private final List<GatewayFilter> gatewayFilters;

private final Map<String, Object> metadata;

CachingRouteLocator路由定位

 public RouteLocator cachedCompositeRouteLocator(List<RouteLocator> routeLocators) {        
     return new CachingRouteLocator(new CompositeRouteLocator(Flux.fromIterable(routeLocators)));
 }

初始化

private final RouteLocator delegate;

private final Flux<Route> routes;

private final Map<String, List> cache = new ConcurrentHashMap<>();

public CachingRouteLocator(RouteLocator delegate) {
	this.delegate = delegate;
	routes = CacheFlux.lookup(cache, CACHE_KEY, Route.class).onCacheMissResume(this::fetch);
}
private Flux<Route> fetch() {
	return this.delegate.getRoutes().sort(AnnotationAwareOrderComparator.INSTANCE);
}

调用CompositeRouteLocator的getRoutes方法中获得Route并保存到routes集合中

CompositeRouteLocator类

public class CompositeRouteLocator implements RouteLocator {

	private final Flux<RouteLocator> delegates;

	public CompositeRouteLocator(Flux<RouteLocator> delegates) {
		this.delegates = delegates;
	}

	@Override
	public Flux<Route> getRoutes() {
		return this.delegates.flatMapSequential(RouteLocator::getRoutes);
	}

}

getRoutes方法调用所有RouteLocator的子类对象拿到对应加载的Route信息

RouteDefinitionRouteLocator类加载配置问价配置的Route

@Bean
public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties,
      List<GatewayFilterFactory> gatewayFilters, List<RoutePredicateFactory> predicates,
      RouteDefinitionLocator routeDefinitionLocator, ConfigurationService configurationService) {
   return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates, gatewayFilters, properties,
         configurationService);
}

List gatewayFilters存放了容器中所有实现了GatewayFilterFactory的工厂类

初始化

public class RouteDefinitionRouteLocator implements RouteLocator {

	/**
	 * Default filters name.
	 */
	public static final String DEFAULT_FILTERS = "defaultFilters";

	protected final Log logger = LogFactory.getLog(getClass());

	private final RouteDefinitionLocator routeDefinitionLocator;

	private final ConfigurationService configurationService;

	private final Map<String, RoutePredicateFactory> predicates = new LinkedHashMap<>();

	private final Map<String, GatewayFilterFactory> gatewayFilterFactories = new HashMap<>();

	private final GatewayProperties gatewayProperties;

	public RouteDefinitionRouteLocator(RouteDefinitionLocator routeDefinitionLocator,
			List<RoutePredicateFactory> predicates, List<GatewayFilterFactory> gatewayFilterFactories,
			GatewayProperties gatewayProperties, ConfigurationService configurationService) {
		this.routeDefinitionLocator = routeDefinitionLocator;
		this.configurationService = configurationService;
		initFactories(predicates);
		gatewayFilterFactories.forEach(factory -> this.gatewayFilterFactories.put(factory.name(), factory));
		this.gatewayProperties = gatewayProperties;
	}
private void initFactories(List<RoutePredicateFactory> predicates) {
		predicates.forEach(factory -> {
			String key = factory.name();
			if (this.predicates.containsKey(key)) {
				this.logger.warn("A RoutePredicateFactory named " + key + " already exists, class: "
						+ this.predicates.get(key) + ". It will be overwritten.");
			}
			this.predicates.put(key, factory);
			if (logger.isInfoEnabled()) {
				logger.info("Loaded RoutePredicateFactory [" + key + "]");
			}
		});
	}

将RoutePredicateFactory和GatewayFilterFactory转移到Map<String, RoutePredicateFactory> 中,名字作为key值。

getFilters方法

获取Filter集合

private List<GatewayFilter> getFilters(RouteDefinition routeDefinition) {
		List<GatewayFilter> filters = new ArrayList<>();

		// TODO: support option to apply defaults after route specific filters?
		if (!this.gatewayProperties.getDefaultFilters().isEmpty()) {
			filters.addAll(loadGatewayFilters(routeDefinition.getId(),
					new ArrayList<>(this.gatewayProperties.getDefaultFilters())));
		}

		if (!routeDefinition.getFilters().isEmpty()) {
			filters.addAll(loadGatewayFilters(routeDefinition.getId(), new ArrayList<>(routeDefinition.getFilters())));
		}

		AnnotationAwareOrderComparator.sort(filters);
		return filters;
	}

获取配置文件中spring.cloud.gateway.defaultFilters和spring.cloud.gateway.routes的元信息加载过滤器

loadGatewayFilters()方法

具体装载filter的方法

List<GatewayFilter> loadGatewayFilters(String id, List<FilterDefinition> filterDefinitions) {
   ArrayList<GatewayFilter> ordered = new ArrayList<>(filterDefinitions.size());
   for (int i = 0; i < filterDefinitions.size(); i++) {
      FilterDefinition definition = filterDefinitions.get(i);
      GatewayFilterFactory factory = this.gatewayFilterFactories.get(definition.getName());
      if (factory == null) {
         throw new IllegalArgumentException(
               "Unable to find GatewayFilterFactory with name " + definition.getName());
      }
      if (logger.isDebugEnabled()) {
         logger.debug("RouteDefinition " + id + " applying filter " + definition.getArgs() + " to "
               + definition.getName());
      }

      // @formatter:off
      Object configuration = this.configurationService.with(factory)
            .name(definition.getName())
            .properties(definition.getArgs())
            .eventFunction((bound, properties) -> new FilterArgsEvent(
                  // TODO: why explicit cast needed or java compile fails
                  RouteDefinitionRouteLocator.this, id, (Map<String, Object>) properties))
            .bind();
      // @formatter:on

      // some filters require routeId
      // TODO: is there a better place to apply this?
      if (configuration instanceof HasRouteId) {
         HasRouteId hasRouteId = (HasRouteId) configuration;
         hasRouteId.setRouteId(id);
      }

      GatewayFilter gatewayFilter = factory.apply(configuration);
      if (gatewayFilter instanceof Ordered) {
         ordered.add(gatewayFilter);
      }
      else {
         ordered.add(new OrderedGatewayFilter(gatewayFilter, i + 1));
      }
   }

   return ordered;
}

configurationService方法生成过滤器工厂实例化过滤器的配置类configuration;通过definition中的名字找到对应的过滤器工厂调用工厂的factory.apply()方法生成过滤器

combinePredicates()方法

private AsyncPredicate<ServerWebExchange> combinePredicates(RouteDefinition routeDefinition) {
   List<PredicateDefinition> predicates = routeDefinition.getPredicates();
   if (predicates == null || predicates.isEmpty()) {
      // this is a very rare case, but possible, just match all
      return AsyncPredicate.from(exchange -> true);
   }
   AsyncPredicate<ServerWebExchange> predicate = lookup(routeDefinition, predicates.get(0));

   for (PredicateDefinition andPredicate : predicates.subList(1, predicates.size())) {
      AsyncPredicate<ServerWebExchange> found = lookup(routeDefinition, andPredicate);
      predicate = predicate.and(found);
   }

   return predicate;
}

@SuppressWarnings("unchecked")
private AsyncPredicate<ServerWebExchange> lookup(RouteDefinition route, PredicateDefinition predicate) {
   RoutePredicateFactory<Object> factory = this.predicates.get(predicate.getName());
   if (factory == null) {
      throw new IllegalArgumentException("Unable to find RoutePredicateFactory with name " + predicate.getName());
   }
   if (logger.isDebugEnabled()) {
      logger.debug("RouteDefinition " + route.getId() + " applying " + predicate.getArgs() + " to "
            + predicate.getName());
   }

   // @formatter:off
   Object config = this.configurationService.with(factory)
         .name(predicate.getName())
         .properties(predicate.getArgs())
         .eventFunction((bound, properties) -> new PredicateArgsEvent(
               RouteDefinitionRouteLocator.this, route.getId(), properties))
         .bind();
   // @formatter:on

   return factory.applyAsync(config);
}

通过RouteDefinition生成Predicates谓词;在一个资源中可能有多个谓词,combinePredicates()方法中用第一个谓词与之后的所以谓词进行和操作连接起来

getRoutes()方法

public Flux<Route> getRoutes() {
   Flux<Route> routes = this.routeDefinitionLocator.getRouteDefinitions().map(this::convertToRoute);

   if (!gatewayProperties.isFailOnRouteDefinitionError()) {
      // instead of letting error bubble up, continue
      routes = routes.onErrorContinue((error, obj) -> {
         if (logger.isWarnEnabled()) {
            logger.warn("RouteDefinition id " + ((RouteDefinition) obj).getId()
                  + " will be ignored. Definition has invalid configs, " + error.getMessage());
         }
      });
   }

   return routes.map(route -> {
      if (logger.isDebugEnabled()) {
         logger.debug("RouteDefinition matched: " + route.getId());
      }
      return route;
   });
}

private Route convertToRoute(RouteDefinition routeDefinition) {
   AsyncPredicate<ServerWebExchange> predicate = combinePredicates(routeDefinition);
   List<GatewayFilter> gatewayFilters = getFilters(routeDefinition);

   return Route.async(routeDefinition).asyncPredicate(predicate).replaceFilters(gatewayFilters).build();
}

getRoutes()方法中调用getFilters()和combinePredicates()方法获得GatewayFilter集合与和操作后的predicate封装到Route中返回Route集合

FilteringWebHandler启动和过滤器链

@Bean
public FilteringWebHandler filteringWebHandler(List<GlobalFilter> globalFilters) {
   return new FilteringWebHandler(globalFilters);
}

初始化

private final List<GatewayFilter> globalFilters;

public FilteringWebHandler(List<GlobalFilter> globalFilters) {
   this.globalFilters = loadFilters(globalFilters);
}

private static List<GatewayFilter> loadFilters(List<GlobalFilter> filters) {
   return filters.stream().map(filter -> {
      GatewayFilterAdapter gatewayFilter = new GatewayFilterAdapter(filter);
      if (filter instanceof Ordered) {
         int order = ((Ordered) filter).getOrder();
         return new OrderedGatewayFilter(gatewayFilter, order);
      }
      return gatewayFilter;
   }).collect(Collectors.toList());
}

初始化将传入的GlobalFilter集合转化GatewayFilter集合

handle()方法

整理filter调用过滤器链

public Mono<Void> handle(ServerWebExchange exchange) {
   Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
   List<GatewayFilter> gatewayFilters = route.getFilters();

   List<GatewayFilter> combined = new ArrayList<>(this.globalFilters);
   combined.addAll(gatewayFilters);
   // TODO: needed or cached?
   AnnotationAwareOrderComparator.sort(combined);

   if (logger.isDebugEnabled()) {
      logger.debug("Sorted gatewayFilterFactories: " + combined);
   }

   return new DefaultGatewayFilterChain(combined).filter(exchange);
}

将整理好的GatewayFilter集合和GlobalFilter集合合并实例化过滤器链开始调用过滤器链

DefaultGatewayFilterChain

DefaultGatewayFilterChain是FilteringWebHandler的一个内部类

private static class DefaultGatewayFilterChain implements GatewayFilterChain {

		private final int index;

		private final List<GatewayFilter> filters;

		DefaultGatewayFilterChain(List<GatewayFilter> filters) {
			this.filters = filters;
			this.index = 0;
		}

		private DefaultGatewayFilterChain(DefaultGatewayFilterChain parent, int index) {
			this.filters = parent.getFilters();
			this.index = index;
		}

		public List<GatewayFilter> getFilters() {
			return filters;
		}

		@Override
		public Mono<Void> filter(ServerWebExchange exchange) {
			return Mono.defer(() -> {
				if (this.index < filters.size()) {
					GatewayFilter filter = filters.get(this.index);
					DefaultGatewayFilterChain chain = new DefaultGatewayFilterChain(this, this.index + 1);
					return filter.filter(exchange, chain);
				}
				else {
					return Mono.empty(); // complete
				}
			});
		}

	}

DefaultGatewayFilterChain类有两个初始化方法第一个方法是外部调用(List filters)传入filter集合用于第一次实例化过滤器链其中index为0;第二个初始化方法(DefaultGatewayFilterChain parent, int index)传入DefaultGatewayFilterChain 类来获取filter集合再通过index来获取所需的filter

filter(ServerWebExchange exchange)方法执行对应的过滤器并传入下一个过滤器信息

Gateway执行流程

在这里插入图片描述

DispatcherHandler执行入口

类似与SpringMVC的DispatcherServlet

初始化

	public DispatcherHandler(ApplicationContext applicationContext) {
		initStrategies(applicationContext);
	}

protected void initStrategies(ApplicationContext context) {
   Map<String, HandlerMapping> mappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
         context, HandlerMapping.class, true, false);

   ArrayList<HandlerMapping> mappings = new ArrayList<>(mappingBeans.values());
   AnnotationAwareOrderComparator.sort(mappings);
   this.handlerMappings = Collections.unmodifiableList(mappings);

   Map<String, HandlerAdapter> adapterBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
         context, HandlerAdapter.class, true, false);

   this.handlerAdapters = new ArrayList<>(adapterBeans.values());
   AnnotationAwareOrderComparator.sort(this.handlerAdapters);

   Map<String, HandlerResultHandler> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
         context, HandlerResultHandler.class, true, false);

   this.resultHandlers = new ArrayList<>(beans.values());
   AnnotationAwareOrderComparator.sort(this.resultHandlers);
}

将实现了接口的实例化对象加载到对应的集合中handlerMappings,HandlerAdapters,resultHandlers

handle方法

public Mono<Void> handle(ServerWebExchange exchange) {
		if (this.handlerMappings == null) {
			return createNotFoundError();
		}
		if (CorsUtils.isPreFlightRequest(exchange.getRequest())) {
			return handlePreFlight(exchange);
		}
		return Flux.fromIterable(this.handlerMappings)
				.concatMap(mapping -> mapping.getHandler(exchange))
				.next()
				.switchIfEmpty(createNotFoundError())
				.flatMap(handler -> invokeHandler(exchange, handler))
				.flatMap(result -> handleResult(exchange, result));
	}

concatMap(mapping -> mapping.getHandler(exchange))调用handlerMappings中的每一个handlerMapping执行getHandle方法(父类AbstractHandlerMapping的方法)拿到FilteringWebHandler处理器执行FilteringWebHandler的handle方法,拿到过滤结果响应结果

getHandle()方法

public Mono<Object> getHandler(ServerWebExchange exchange) {
   return getHandlerInternal(exchange).map(handler -> {
      if (logger.isDebugEnabled()) {
         logger.debug(exchange.getLogPrefix() + "Mapped to " + handler);
      }
      ServerHttpRequest request = exchange.getRequest();
      if (hasCorsConfigurationSource(handler) || CorsUtils.isPreFlightRequest(request)) {
         CorsConfiguration config = (this.corsConfigurationSource != null ?
               this.corsConfigurationSource.getCorsConfiguration(exchange) : null);
         CorsConfiguration handlerConfig = getCorsConfiguration(handler, exchange);
         config = (config != null ? config.combine(handlerConfig) : handlerConfig);
         if (config != null) {
            config.validateAllowCredentials();
         }
         if (!this.corsProcessor.process(config, exchange) || CorsUtils.isPreFlightRequest(request)) {
            return NO_OP_HANDLER;
         }
      }
      return handler;
   });
}

调用子类实现getHandlerInternal方法

getHandlerInternal()方法

主要是RoutePredicateHandlerMapping的实现类

protected Mono<?> getHandlerInternal(ServerWebExchange exchange) {
   // don't handle requests on management port if set and different than server port
   if (this.managementPortType == DIFFERENT && this.managementPort != null
         && exchange.getRequest().getURI().getPort() == this.managementPort) {
      return Mono.empty();
   }
   exchange.getAttributes().put(GATEWAY_HANDLER_MAPPER_ATTR, getSimpleName());

   return lookupRoute(exchange)
         // .log("route-predicate-handler-mapping", Level.FINER) //name this
         .flatMap((Function<Route, Mono<?>>) r -> {
            exchange.getAttributes().remove(GATEWAY_PREDICATE_ROUTE_ATTR);
            if (logger.isDebugEnabled()) {
               logger.debug("Mapping [" + getExchangeDesc(exchange) + "] to " + r);
            }

            exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, r);
            return Mono.just(webHandler);
         }).switchIfEmpty(Mono.empty().then(Mono.fromRunnable(() -> {
            exchange.getAttributes().remove(GATEWAY_PREDICATE_ROUTE_ATTR);
            if (logger.isTraceEnabled()) {
               logger.trace("No RouteDefinition found for [" + getExchangeDesc(exchange) + "]");
            }
         })));
}

拿到谓词匹配成功后的route将route存放在exchange.getAttributes().remove(GATEWAY_PREDICATE_ROUTE_ATTR)中

FilteringWebHandler的handle方法从这里获取route值 Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);最后返回FilteringWebHandler处理器

lookupRoute()方法

protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
   return this.routeLocator.getRoutes()
         // individually filter routes so that filterWhen error delaying is not a
         // problem
         .concatMap(route -> Mono.just(route).filterWhen(r -> {
            // add the current route we are testing
            exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId());
            return r.getPredicate().apply(exchange);
         })
               // instead of immediately stopping main flux due to error, log and
               // swallow it
               .doOnError(e -> logger.error("Error applying predicate for route: " + route.getId(), e))
               .onErrorResume(e -> Mono.empty()))
         // .defaultIfEmpty() put a static Route not found
         // or .switchIfEmpty()
         // .switchIfEmpty(Mono.<Route>empty().log("noroute"))
         .next()
         // TODO: error handling
         .map(route -> {
            if (logger.isDebugEnabled()) {
               logger.debug("Route matched: " + route.getId());
            }
            validateRoute(route, exchange);
            return route;
         });
}

routeLocator接口getRoutes()方法是调用 CachedCompositeRouteLocator类中的getRoutes()(由CachedCompositeRouteLocator初始化时,调用CompositeRouteLocator的getRoute()方法拿到所有的实现RouterLocator的对象生成的route)方法返回所有的配置route;拿到每一个route进行谓词匹配,匹配成功返回route只允许匹配一个route

invokeHandler()方法

private Mono<HandlerResult> invokeHandler(ServerWebExchange exchange, Object handler) {
   if (ObjectUtils.nullSafeEquals(exchange.getResponse().getStatusCode(), HttpStatus.FORBIDDEN)) {
      return Mono.empty();  // CORS rejection
   }
   if (this.handlerAdapters != null) {
      for (HandlerAdapter handlerAdapter : this.handlerAdapters) {
         if (handlerAdapter.supports(handler)) {
            return handlerAdapter.handle(exchange, handler);
         }
      }
   }
   return Mono.error(new IllegalStateException("No HandlerAdapter: " + handler));
}

拿到FilteringWebHandler的适配器调用FilteringWebHandler的handle()方法

handleResult()方法

private Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
   return getResultHandler(result).handleResult(exchange, result)
         .checkpoint("Handler " + result.getHandler() + " [DispatcherHandler]")
         .onErrorResume(ex ->
               result.applyExceptionHandler(ex).flatMap(exResult -> {
                  String text = "Exception handler " + exResult.getHandler() +
                        ", error=\"" + ex.getMessage() + "\" [DispatcherHandler]";
                  return getResultHandler(exResult).handleResult(exchange, exResult).checkpoint(text);
               }));
}

private HandlerResultHandler getResultHandler(HandlerResult handlerResult) {
   if (this.resultHandlers != null) {
      for (HandlerResultHandler resultHandler : this.resultHandlers) {
         if (resultHandler.supports(handlerResult)) {
            return resultHandler;
         }
      }
   }
   throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue());
}

拿到过滤结果匹配结果处理器去匹配结果将结果交给所得到的匹配到的结果处理器去处理


//获得接口的所有实例
BeanFactoryUtils.beansOfTypeIncludingAncestors(
      context, HandlerMapping.class, true, false);
//只读不可修改集合
Collections.unmodifiableList()

warning!
1.GatewayFilter失效,从上到下第一个Route谓词匹配成功就不会匹配之后的

​2.排序顺序,order越小越先执行,order相同先执行GlobalFilter,过滤器类型相同按名字排序

​3.GatewayFilter采用配置一定要指明filter(xxxxxxGatewayFilter)只用填写xxxxxx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snack-counter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值