SpringCloud Gateway 路由配置定位原理分析
環(huán)境:springcloud Hoxton.SR11
本節(jié)主要了解系統(tǒng)中的謂詞與配置的路由信息是如何進(jìn)行初始化關(guān)聯(lián)生成路由對(duì)象的。每個(gè)謂詞工廠中的Config對(duì)象又是如何被解析配置的。
所有的謂詞工廠中的Config中屬性值是如何被配置的。
在SpringCloud Gateway中的所有謂詞工廠如下:

命名規(guī)則:XxxRoutePredicateFactory。所有的這些謂詞工廠都是如下的繼承關(guān)系
- public class MethodRoutePredicateFactory extends AbstractRoutePredicateFactory<MethodRoutePredicateFactory.Config>
- //
- public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config>
- // ...
所有的謂詞工廠繼承的
AbstractRoutePredicateFactory中的泛型都是內(nèi)部類的Config。這個(gè)是如何被配置上值的呢?
6.1 gateway自動(dòng)配置
在下面這個(gè)類中配置了所有的Predicate和Filter。
- public class GatewayAutoConfiguration {
- @Bean
- @ConditionalOnEnabledPredicate
- public PathRoutePredicateFactory pathRoutePredicateFactory() {
- return new PathRoutePredicateFactory();
- }
- @Bean
- @ConditionalOnEnabledPredicate
- public QueryRoutePredicateFactory queryRoutePredicateFactory() {
- return new QueryRoutePredicateFactory();
- }
- @Bean
- public RouteLocator routeDefinitionRouteLocator(GatewayProperties properties, List<GatewayFilterFactory> gatewayFilters, List<RoutePredicateFactory> predicates, RouteDefinitionLocator routeDefinitionLocator, ConfigurationService configurationService) {
- return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates,
- gatewayFilters, properties, configurationService);
- }
- @Bean
- @Primary
- @ConditionalOnMissingBean(name = "cachedCompositeRouteLocator")
- public RouteLocator cachedCompositeRouteLocator(List<RouteLocator> routeLocators) {
- return new CachingRouteLocator(new CompositeRouteLocator(Flux.fromIterable(routeLocators)));
- }
- }
這里會(huì)層層委托最終查找查找路由定位會(huì)交給
RouteDefinitionRouteLocator。CachingRouteLocator起到緩存的作用,將配置的所有路由信息保存。
注意:這里的路由信息是在容器啟動(dòng)后就會(huì)被初始化的。
- public class CachingRouteLocator {
- private final RouteLocator delegate;
- private final Flux<Route> routes;
- private final Map<String, List> cache = new ConcurrentHashMap<>();
- private ApplicationEventPublisher applicationEventPublisher;
- 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);
- }
- }
實(shí)例化CachingRouteLocator就開始查找所有配置的Route信息。最終的會(huì)委托給
RouteDefinitionRouteLocator
RouteDefinitionRouteLocator構(gòu)造函數(shù)中的initFactories方法用來(lái)映射路由工廠的XxxRoutePredicateFactory。
- 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);
- });
- }
方法中解析每一個(gè)謂詞工廠對(duì)應(yīng)的名稱然后緩存到predicates 集合中。
factory.name()方法解析謂詞名稱。
- default String name() {
- return NameUtils.normalizeRoutePredicateName(getClass());
- }
CachingRouteLocator是個(gè)緩存路由定位器,是個(gè)首選的RouteLocator(@Primary),這里將
RouteDefinitionRouteLocator進(jìn)行了合并。
6.2 生成路由對(duì)象Route及Config配置
getRoutes---》convertToRoute---》combinePredicates---》lookup。
根據(jù)上面的自動(dòng)配置也知道了在服務(wù)啟動(dòng)時(shí)就進(jìn)行初始化所有路由信息了。
獲取路由信息
- public Flux<Route> getRoutes() {
- Flux<Route> routes = this.routeDefinitionLocator.getRouteDefinitions() .map(this::convertToRoute);
- routes = routes.onErrorContinue((error, obj) -> {
- return routes.map(route -> {
- return route;
- });
- }
合并謂詞
- private AsyncPredicate<ServerWebExchange> combinePredicates(
- RouteDefinition routeDefinition) {
- // other code
- for (PredicateDefinition andPredicate : predicates.subList(1, predicates.size())) {
- AsyncPredicate<ServerWebExchange> found = lookup(routeDefinition, andPredicate);
- predicate = predicate.and(found);
- }
- return predicate;
- }
進(jìn)入lookup中
- 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());
- }
- // 這里將配置中(yml文件)配置的name,args和謂詞工廠中的Config進(jìn)行關(guān)聯(lián)設(shè)置值
- Object config = this.configurationService.with(factory)
- .name(predicate.getName())
- .properties(predicate.getArgs())
- .eventFunction((bound, properties) -> new PredicateArgsEvent(
- RouteDefinitionRouteLocator.this, route.getId(), properties))
- .bind();
- // 最終調(diào)用謂詞工廠(XxxRoutePredicateFactory的apply方法返回RoutePredicate該對(duì)象繼承Predicate)
- return factory.applyAsync(config);
- }
lookup方法中查找,也就是在這里將對(duì)應(yīng)的謂詞Config與RouteDefinition(Predicate)中定義的相對(duì)應(yīng)的屬性關(guān)聯(lián)。
進(jìn)入factory.applyAsync方法
- @FunctionalInterface
- public interface RoutePredicateFactory<C> extends ShortcutConfigurable, Configurable<C> {
- default AsyncPredicate<ServerWebExchange> applyAsync(C config) {
- return toAsyncPredicate(apply(config)); // 查看下面的6.2-1圖當(dāng)前apply所有的實(shí)現(xiàn)就是系統(tǒng)內(nèi)部定義的XxxRoutePredicateFactory
- }
- }
- // apply(config),如這里配置了Path謂詞,那么就會(huì)進(jìn)入PathRoutePredicateFactory中的apply方法
- public Predicate<ServerWebExchange> apply(Config config) {
- // other code
- return new GatewayPredicate() {
- public boolean test() {
- // todo
- }
- }
- }
- // 最后返回一個(gè)異步的謂詞
- public static AsyncPredicate<ServerWebExchange> toAsyncPredicate(Predicate<? super ServerWebExchange> predicate) {
- Assert.notNull(predicate, "predicate must not be null");
- // 這里from就是返回一個(gè)DefaultAsyncPredicate默認(rèn)的異步謂詞
- return AsyncPredicate.from(predicate);
- }
- static AsyncPredicate<ServerWebExchange> from( Predicate<? super ServerWebExchange> predicate) {
- return new DefaultAsyncPredicate<>(GatewayPredicate.wrapIfNeeded(predicate));
- }

最后在combinePredicates方法中將當(dāng)前路由中配置的所有謂詞進(jìn)行了and操作返回。最終回到convertToRoute方法中將當(dāng)前路由中配置的謂詞,過(guò)濾器進(jìn)行了整合包裝返回Route(一個(gè)路由對(duì)象)
- public class Route implements Ordered {
- 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;
- }
這些Route對(duì)象會(huì)被保存在上面說(shuō)的
CachingRouteLocator.routes中。
6.3 定位路由
根據(jù)上面的配置RouteLocator 該類用來(lái)定位路由(查找具體的使用哪個(gè)路由);當(dāng)一個(gè)請(qǐng)求過(guò)來(lái)會(huì)查找是哪個(gè)路由。
RouteLocator中定義了一個(gè)方法
- public interface RouteLocator {
- Flux<Route> getRoutes();
- }
查看這個(gè)getRoutes方法是誰(shuí)調(diào)用的

看到這個(gè)
RoutePredicateHandlerMapping是不是想起了Spring MVC中的HandlerMapping(我們所有的Controller都會(huì)被 RequestMappingHanlderMapping 匹配)。通過(guò)名稱也就知道了該HandlerMapping用來(lái)匹配我們的路由謂詞的誰(shuí)來(lái)處理路由。
接下來(lái)回到前面說(shuō)的
RequestMappingHanlderMapping 對(duì)象,當(dāng)我們請(qǐng)求一個(gè)路由地址時(shí)會(huì)執(zhí)行該類中的lookup方法查找路由
- protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
- // 這里的this.routeLocator就是 CachingRouteLocator對(duì)象
- return this.routeLocator.getRoutes()
- .concatMap(route -> Mono.just(route).filterWhen(r -> {
- exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId());
- // 過(guò)濾查找符合的路由
- return r.getPredicate().apply(exchange);
- }).doOnError(e -> logger.error(
- "Error applying predicate for route: " + route.getId(),
- e)).onErrorResume(e -> Mono.empty()))
- .next()
- .map(route -> {
- if (logger.isDebugEnabled()) {
- logger.debug("Route matched: " + route.getId());
- }
- validateRoute(route, exchange);
- return route;
- });
- }
進(jìn)入r.getPredicate().apply(exchange)
- public interface AsyncPredicate<T> extends Function<T, Publisher<Boolean>> {
- static AsyncPredicate<ServerWebExchange> from(Predicate<? super ServerWebExchange> predicate) {
- return new DefaultAsyncPredicate<>(GatewayPredicate.wrapIfNeeded(predicate));
- }
- class DefaultAsyncPredicate<T> implements AsyncPredicate<T> {
- private final Predicate<T> delegate;
- public DefaultAsyncPredicate(Predicate<T> delegate) {
- this.delegate = delegate;
- }
- @Override
- public Publisher<Boolean> apply(T t) {
- return Mono.just(delegate.test(t));
- }
- @Override
- public String toString() {
- return this.delegate.toString();
- }
- }
- }
這里會(huì)調(diào)用Predicate.test方法(XxxRoutePredicateFactory中的apply方法返回的GatewayPredicate)。
調(diào)用GatewayPredicate.test返回判斷當(dāng)前請(qǐng)求的路由是否匹配。
整體的一個(gè)流程:
1、系統(tǒng)先初始化所有的Predicate(謂詞)和Filter(過(guò)濾器)
2、根據(jù)配置的路由信息(過(guò)濾器,謂詞)包裝返回Route對(duì)象
3、根據(jù)請(qǐng)求路由路徑查找匹配的路由