SpringCloudGateway注册发现源码分析

上一篇文章简单地介绍了SpringCloudGateway配合Eureka来实现服务的注册与发现,本篇我们进入的SCG的源码来看看它是如何实现的。
首先来弄明白,服务发现是何时启动的,其实在SCG的启动文件里有服务发现的引导类:
在这里插入图片描述
进入到GatewayDiscoveryClientAutoConfiguration,可以发现如下代码,看到配置就明白了,只要我们在配置文件中设置这spring.cloud.gateway.discovery.locator.enabled=true,则服务注册发现就会注入DiscoveryClientRouteDefinitionLocator这个bean到容器

	@Configuration(proxyBeanMethods = false)
	@Deprecated
	@ConditionalOnProperty(value = "spring.cloud.discovery.reactive.enabled",
			havingValue = "false")
	public static class BlockingDiscoveryClientRouteDefinitionLocatorConfiguration {

		@Bean
		@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
		public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
				DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
			return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
		}

查看DiscoveryClientRouteDefinitionLocator实现了RouteDefinitionLocator接口,则其最终会注入到RoutePredicateHandlerMapping,这样我们就明白了其实服务注册也是跟我们在配置文件配置的route规则,以及在java类中写的bulider.routes()是一样的处理逻辑。服务注册发现也是一个route,只不过其predicate和访问目标服务的方式比较特殊。我们可以debug源码,验证我们的分析,如下图所示,可以看到服务注册发现确实就是一个route。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值