新聞中心
環(huán)境:SpringCloud Hoxton.SR11

為中江等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及中江網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設計、成都網(wǎng)站制作、中江網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
本節(jié)主要了解系統(tǒng)中的謂詞與配置的路由信息是如何進行初始化關聯(lián)生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的。
所有的謂詞工廠中的Config中屬性值是如何被配置的。
在SpringCloud Gateway中的所有謂詞工廠如下:
圖片
命名規(guī)則:XxxRoutePredicateFactory。所有的這些謂詞工廠都是如下的繼承關系
public class MethodRoutePredicateFactory extends AbstractRoutePredicateFactory
//
public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory
// ... 所有的謂詞工廠繼承的AbstractRoutePredicateFactory中的泛型都是內部類的Config。這個是如何被配置上值的呢?
6.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 gatewayFilters, List predicates, RouteDefinitionLocator routeDefinitionLocator, ConfigurationService configurationService) {
return new RouteDefinitionRouteLocator(routeDefinitionLocator, predicates,
gatewayFilters, properties, configurationService);
}
@Bean
@Primary
@ConditionalOnMissingBean(name = "cachedCompositeRouteLocator")
public RouteLocator cachedCompositeRouteLocator(List routeLocators) {
return new CachingRouteLocator(new CompositeRouteLocator(Flux.fromIterable(routeLocators)));
}
} 這里會層層委托最終查找查找路由定位會交給RouteDefinitionRouteLocator。CachingRouteLocator起到緩存的作用,將配置的所有路由信息保存。
注意:這里的路由信息是在容器啟動后就會被初始化的。
public class CachingRouteLocator {
private final RouteLocator delegate;
private final Flux routes;
private final Map 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 fetch() {
return this.delegate.getRoutes().sort(AnnotationAwareOrderComparator.INSTANCE);
}
} 實例化CachingRouteLocator就開始查找所有配置的Route信息。最終的會委托給RouteDefinitionRouteLocator
RouteDefinitionRouteLocator構造函數(shù)中的initFactories方法用來映射路由工廠的XxxRoutePredicateFactory。
private void initFactories(List 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進行了合并。
6.2 生成路由對象Route及Config配置
getRoutes---》convertToRoute---》combinePredicates---》lookup。
根據(jù)上面的自動配置也知道了在服務啟動時就進行初始化所有路由信息了。
獲取路由信息
public Flux getRoutes() {
Flux routes = this.routeDefinitionLocator.getRouteDefinitions() .map(this::convertToRoute);
routes = routes.onErrorContinue((error, obj) -> {
return routes.map(route -> {
return route;
});
} 合并謂詞
private AsyncPredicate combinePredicates(
RouteDefinition routeDefinition) {
// other code
for (PredicateDefinition andPredicate : predicates.subList(1, predicates.size())) {
AsyncPredicate found = lookup(routeDefinition, andPredicate);
predicate = predicate.and(found);
}
return predicate;
} 進入lookup中
private AsyncPredicate lookup(RouteDefinition route, PredicateDefinition predicate) {
RoutePredicateFactory lookup方法中查找,也就是在這里將對應的謂詞Config與RouteDefinition(Predicate)中定義的相對應的屬性關聯(lián)。
進入factory.applyAsync方法
@FunctionalInterface
public interface RoutePredicateFactory extends ShortcutConfigurable, Configurable {
default AsyncPredicate applyAsync(C config) {
return toAsyncPredicate(apply(config)); // 查看下面的6.2-1圖當前apply所有的實現(xiàn)就是系統(tǒng)內部定義的XxxRoutePredicateFactory
}
}
// apply(config),如這里配置了Path謂詞,那么就會進入PathRoutePredicateFactory中的apply方法
public Predicate apply(Config config) {
// other code
return new GatewayPredicate() {
public boolean test() {
// todo
}
}
}
// 最后返回一個異步的謂詞
public static AsyncPredicate toAsyncPredicate(Predicate super ServerWebExchange> predicate) {
Assert.notNull(predicate, "predicate must not be null");
// 這里from就是返回一個DefaultAsyncPredicate默認的異步謂詞
return AsyncPredicate.from(predicate);
}
static AsyncPredicate 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 predicate;
private final List gatewayFilters;
private final Map metadata;
} 這些Route對象會被保存在上面說的CachingRouteLocator.routes中。
6.3 定位路由
根據(jù)上面的配置RouteLocator 該類用來定位路由(查找具體的使用哪個路由);當一個請求過來會查找是哪個路由。
RouteLocator中定義了一個方法
public interface RouteLocator {
Flux getRoutes();
} 查看這個getRoutes方法是誰調用的
圖片
看到這個RoutePredicateHandlerMapping是不是想起了Spring MVC中的HandlerMapping(我們所有的Controller都會被 RequestMappingHanlderMapping 匹配)。通過名稱也就知道了該HandlerMapping用來匹配我們的路由謂詞的誰來處理路由。
接下來回到前面說的RequestMappingHanlderMapping 對象,當我們請求一個路由地址時會執(zhí)行該類中的lookup方法查找路由
protected Mono 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 extends Function> {
static AsyncPredicate from(Predicate super ServerWebExchange> predicate) {
return new DefaultAsyncPredicate<>(GatewayPredicate.wrapIfNeeded(predicate));
}
class DefaultAsyncPredicate implements AsyncPredicate {
private final Predicate delegate;
public DefaultAsyncPredicate(Predicate delegate) {
this.delegate = delegate;
}
@Override
public Publisher 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、系統(tǒng)先初始化所有的Predicate(謂詞)和Filter(過濾器)
2、根據(jù)配置的路由信息(過濾器,謂詞)包裝返回Route對象
3、根據(jù)請求路由路徑查找匹配的路由
標題名稱:深入SpringCloudGateway底層詳解路由配置定位原理
鏈接分享:http://www.fisionsoft.com.cn/article/dhhshio.html


咨詢
建站咨詢
