Spring Cloud Config Server 节点迁移引起的问题,请格外注意这一点!

本文分析了Spring Cloud Config Server在节点迁移后可能导致的问题,包括配置中心因健康检查失败报错的原因。通过源码解读,揭示了Spring Cloud Config的设计缺陷,并介绍了官方在2.x.x版本中如何修复此问题,即使用服务发现获取所有配置中心实例URI并进行重试。建议在生产环境中使用最新版Spring Cloud Config并进行相关测试。
摘要由CSDN通过智能技术生成

前言:

虽然强烈推荐选择使用国内开源的配置中心,如携程开源的 Apollo 配置中心、阿里开源的 Nacos 注册&配置中心。

但实际架构选型时,根据实际项目规模、业务复杂性等因素,有的项目还是会选择 Spring Cloud Config,也是 Spring Cloud 官网推荐的。特别是对性能要求也不是很高的场景,Spring Cloud Config 还算是好用的,基本能够满足需求,通过 Git 天然支持版本控制方式管理配置。

而且,目前 github 社区也有小伙伴针对 Spring Cloud Config 一些「缺陷」,开发了简易的配置管理界面,并且也已开源,如 spring-cloud-config-admin,也是超哥( 程序员DD)杰作,该项目地址:https://dyc87112.github.io/spring-cloud-config-admin-doc/

本文所使用的 Spring Cloud 版本:Edgware.SR3,Spring Boot 版本:1.5.10.RELEASE

问题分析:

个人认为这个问题是有代表性的,也能基于该问题,了解到官网是如何改进的。使用 Spring Cloud Config 过程中,如果遇到配置中心服务器迁移,可能会遇到 DD 这篇博客所描述的问题:
http://blog.didispace.com/Spring-Cloud-Config-Server-ip-change-problem/

我这里大概简述下该文章中提到的问题:

当使用的 Spring Cloud Config 配置中心节点迁移或容器化方式部署(IP 是变化的),Config Server 端会因为健康检查失败报错,检查失败是因为使用的还是迁移之前的节点 IP 导致。

本文结合这个问题作为切入点,继续延伸下,并结合源码探究下原因以及改进措施。

前提条件是使用了 DiscoveryClient 服务注册发现,如果我们使用了 Eureka 作为注册中心,其实现类是 EurekaDiscoveryClient
客户端通过 Eureka 连接配置中心,需要做如下配置:

spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.discovery.enabled=true

这里的关键是 spring.cloud.config.discovery.enabled 配置,默认值是 false,设置为 true 表示激活服务发现,最终会由 DiscoveryClientConfigServiceBootstrapConfiguration 启动配置类来查找配置中心服务。

接下来我们看下这个类的源码:

@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false) 
@Configuration
 // 引入工具类自动配置类
@Import({ UtilAutoConfiguration.class })
// 开启服务发现
@EnableDiscoveryClient 
public class DiscoveryClientConfigServiceBootstrapConfiguration {
@Autowired
private ConfigClientProperties config;
@Autowired
private ConfigServerInstanceProvider instanceProvider;
private HeartbeatMonitor monitor = new HeartbeatMonitor();
@Bean
public ConfigServerInstanceProvider configServerInstanceProvider(
                DiscoveryClient discoveryClient) {
    return new ConfigServerInstanceProvider(discoveryClient);
}

// 上下文刷新事件监听器,当服务启动或触发 /refresh 或触发消息总线的 /bus/refresh 后都会触发该事件
@EventListener(ContextRefreshedEvent.class)
public void startup(ContextRefreshedEvent event) {
    refresh();
}

// 心跳事件监听器,这个监听事件是客户端从Eureka中Fetch注册信息时触发的。
@EventLi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值