RouteDefinition路由定义,Spring-Cloud-Gateway通过RouteDefinition来转换生成具体的路由信息。RouteDefinition的信息是怎么加载初始化到网关系统中的,接下来阅读Spring-Cloud-Gateway的RouteDefinitionLocator(路由定义信息加载器)接口,学习RouteDefinition初始化加载


首先查看RouteDefinitionLocator源码

/**
 * 路由定义信息的定位器,
 * 负责读取路由配置( org.springframework.cloud.gateway.route.RouteDefinition
 * 子类实现类
 *  1.CachingRouteDefinitionLocator -RouteDefinitionLocator包装类, 缓存目标RouteDefinitionLocator 为routeDefinitions提供缓存功能
 *  2.CompositeRouteDefinitionLocator -RouteDefinitionLocator包装类,组合多种 RouteDefinitionLocator 的实现,为 routeDefinitions提供统一入口
 *  3.PropertiesRouteDefinitionLocator-从配置文件(GatewayProperties 例如,YML / Properties 等 ) 读取RouteDefinition
 *  4.DiscoveryClientRouteDefinitionLocator-从注册中心( 例如,Eureka / Consul / Zookeeper / Etcd 等 )读取RouteDefinition
 *  5.RouteDefinitionRepository-从存储器( 例如,内存 / Redis / MySQL 等 )读取RouteDefinition
 * @author Spencer Gibb
 */
public interface RouteDefinitionLocator {

    /**
     * 获取RouteDefinition
     * @return
     */
    Flux<RouteDefinition> getRouteDefinitions();
}

RouteDefinitionLocator接口有且仅有一个方法getRouteDefinitions,此方法获取RouteDefinition的核心方法,返回Flux<RouteDefinition>


RouteDefinitionLocator 类图如下:

graph TD
RouteDefinitionLocator-->CachingRouteDefinitionLocator
RouteDefinitionLocator-->CompositeRouteDefinitionLocator
RouteDefinitionLocator-->PropertiesRouteDefinitionLocator
RouteDefinitionLocator-->DiscoveryClientRouteDefinitionLocator
RouteDefinitionLocator-->RouteDefinitionRepository

子类功能描述:

  • CachingRouteDefinitionLocator:RouteDefinitionLocator包装类, 缓存目标RouteDefinitionLocator 为routeDefinitions提供缓存功能
  • CompositeRouteDefinitionLocator -RouteDefinitionLocator包装类,组合多种 RouteDefinitionLocator 的实现,为 routeDefinitions提供统一入口
  • PropertiesRouteDefinitionLocator-从配置文件(GatewayProperties 例如,YML / Properties 等 ) 读取RouteDefinition
  • RouteDefinitionRepository-从存储器( 例如,内存 / Redis / MySQL 等 )读取RouteDefinition
  • DiscoveryClientRouteDefinitionLocator-从注册中心( 例如,Eureka / Consul / Zookeeper / Etcd 等

接下来详细看子类的源码分析主要实现功能

  • PropertiesRouteDefinitionLocator

/**
 * 从Properties(GatewayProperties)中加载RouteDefinition信息
 * @author Spencer Gibb
 */
public class PropertiesRouteDefinitionLocator implements RouteDefinitionLocator {

    /**
     * 从appliccation.yml中解析前缀为spring.cloud.gateway的配置
     */
    private final GatewayProperties properties;

    public PropertiesRouteDefinitionLocator(GatewayProperties properties) {
        this.properties = properties;
    }

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        return Flux.fromIterable(this.properties.getRoutes());
    }
}

PropertiesRouteDefinitionLocator很简单从GatewayProperties实例获取RouteDefinition信息

  • GatewayProperties 在GatewayProperties初始化加载文中已详细描述
  • Flux 响应式编程
  • CachingRouteDefinitionLocator

/**
 * RouteDefinitionLocator 包装实现类,实现了路由定义的本地缓存功能
 * @author Spencer Gibb
 */
public class CachingRouteDefinitionLocator implements RouteDefinitionLocator {

    /**
     * 实际路由定义定位器
     */
    private final RouteDefinitionLocator delegate;
    
    private final Flux<RouteDefinition> routeDefinitions;
    /**
     * 路由定义的本地缓存
     */
    private final Map<String, List> cache = new HashMap<>();

    public CachingRouteDefinitionLocator(RouteDefinitionLocator delegate) {
        this.delegate = delegate;
        routeDefinitions = CacheFlux.lookup(cache, "routeDefs", RouteDefinition.class)
                .onCacheMissResume(() -> this.delegate.getRouteDefinitions());

    }

}   

RouteDefinitionLocator包装类,缓存目标RouteDefinitionLocator 为routeDefinitions提供缓存功能

  • DiscoveryClientRouteDefinitionLocator

public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLocator {
    /**
     * 注册中心客户端
     */
    private final DiscoveryClient discoveryClient;
    /**
     * 本地配置信息
     */
    private final DiscoveryLocatorProperties properties;
    /**
     * 路由ID前缀
     */
    private final String routeIdPrefix;

    public DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
        this.discoveryClient = discoveryClient;
        this.properties = properties;
        if (StringUtils.hasText(properties.getRouteIdPrefix())) {
            this.routeIdPrefix = properties.getRouteIdPrefix();
        } else {
            this.routeIdPrefix = this.discoveryClient.getClass().getSimpleName() + "_";
        }
    }

    /**
     * 通过注册中心查找服务组装路由定义信息
     * @return
     */
    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
     ...代码在根据注册中心查找路由详细解析

    }
}   

DiscoveryClientRouteDefinitionLocator通过调用 DiscoveryClient 获取注册在注册中心的服务列表,生成对应的 RouteDefinition 数组

  • CompositeRouteDefinitionLocator

/**
 * 组合多个 RouteDefinitionLocator 的实现,为 routeDefinitions提供统一入口
 * @author Spencer Gibb
 */
public class CompositeRouteDefinitionLocator implements RouteDefinitionLocator {

    /**
     * 所有路由定义定位器实例集合
     */
    private final Flux<RouteDefinitionLocator> delegates;

    public CompositeRouteDefinitionLocator(Flux<RouteDefinitionLocator> delegates) {
        this.delegates = delegates;
    }

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        //将各个RouteDefinitionLocator的getRouteDefinitions合并返回统一的Flux<RouteDefinition>
        return this.delegates.flatMap(RouteDefinitionLocator::getRouteDefinitions);
    }
}

CompositeRouteDefinitionLocator 的主要作用就是将各个定位器合并提供统一的getRouteDefinitions方法入口

通过子类实现具体功能可以很清晰的看到定位器加载RouteDefinition整个流程

graph TD
PropertiesRouteDefinitionLocator-->|配置文件加载初始化| CompositeRouteDefinitionLocator
RouteDefinitionRepository-->|存储器中加载初始化| CompositeRouteDefinitionLocator
DiscoveryClientRouteDefinitionLocator-->|注册中心加载初始化| CompositeRouteDefinitionLocator

最终提供通过CompositeRouteDefinitionLocator提供统一的
getRouteDefinitions方法

RouteDefinitionLocator实例的初始化在GatewayAutoConfiguration中已经完成

GatewayDiscoveryClientAutoConfiguration

@Configuration
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@AutoConfigureBefore(GatewayAutoConfiguration.class)
@ConditionalOnClass({DispatcherHandler.class, DiscoveryClient.class})
@EnableConfigurationProperties
public class GatewayDiscoveryClientAutoConfiguration {

    //初始化注册中心路由定义定位器
    @Bean
    @ConditionalOnBean(DiscoveryClient.class)
    @ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
    public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
            DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
        return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
    }

GatewayAutoConfiguration

   //初始化配置路由定义加载器
    @Bean
    @ConditionalOnMissingBean
    public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator(GatewayProperties properties) {
        return new PropertiesRouteDefinitionLocator(properties);
    }

    //初始化存储路由定义加载器
    @Bean
    @ConditionalOnMissingBean(RouteDefinitionRepository.class)
    public InMemoryRouteDefinitionRepository inMemoryRouteDefinitionRepository() {
        return new InMemoryRouteDefinitionRepository();
    }

     //初始化聚合路由定义加载器
    @Bean
    @Primary
    public RouteDefinitionLocator routeDefinitionLocator(List<RouteDefinitionLocator> routeDefinitionLocators) {
        return new CompositeRouteDefinitionLocator(Flux.fromIterable(routeDefinitionLocators));
    }

在Spring-Cloud-Gateway初始化完成后需要的路由定义加载器已全部实例化完成,这样就为路由的加载创建完成了必要的条件。

 

作者:Mr_1214
链接:https://www.jianshu.com/p/b02c7495eb5e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。