解决Spring Cloud Bus不刷新所有节点的问题及理解"Application Context ID must be unique"

如果同一微服务的多个实例使用的端口相同,当配置修改时,使用Spring Cloud Bus不会刷新全部实例的配置。此时需要配置各个实例的spring.application.index为不同的值。下面我们来分析一下原因。

在Spring Cloud Config上有这么一段:

Application Context ID must be unique

The bus tries to eliminate processing an event twice, once from the original ApplicationEvent and once from the queue. To do this, it checks the sending application context id againts the current application context id. If multiple instances of a service have the same application context id, events will not be processed. Running on a local machine, each service will be on a different port and that will be part of the application context id. Cloud Foundry supplies an index to differentiate. To ensure that the application context id is the unique, setspring.application.index to something unique for each instance of a service. For example, in lattice, setspring.application.index=${INSTANCE_INDEX} in application.properties (or bootstrap.properties if using configserver).

这段话的意思,大致上是说如果相同微服务的多个实例,使用的是相同的端口时,需要配置spring.application.index 属性,本文来分析一下为什么。

(1) 我们知道定位Spring Boot的问题,往往可以从配置开始。按照这个思路,先找到spring.application.index 所在的类ContextIdApplicationContextInitializer。至于怎么找到的,可以看这里:docs.spring.io/spring-boot… ,搜索spring.application.index即可。

(2) 在org.springframework.boot.context.ContextIdApplicationContextInitializer 类的getApplicationId() 方法中,有类似以下的内容:

从代码可知,如果什么都不设置,并且相同微服务的多个实例使用的是相同的端口的话,那么isFromSelf将会返回true。

(4) 在org.springframework.cloud.bus.BusAutoConfiguration.acceptRemote(RemoteApplicationEvent)中的代码:

看到这段代码,原因已经一目了然了。

欢迎大家和我一起学习spring cloud构建微服务云架构,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目

我本人邀约各大BATJ架构大牛共创Spring Cloud构建微服务架构的交流社区。 (群号:547793198)欢迎各路架构师、开发者,学习与交流使用Spring Cloud诸多强大组件的实战经验。

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!

合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

希望此文能帮到大家的同时,也听听大家的观点。欢迎留言讨论,加关注,分享你的高见!持续更新!

  • To-陌霖Java架构

分享互联网最新文章 关注互联网最新发展

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值