解決Spring Cloud Bus不刷新所有節(jié)點的問題及理解"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。至于怎么找到的,可以看這里: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() 方法中,有類似以下的內(nèi)容:

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;
}

其中,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();
}

從代碼可知,如果什么都不設置,并且相同微服務的多個實例使用的是相同的端口的話,那么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()));
  }
}

看到這段代碼,原因已經(jīng)一目了然了。

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

本文首發(fā)

http://www.itmuch.com/spring-cloud-code-read/spring-cloud-code-read-spring-cloud-bus/

干貨分享

全是干貨
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容