草庐IT

Spring Cloud GateWay路由信息的获取

我是谁,该干嘛 2024-01-24 原文

Spring Cloud GateWay 基本术语

  • Route(路由): 网关的基本构建,它由ID、目标URI、断言收集器集合、过滤器集合组成。
  • Predicate(断言): 路由的匹配条件,只有同时满足所有条件时才能通过匹配
  • Filter(过滤器): 对请求进行拦截,通过他你可以在发送下游请求之前或之后修改请求和响应。

特性:
1、支持动态路由
2、持内置到Spring Handler映射中的路由匹配
3、支持HTTP路由匹配
4、过滤器链作用于路由匹配
5、过滤器可以修改HTTP请求头和响应数据
6、支持 Spring Cloud DiscoveryClient 路由配置
7、支持API或者配置驱动

整体架构

1、当客户端发送一个请求的时候,首先它通过请求的路由匹配(断言)找到对应的Route 。
2、然后通过一系列的过滤器链 进行处理,最终到达 Proxied Service 。
3、响应同样的经过一系列的Filter链过滤,最后响应给客户端。

Spring Cloud GateWay 路由 Route 信息加载

Spring Cloud GateWay 是基于SpringBoot的自动装配来完成加载所需要的Bean信息的。 先我们只关注 GatewayAutoConfiguration 这个类。由于我们今天只关注路由信息的加载,所以我们只需关注如下几个方法。

  • 1、读取配置路由信息,从这我们可以看到 Spring Cloud GateWay 在启动时就加载了配置文件种的路由信息以及全局过滤器信息
	// 从配置文件读取Route信息
	@Bean
	public GatewayProperties gatewayProperties() {
		return new GatewayProperties();
	}
@ConfigurationProperties("spring.cloud.gateway") // 从配置文件读取路由信息
@Validated
public class GatewayProperties {
	// 路由列表
	@NotNull
	@Valid
	private List<RouteDefinition> routes = new ArrayList<>();
	
	// 作用于所有的路由过滤器,相当于全局过滤器
	private List<FilterDefinition> defaultFilters = new ArrayList<>();
	
	// ... 省略部分代码
}

RouteDefinition 路由信息。它里面定义了一个全局路由Id、断言列表、过滤器链和要转发的目的URI。这与我们一开始说的Route概率差不多。对于PredicateDefinition、FilterDefinition 这儿就不做介绍了。

@Validated
public class RouteDefinition {

	@NotEmpty // 路由Id
	private String id = UUID.randomUUID().toString();

	@NotEmpty
	@Valid // 配置的断言信息
	private List<PredicateDefinition> predicates = new ArrayList<>();

	@Valid // 配置的过滤器信息
	private List<FilterDefinition> filters = new ArrayList<>();

	@NotNull // 要转发的目的URI
	private URI uri;
}
  • 2、这时我们已经将路由信息封装到 RouteDefinition 中了,那我们是如何获取 RouteDefinition信息的呢?Spring Cloud GateWay 提供了如下接口 RouteDefinitionLocator(RouteDefinition信息定位器) 来获取RouteDefinition信息。
public interface RouteDefinitionLocator {
	// 获取 RouteDefinition
	Flux<RouteDefinition> getRouteDefinitions();
}

其实现类有如下几种

  • CachingRouteDefinitionLocator:缓存方式
  • CompositeRouteDefinitionLocator:组合方式
  • PropertiesRouteDefinitionLocator:基于属性配置
  • DiscoveryClientRouteDefinitionLocator:基于服务发现
  • RouteDefinitionRepository 继承自RouteDefinitionLocator,用于对路由定义的操作(保存、删除路由定义)

    在 GatewayAutoConfiguration中我们可以看到如下几个方法,以及加载Spring Cloud GateWay 默认提供的断言和过滤器
	// PropertiesRouteDefinitionLocator ,加载了GatewayProperties。从而获取对应的RouteDefinition
	@Bean
	@ConditionalOnMissingBean
	public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator(
			GatewayProperties properties) {
		return new PropertiesRouteDefinitionLocator(properties);
	}
	
	// 获取到所有的 RouteDefinition信息定位器 ,然后组合成 CompositeRouteDefinitionLocator
	@Bean
	@Primary
	public RouteDefinitionLocator routeDefinitionLocator(
			List<RouteDefinitionLocator> routeDefinitionLocators) {
		return new CompositeRouteDefinitionLocator(
				Flux.fromIterable(routeDefinitionLocators));
	}
	
	// 加载Spring Cloud GateWay 提供过滤器
	@Bean
	public AddResponseHeaderGatewayFilterFactory addResponseHeaderGatewayFilterFactory() {
		return new AddResponseHeaderGatewayFilterFactory();
	} // ...
	
	// 加载Spring Cloud GateWay 提供的断言
	@Bean
	public BeforeRoutePredicateFactory beforeRoutePredicateFactory() {
		return new BeforeRoutePredicateFactory();
	}
  • 3、现在我们已经知道RouteDefinition加载的方式以及如何获取。那在请求的时候我们如何获取Route呢?这时Spring Cloud Gateway 有提供了如下接口 RouteLocator 来获取路由。
public interface RouteLocator {
	// 获取路由
	Flux<Route> getRoutes();
}

有关Spring Cloud GateWay路由信息的获取的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  5. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  6. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  7. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  8. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  9. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  10. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

随机推荐