成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringCloud Gateway 路由如何定位從底層源碼分析

開發 前端
所有的謂詞工廠繼承的AbstractRoutePredicateFactory中的泛型都是內部類的Config。這個是如何被配置上值的呢?

環境:springcloud Hoxton.SR11

本節主要了解系統中的謂詞與配置的路由信息是如何進行初始化關聯生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的。

所有的謂詞工廠中的Config中屬性值是如何被配置的。

在SpringCloud Gateway中的所有謂詞工廠如下:

圖片圖片

命名規則:XxxRoutePredicateFactory。所有的這些謂詞工廠都是如下的繼承關系

public class MethodRoutePredicateFactory extends   AbstractRoutePredicateFactory<MethodRoutePredicateFactory.Config>
// 
public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config>
// ...

所有的謂詞工廠繼承的AbstractRoutePredicateFactory中的泛型都是內部類的Config。這個是如何被配置上值的呢?

1.1 gateway自動配置

在下面這個類中配置了所有的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)));
  }
}

這里會層層委托最終查找查找路由定位會交給RouteDefinitionRouteLocator。CachingRouteLocator起到緩存的作用,將配置的所有路由信息保存。

注意:這里的路由信息是在容器啟動后就會被初始化的。

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);
  }
}

實例化CachingRouteLocator就開始查找所有配置的Route信息。最終的會委托給RouteDefinitionRouteLocator

RouteDefinitionRouteLocator構造函數中的initFactories方法用來映射路由工廠的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);
  });
}

方法中解析每一個謂詞工廠對應的名稱然后緩存到predicates 集合中。

factory.name()方法解析謂詞名稱。

default String name() {
  return NameUtils.normalizeRoutePredicateName(getClass());
}

CachingRouteLocator是個緩存路由定位器,是個首選的RouteLocator(@Primary),這里將RouteDefinitionRouteLocator進行了合并。

1.2 生成路由對象Route及Config配置

getRoutes---》convertToRoute---》combinePredicates---》lookup。

根據上面的自動配置也知道了在服務啟動時就進行初始化所有路由信息了。

獲取路由信息

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;
}

進入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進行關聯設置值
  Object config = this.configurationService.with(factory)
    .name(predicate.getName())
    .properties(predicate.getArgs())
    .eventFunction((bound, properties) -> new PredicateArgsEvent(
        RouteDefinitionRouteLocator.this, route.getId(), properties))
    .bind();
  // 最終調用謂詞工廠(XxxRoutePredicateFactory的apply方法返回RoutePredicate該對象繼承Predicate)
  return factory.applyAsync(config);
}

lookup方法中查找,也就是在這里將對應的謂詞Config與RouteDefinition(Predicate)中定義的相對應的屬性關聯。

進入factory.applyAsync方法

@FunctionalInterface
public interface RoutePredicateFactory<C> extends ShortcutConfigurable, Configurable<C> {
  default AsyncPredicate<ServerWebExchange> applyAsync(C config) {
    return toAsyncPredicate(apply(config)); // 查看下面的6.2-1圖當前apply所有的實現就是系統內部定義的XxxRoutePredicateFactory
  }
}
// apply(config),如這里配置了Path謂詞,那么就會進入PathRoutePredicateFactory中的apply方法
public Predicate<ServerWebExchange> apply(Config config) {
  // other code    
  return new GatewayPredicate() {
    public boolean test() {
      // todo    
    }
  }
}
// 最后返回一個異步的謂詞
public static AsyncPredicate<ServerWebExchange> toAsyncPredicate(Predicate<? super ServerWebExchange> predicate) {
  Assert.notNull(predicate, "predicate must not be null");
  // 這里from就是返回一個DefaultAsyncPredicate默認的異步謂詞
  return AsyncPredicate.from(predicate);
}
static AsyncPredicate<ServerWebExchange> from( Predicate<? super ServerWebExchange> predicate) {
  return new DefaultAsyncPredicate<>(GatewayPredicate.wrapIfNeeded(predicate));
}

圖6.2-1

圖片圖片

最后在combinePredicates方法中將當前路由中配置的所有謂詞進行了and操作返回。最終回到convertToRoute方法中將當前路由中配置的謂詞,過濾器進行了整合包裝返回Route(一個路由對象)

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對象會被保存在上面說的
CachingRouteLocator.routes中。

6.3 定位路由

根據上面的配置RouteLocator 該類用來定位路由(查找具體的使用哪個路由);當一個請求過來會查找是哪個路由。

RouteLocator中定義了一個方法

public interface RouteLocator {


  Flux<Route> getRoutes();


}

查看這個getRoutes方法是誰調用的

圖片圖片

看到這個RoutePredicateHandlerMapping是不是想起了Spring MVC中的HandlerMapping(我們所有的Controller都會被 RequestMappingHanlderMapping 匹配)。通過名稱也就知道了該HandlerMapping用來匹配我們的路由謂詞的誰來處理路由。

接下來回到前面說的RequestMappingHanlderMapping 對象,當我們請求一個路由地址時會執行該類中的lookup方法查找路由

protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
  // 這里的this.routeLocator就是 CachingRouteLocator對象 
  return this.routeLocator.getRoutes()
      .concatMap(route -> Mono.just(route).filterWhen(r -> {
        exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId());
        // 過濾查找符合的路由  
        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;
     });
}

進入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();
    }


  }


}

這里會調用Predicate.test方法(XxxRoutePredicateFactory中的apply方法返回的GatewayPredicate)。

調用GatewayPredicate.test返回判斷當前請求的路由是否匹配。

整體的一個流程:

1、系統先初始化所有的Predicate(謂詞)和Filter(過濾器)

2、根據配置的路由信息(過濾器,謂詞)包裝返回Route對象

3、根據請求路由路徑查找匹配的路由


責任編輯:武曉燕 來源: Spring全家桶實戰案例源碼
相關推薦

2021-07-07 06:38:33

SpringCloud路由配置定位原理分析

2023-07-20 10:04:37

底層路由配置

2021-03-26 22:23:13

Python算法底層

2021-07-20 10:26:53

源碼底層ArrayList

2019-10-16 16:33:41

Docker架構語言

2023-03-17 17:51:30

APIServer路由注冊

2021-03-26 06:01:45

日志MongoDB存儲

2025-03-05 00:49:00

Win32源碼malloc

2020-12-14 08:03:52

ArrayList面試源碼

2020-12-17 08:03:57

LinkedList面試源碼

2020-04-27 07:13:37

Nginx底層進程

2010-07-29 08:41:11

路由器故障

2010-08-10 11:19:28

路由器故障

2023-11-09 09:08:38

RibbonSpring

2023-07-17 08:02:44

ZuulIO反應式

2022-05-12 08:21:13

項目網關模塊

2022-06-12 07:27:39

微服務GateWay

2022-05-16 08:22:11

網關過濾器路由

2025-03-31 00:00:00

MCPAPI服務器通信
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品欧美一区喷水 | 亚洲欧美日韩在线一区二区 | 最新av在线播放 | 成人在线视频网址 | 一级黄色短片 | 国产精品毛片无码 | 91欧美| 少妇一级淫片aaaaaaaaa | 国产视频久久久 | 99久久婷婷国产综合精品首页 | 欧美三级三级三级爽爽爽 | 成人免费一区二区 | 香蕉超碰| 日韩精品在线网站 | 日本中出视频 | 日韩另类视频 | 久久国产亚洲 | 国产女人第一次做爰毛片 | 免费a级毛片在线播放 | 天天射视频| 欧洲一区二区在线 | 国产一区二区三区四区五区加勒比 | 国产精品一区二区不卡 | 亚洲一区二区久久久 | 久草资源网站 | 国产aⅴ爽av久久久久久久 | 日本一区二区高清不卡 | 天天操天天拍 | 久久久久电影 | 精品久久一区 | 中文字幕高清在线 | 国际精品鲁一鲁一区二区小说 | 欧美日韩国产在线 | 在线精品一区二区 | 天天欧美 | 欧美xxxx在线 | 亚洲免费婷婷 | 国产农村妇女精品一二区 | 国产精品视频久久 | 国产精品中文字幕在线观看 | 久久久黄色 |