日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

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

來源: 責編: 時間:2023-08-14 22:01:30 375觀看
導讀環境:springcloud Hoxton.SR11本節主要了解系統中的謂詞與配置的路由信息是如何進行初始化關聯生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的。所有的謂詞工廠中的Config中屬性值是如何被配置的。

環境:springcloud Hoxton.SR11hVk28資訊網——每日最新資訊28at.com

本節主要了解系統中的謂詞與配置的路由信息是如何進行初始化關聯生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的。hVk28資訊網——每日最新資訊28at.com

所有的謂詞工廠中的Config中屬性值是如何被配置的。hVk28資訊網——每日最新資訊28at.com

在SpringCloud Gateway中的所有謂詞工廠如下:hVk28資訊網——每日最新資訊28at.com

圖片圖片hVk28資訊網——每日最新資訊28at.com

命名規則:XxxRoutePredicateFactory。所有的這些謂詞工廠都是如下的繼承關系hVk28資訊網——每日最新資訊28at.com

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

所有的謂詞工廠繼承的AbstractRoutePredicateFactory中的泛型都是內部類的Config。這個是如何被配置上值的呢?hVk28資訊網——每日最新資訊28at.com

1.1 gateway自動配置

在下面這個類中配置了所有的Predicate和Filter。hVk28資訊網——每日最新資訊28at.com

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起到緩存的作用,將配置的所有路由信息保存。hVk28資訊網——每日最新資訊28at.com

注意:這里的路由信息是在容器啟動后就會被初始化的。hVk28資訊網——每日最新資訊28at.com

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信息。最終的會委托給RouteDefinitionRouteLocatorhVk28資訊網——每日最新資訊28at.com

RouteDefinitionRouteLocator構造函數中的initFactories方法用來映射路由工廠的XxxRoutePredicateFactory。hVk28資訊網——每日最新資訊28at.com

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 集合中。hVk28資訊網——每日最新資訊28at.com

factory.name()方法解析謂詞名稱。hVk28資訊網——每日最新資訊28at.com

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

CachingRouteLocator是個緩存路由定位器,是個首選的RouteLocator(@Primary),這里將RouteDefinitionRouteLocator進行了合并。hVk28資訊網——每日最新資訊28at.com

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

getRoutes---》convertToRoute---》combinePredicates---》lookup。hVk28資訊網——每日最新資訊28at.com

根據上面的自動配置也知道了在服務啟動時就進行初始化所有路由信息了。hVk28資訊網——每日最新資訊28at.com

獲取路由信息hVk28資訊網——每日最新資訊28at.com

public Flux<Route> getRoutes() {  Flux<Route> routes = this.routeDefinitionLocator.getRouteDefinitions() .map(this::convertToRoute);  routes = routes.onErrorContinue((error, obj) -> {    return routes.map(route -> {            return route;  });}

合并謂詞(鏈式調用)hVk28資訊網——每日最新資訊28at.com

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中hVk28資訊網——每日最新資訊28at.com

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)中定義的相對應的屬性關聯。hVk28資訊網——每日最新資訊28at.com

進入factory.applyAsync方法hVk28資訊網——每日最新資訊28at.com

@FunctionalInterfacepublic 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-1hVk28資訊網——每日最新資訊28at.com

圖片圖片hVk28資訊網——每日最新資訊28at.com

最后在combinePredicates方法中將當前路由中配置的所有謂詞進行了and操作返回。最終回到convertToRoute方法中將當前路由中配置的謂詞,過濾器進行了整合包裝返回Route(一個路由對象)hVk28資訊網——每日最新資訊28at.com

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中。hVk28資訊網——每日最新資訊28at.com

6.3 定位路由

根據上面的配置RouteLocator 該類用來定位路由(查找具體的使用哪個路由);當一個請求過來會查找是哪個路由。hVk28資訊網——每日最新資訊28at.com

RouteLocator中定義了一個方法hVk28資訊網——每日最新資訊28at.com

public interface RouteLocator {  Flux<Route> getRoutes();}

查看這個getRoutes方法是誰調用的hVk28資訊網——每日最新資訊28at.com

圖片圖片hVk28資訊網——每日最新資訊28at.com

看到這個RoutePredicateHandlerMapping是不是想起了Spring MVC中的HandlerMapping(我們所有的Controller都會被 RequestMappingHanlderMapping 匹配)。通過名稱也就知道了該HandlerMapping用來匹配我們的路由謂詞的誰來處理路由。hVk28資訊網——每日最新資訊28at.com

接下來回到前面說的RequestMappingHanlderMapping 對象,當我們請求一個路由地址時會執行該類中的lookup方法查找路由hVk28資訊網——每日最新資訊28at.com

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)hVk28資訊網——每日最新資訊28at.com

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)。hVk28資訊網——每日最新資訊28at.com

調用GatewayPredicate.test返回判斷當前請求的路由是否匹配。hVk28資訊網——每日最新資訊28at.com

整體的一個流程:hVk28資訊網——每日最新資訊28at.com

1、系統先初始化所有的Predicate(謂詞)和Filter(過濾器)hVk28資訊網——每日最新資訊28at.com

2、根據配置的路由信息(過濾器,謂詞)包裝返回Route對象hVk28資訊網——每日最新資訊28at.com

3、根據請求路由路徑查找匹配的路由hVk28資訊網——每日最新資訊28at.com


hVk28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-5745-0.htmlSpringCloud Gateway 路由如何定位從底層源碼分析

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 18 個高級工程師必須會的強大JavaScript 技巧

下一篇: 代碼的未來洞察:程序員們對技術趨勢的解讀

標簽:
  • 熱門焦點
  • K60 Pro官方停產 第三方瞬間漲價

    雖然沒有官方宣布,但Redmi的一些高管也已經透露了,Redmi K60 Pro已經停產且不會補貨,這一切都是為了即將到來的K60 Ultra鋪路,屬于廠家的正常操作。但有意思的是該機在停產之后
  • MIX Fold3包裝盒泄露 新機本月登場

    小米的全新折疊屏旗艦MIX Fold3將于本月發布,近日該機的真機包裝盒在網上泄露。從圖上來看,新的MIX Fold3包裝盒在外觀設計方面延續了之前的方案,變化不大,這也是目前小米旗艦
  • 5月iOS設備性能榜:M1 M2依舊是榜單前五

    和上個月一樣,沒有新品發布的iOS設備性能榜的上榜設備并沒有什么更替,僅僅只有跑分變化而產生的排名變動,剛剛開始的蘋果WWDC2023,推出的產品也依舊是新款Mac Pro、新款Mac Stu
  • 三言兩語說透設計模式的藝術-單例模式

    寫在前面單例模式是一種常用的軟件設計模式,它所創建的對象只有一個實例,且該實例易于被外界訪問。單例對象由于只有一個實例,所以它可以方便地被系統中的其他對象共享,從而減少
  • 三萬字盤點 Spring 九大核心基礎功能

    大家好,我是三友~~今天來跟大家聊一聊Spring的9大核心基礎功能。話不多說,先上目錄:圖片友情提示,本文過長,建議收藏,嘿嘿嘿!一、資源管理資源管理是Spring的一個核心的基礎功能,不
  • 自動化在DevOps中的力量:簡化軟件開發和交付

    自動化在DevOps中扮演著重要角色,它提升了DevOps的效能。通過自動化工具和方法,DevOps團隊可以實現以下目標:消除手動和重復性任務。簡化流程。在整個軟件開發生命周期中實現更
  • 朋友圈可以修改可見范圍了 蘋果用戶可率先體驗

    近日,iOS用戶迎來微信8.0.27正式版更新,除了可更換二維碼背景外,還新增了多項實用功能。在新版微信中,朋友圈終于可以修改可見范圍,簡單來說就是已發布的朋友圈
  • 華為舉行春季智慧辦公新品發布會 首次推出電子墨水屏平板

    北京時間2月27日晚,華為在巴塞羅那舉行春季智慧辦公新品發布會,在海外市場推出之前已經在中國市場上市的筆記本、平板、激光打印機等辦公產品,并首次推出搭載
  • 北京:科技教育體驗基地開始登記

      北京“科技館之城”科技教育體驗基地登記和認證工作日前啟動。首批北京科技教育體驗基地擬于2023年全國科普日期間掛牌,后續還將開展常態化登記?! ”本┛萍冀逃w驗基
Top 主站蜘蛛池模板: 辽宁省| 高碑店市| 云和县| 翁源县| 江永县| 定襄县| 溆浦县| 康平县| 耿马| 海淀区| 铜川市| 南郑县| 河南省| 孟村| 增城市| 卢氏县| 米脂县| 竹北市| 浦江县| 天等县| 米林县| 晋州市| 遂平县| 公安县| 武汉市| 辽阳县| 新泰市| 五寨县| 临汾市| 佛学| 冀州市| 玛多县| 永胜县| 中牟县| 崇礼县| 习水县| 张家港市| 辽阳县| 金坛市| 山阳县| 泽普县|