环境:springcloud Hoxton.SR11
本节主要了解系统中的谓词与配置的路由信息是如何进行初始化关联生成路由对象的。每个谓词工厂中的Config对象又是如何被解析配置的。
所有的谓词工厂中的Config中属性值是如何被配置的。
在SpringCloud Gateway中的所有谓词工厂如下:
图片
命名规则:XxxRoutePredicateFactory。所有的这些谓词工厂都是如下的继承关系
public class MethodRoutePredicateFactory extends AbstractRoutePredicateFactory
//
public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory
// ...
所有的谓词工厂继承的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 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构造函数中的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进行了合并。
1.2 生成路由对象Route及Config配置
getRoutes---》convertToRoute---》combinePredicates---》lookup。
根据上面的自动配置也知道了在服务启动时就进行初始化所有路由信息了。
获取路由信息
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 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 extends ShortcutConfigurable, Configurable {
default AsyncPredicate applyAsync(C config) {
return toAsyncPredicate(apply(config)); // 查看下面的6.2-1图当前apply所有的实现就是系统内部定义的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