環境:springcloud Hoxton.SR11
本節主要了解系統中的謂詞與配置的路由信息是如何進行初始化關聯生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的。
所有的謂詞工廠中的Config中屬性值是如何被配置的。
在SpringCloud Gateway中的所有謂詞工廠如下:
命名規則:XxxRoutePredicateFactory。所有的這些謂詞工廠都是如下的繼承關系
- public class MethodRoutePredicateFactory extends AbstractRoutePredicateFactory<MethodRoutePredicateFactory.Config>
- //
- public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<PathRoutePredicateFactory.Config>
- // ...
所有的謂詞工廠繼承的
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<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進行了合并。
6.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、根據請求路由路徑查找匹配的路由
完畢!!!
到此這篇關于SpringCloud Gateway 路由配置定位原理分析的文章就介紹到這了,更多相關SpringCloud Gateway原理分析內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!