Spring-Cloud-Gateway 源码解析 —— 路由(1.3)之 RouteDefinitionRepository 存储器

摘要: 原创出处 http://www.iocoder.cn/Spring-Cloud-Gateway/route-definition-locator-properties/ 「芋道源码」欢迎转载,保留摘要,谢谢!

本文主要基于 Spring-Cloud-Gateway 2.0.X M4


1. 概述

本文主要对 PropertiesRouteDefinitionLocator 的源码实现

  • 蓝色部分 :PropertiesRouteDefinitionLocator 。

推荐 Spring Cloud 书籍

推荐 Spring Cloud 视频

2. PropertiesRouteDefinitionLocator

org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator ,从配置文件( 例如,YML / Properties 等 ) 读取路由配置。代码如下 :

public class PropertiesRouteDefinitionLocator implements RouteDefinitionLocator {

    private final GatewayProperties properties;

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

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        return Flux.fromIterable(this.properties.getRoutes());
    }
}
  • #getRouteDefinitions() 方法,从 GatewayProperties 获取路由配置数组。

3. GatewayProperties

org.springframework.cloud.gateway.config.GatewayProperties ,从配置文件读取

  • 路由配置
  • 默认过滤器配置。当 RouteDefinition => Route 时,会将过滤器配置添加到每个 Route 。

GatewayProperties 代码如下 :

@ConfigurationProperties("spring.cloud.gateway")
@Validated
public class GatewayProperties {

    /**
     * List of Routes
     */
    @NotNull
    @Valid
    private List<RouteDefinition> routes = new ArrayList<>();

    /**
     * List of filter definitions that are applied to every route.
     */
    private List<FilterDefinition> defaultFilters = loadDefaults();

    private ArrayList<FilterDefinition> loadDefaults() {
        ArrayList<FilterDefinition> defaults = new ArrayList<>();
        FilterDefinition definition = new FilterDefinition();
        definition.setName(normalizeFilterName(RemoveNonProxyHeadersGatewayFilterFactory.class));
        defaults.add(definition);
        return defaults;
    }
}
  • routes 属性,路由配置。通过 spring.cloud.gateway.routes 配置。以 YAML 配置文件举例子 :
    spring:
      cloud:
        gateway:
          routes:
          # =====================================
          - host_example_to_httpbin=${test.uri}, Host=**.example.org

          # =====================================
          - id: host_foo_path_headers_to_httpbin
            uri: ${test.uri}
            predicates:
            - Host=**.foo.org
            - Path=/headers
            - Method=GET
            - Header=X-Request-Id, \d+
            - Query=foo, ba.
            - Query=baz
            - Cookie=chocolate, ch.p
            - After=1900-01-20T17:42:47.789-07:00[America/Denver]
            filters:
            - AddResponseHeader=X-Response-Foo, Bar

          # =====================================
          - id: add_request_header_test
            uri: ${test.uri}
            predicates:
            - Host=**.addrequestheader.org
            - Path=/headers
            filters:
            - AddRequestHeader=X-Request-Foo, Bar
* 更多例子,点击 [application.yml](https://github.com/YunaiV/spring-cloud-gateway/blob/382a4cd98fbb8ac53a83a5559bacb0f885838074/spring-cloud-gateway-core/src/test/resources/application.yml#L15) 查看。
  • defaultFilters 属性,默认过滤器配置。通过 spring.cloud.gateway.default-filters 配置。以 YAML 配置文件举例子 :
    spring:
      cloud:
        gateway:
          default-filters:
          - AddResponseHeader=X-Response-Default-Foo, Default-Bar
          - PrefixPath=/httpbin
* 更多例子,点击 [application.yml](https://github.com/YunaiV/spring-cloud-gateway/blob/382a4cd98fbb8ac53a83a5559bacb0f885838074/spring-cloud-gateway-core/src/test/resources/application.yml#L10) 查看。
赞(0) 打赏

如未加特殊说明,此网站文章均为原创,转载必须注明出处。Java 技术驿站 » Spring-Cloud-Gateway 源码解析 —— 路由(1.3)之 RouteDefinitionRepository 存储器
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

关注【Java 技术驿站】公众号,每天早上 8:10 为你推送一篇技术文章

扫描二维码关注我!


关注【Java 技术驿站】公众号 回复 “VIP”,获取 VIP 地址永久关闭弹出窗口

免费获取资源

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏