spring cloud bus 配置不能同步刷新

项目
在用spring cloud config做动态刷新,用spring cloud bus +kafka 做总线,
config 配置服务项目
config-client 同步配置项目
问题:
修改配置,然后在 config 主机调用接口http://localhost:9091/actuator/bus-refresh刷新配置时,config-client的配置依然没有改变。
解决方法:
两个项目的jar包(spring-cloud-starter-stream-kafka)版本不一致导致的。
config的版本是2.1.2
config-client 2.1.1
一开始配的是

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>

就不需要配spring-cloud-starter-stream-kafka和 spring-cloud-bus
但是发现spring-cloud-starter-bus-kafka这个版本虽然一致,但是包含的两个jar包版本还是不一致。
在这里插入图片描述
通过解决这次问题发现
当配置改变后会触发一个事件,该事件会往消息中间件topic为springCloudBus写入一个刷新事件RefreshRemoteApplicationEvent。
而监听到topic为springCloudBus的config-client 就会去触发本地一个刷新事件。
这次问题是因为两个jar包版本不一致导致topic也不一致。

梳理一下逻辑
spring cloud stream 在和消息中间件交互的时候增加了一层就是用绑定器binder来处理,这样可以抽象地和消息中间件交互。
spring cloud bus 配置不能同步刷新_第1张图片
中间传递消息的就是抽象出来的消息通道channel:inputs和outputs
消息给不同的消费者抽象出不同的主题topic。
spring cloud bus 通过channel:springCloudBusOutput 把消息写到总线,通过channel:springCloudBusInput读取消息总线
spring cloud config 刷新配置事件的topic是springCloudBus

关键类 BusAutoConfiguration

@Autowired
	@Output(SpringCloudBusClient.OUTPUT)
	public void setCloudBusOutboundChannel(MessageChannel cloudBusOutboundChannel) {
		this.cloudBusOutboundChannel = cloudBusOutboundChannel;
	}
    
    //监听事件RemoteApplicationEvent
	@EventListener(classes = RemoteApplicationEvent.class)
	public void acceptLocal(RemoteApplicationEvent event) {
		if (this.serviceMatcher.isFromSelf(event)
				&& !(event instanceof AckRemoteApplicationEvent)) {
			//从springCloudBusOutput写入总线
			this.cloudBusOutboundChannel.send(MessageBuilder.withPayload(event).build());
		}
	}

    //从springCloudBusInput读取消息总线
	@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()));
		}
	}

参考:《Spring Cloud微服务实战》

你可能感兴趣的:(处理的问题)