网关 Spring-Cloud-Gateway 源码解析 —— 路由(1.2)之 PropertiesRouteDefinitionLocator 配置文件

栏目: Java · 发布时间: 8年前

内容简介:网关 Spring-Cloud-Gateway 源码解析 —— 路由(1.2)之 PropertiesRouteDefinitionLocator 配置文件

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

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

网关 Spring-Cloud-Gateway 源码解析 —— 路由(1.2)之 PropertiesRouteDefinitionLocator 配置文件

关注 微信公众号:【芋道源码】 有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有 源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言 将得到 认真 回复。 甚至不知道如何读源码也可以请教噢
  4. 新的 源码解析文章 实时 收到通知。 每周更新一篇左右
  5. 认真的 源码交流微信群。

1. 概述

本文主要对 PropertiesRouteDefinitionLocator 的源码实现

网关 Spring-Cloud-Gateway 源码解析 —— 路由(1.2)之 PropertiesRouteDefinitionLocator 配置文件

  • 蓝色 部分 :PropertiesRouteDefinitionLocator 。

推荐 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
    
  • defaultFilters 属性, 默认 过滤器配置。通过 spring.cloud.gateway.default-filters 配置。以 YAML 配置文件举例子 :

    spring:
    cloud:
    gateway:
    default-filters:
    - AddResponseHeader=X-Response-Default-Foo, Default-Bar
    - PrefixPath=/httpbin
    

666. 彩蛋

TODO 【3017】 与 Spring Cloud Config 结合

网关 Spring-Cloud-Gateway 源码解析 —— 路由(1.2)之 PropertiesRouteDefinitionLocator 配置文件

胖友,分享一波朋友圈可好!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Reality Is Broken

Reality Is Broken

Jane McGonigal / Penguin Press HC, The / 2011-1-20 / USD 26.95

Visionary game designer Jane McGonigal reveals how we can harness the power of games to solve real-world problems and boost global happiness. More than 174 million Americans are gamers, and......一起来看看 《Reality Is Broken》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换