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

简介: 如果同一微服务的多个实例使用的端口相同,当配置修改时,使用Spring Cloud Bus不会刷新全部实例的配置。此时需要配置各个实例的spring.application.index为不同的值。

如果同一微服务的多个实例使用的端口相同,当配置修改时,使用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。至于怎么找到的,可以看这里:http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#common-application-properties ,搜索spring.application.index即可。

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

private String getApplicationId(ConfigurableEnvironment environment) {
  String name = environment.resolvePlaceholders(this.name);
  String index = environment.resolvePlaceholders(INDEX_PATTERN);
  String profiles = StringUtils
      .arrayToCommaDelimitedString(environment.getActiveProfiles());
  if (StringUtils.hasText(profiles)) {
    name = name + ":" + profiles;
  }
  if (!"null".equals(index)) {
    name = name + ":" + index;
  }
  return name;
}
AI 代码解读

其中,name的表达式如下:

${spring.application.name:${vcap.application.name:${spring.config.name:application}}} ,也就是配置的spring.application.name (以主流方式为例,当然也可能是spring.config.name)。

而index的表达式是:

${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}

也就是如果什么都不配置,就取server.port。

综上,如果什么都不配置,那么getApplicationId返回的是${spring.application.name}:${server.port}

(3) 在Spring Cloud Bus中的org.springframework.cloud.bus.ServiceMatcher 有以下代码:

public boolean isFromSelf(RemoteApplicationEvent event) {
  String originService = event.getOriginService();
  String serviceId = getServiceId();
  return this.matcher.match(originService, serviceId);
}

public boolean isForSelf(RemoteApplicationEvent event) {
  String destinationService = event.getDestinationService();
  return (destinationService == null || destinationService.trim().isEmpty() || this.matcher
      .match(destinationService, getServiceId()));
}

public String getServiceId() {
  return this.context.getId();
}
AI 代码解读

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

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

@StreamListener(SpringCloudBusClient.INPUT)
public void acceptRemote(RemoteApplicationEvent event) {
  if (event instanceof AckRemoteApplicationEvent) {
    if (this.bus.getTrace().isEnabled() && !this.serviceMatcher.isFromSelf(event)
        && this.applicationEventPublisher != null) {
      this.applicationEventPublisher.publishEvent(event);
    }
    // If it's an ACK we are finished processing at this point
    return;
  }
  if (this.serviceMatcher.isForSelf(event)
      && this.applicationEventPublisher != null) {
    if (!this.serviceMatcher.isFromSelf(event)) {
      this.applicationEventPublisher.publishEvent(event);
    }
    if (this.bus.getAck().isEnabled()) {
      AckRemoteApplicationEvent ack = new AckRemoteApplicationEvent(this,
          this.serviceMatcher.getServiceId(),
          this.bus.getAck().getDestinationService(),
          event.getDestinationService(), event.getId(), event.getClass());
      this.cloudBusOutboundChannel
          .send(MessageBuilder.withPayload(ack).build());
      this.applicationEventPublisher.publishEvent(ack);
    }
  }
  if (this.bus.getTrace().isEnabled() && this.applicationEventPublisher != null) {
    // We are set to register sent events so publish it for local consumption,
    // irrespective of the origin
    this.applicationEventPublisher.publishEvent(new SentApplicationEvent(this,
        event.getOriginService(), event.getDestinationService(),
        event.getId(), event.getClass()));
  }
}
AI 代码解读

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

Github上的相关issue:https://github.com/spring-cloud/spring-cloud-bus/issues/18

本文链接: http://www.itmuch.com/spring-cloud-code-read/spring-cloud-code-read-spring-cloud-bus/
**版权声明: **本博客由周立创作,采用 CC BY 3.0 CN 许可协议。可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。

目录
打赏
0
0
0
0
29
分享
相关文章
探索Spring之利剑:ApplicationContext接口
本文深入介绍了Spring框架中的核心接口ApplicationContext,解释了其作为应用容器的功能,包括事件发布、国际化支持等,并通过基于XML和注解的配置示例展示了如何使用ApplicationContext管理Bean实例。
131 6
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
54 6
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
90 5
|
3月前
|
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
66 5
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
|
8月前
|
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
186 3
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
818 15
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
103 0
通用快照方案问题之Spring Boot Admin的定义如何解决
通用快照方案问题之Spring Boot Admin的定义如何解决
89 0
通用快照方案问题之Martin Flower提出的微服务之间的通信如何解决
通用快照方案问题之Martin Flower提出的微服务之间的通信如何解决
61 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等