Spring-Cloud-Gateway源码分析系列 | Spring-Cloud-Gateway之RouteDefinition初始化加载

推荐 Spring Boot/Cloud 视频:

  • Java 微服务实践 - Spring Boot
  • Java 微服务实践 - Spring Cloud

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 getRouteDefinitions();
}

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

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 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 routeDefinitions;
    /**
     * 路由定义的本地缓存
     */
    private final Map 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 getRouteDefinitions() {
     ...代码在根据注册中心查找路由详细解析

    }
}   

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

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

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

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

    @Override
    public Flux getRouteDefinitions() {
        //将各个RouteDefinitionLocator的getRouteDefinitions合并返回统一的Flux
        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 routeDefinitionLocators) {
        return new CompositeRouteDefinitionLocator(Flux.fromIterable(routeDefinitionLocators));
    }

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

专家推荐

“随着微服务架构的发展,Spring Cloud 使用得越来越广泛。驰狼课堂 Spring Boot 快速入门,Spring Boot 与Spring Cloud 整合,docker+k8s,大型电商商城等多套免费实战教程可以帮您真正做到快速上手,将技术点切实运用到微服务项目中。” 
关注公众号,每天精彩内容,第一时间送达!
Spring-Cloud-Gateway源码分析系列 | Spring-Cloud-Gateway之RouteDefinition初始化加载_第1张图片

你可能感兴趣的:(Spring-Cloud-Gateway源码分析系列 | Spring-Cloud-Gateway之RouteDefinition初始化加载)