博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud config与bus结合
阅读量:6566 次
发布时间:2019-06-24

本文共 3616 字,大约阅读时间需要 12 分钟。

  hot3.png

##整体流程

输入图片说明

默认gitlab的webhook更新调用config server的/monitor(spring-cloud-config-monitor)触发RefreshRemoteApplicationEvent事件,然后spring cloud bus的StreamListener监听RemoteApplicationEvent,通过mq发布到每个config client,然后client接收RemoteApplicationEvent事件来实现refresh。

##PropertyPathEndpoint/monitor

@RequiredArgsConstructor@RestController@RequestMapping(path = "${spring.cloud.config.monitor.endpoint.path:}/monitor")@CommonsLogpublic class PropertyPathEndpoint		implements ApplicationEventPublisherAware, ApplicationContextAware {@RequestMapping(method = RequestMethod.POST)	public Set
notifyByPath(@RequestHeader HttpHeaders headers, @RequestBody Map
request) { PropertyPathNotification notification = this.extractor.extract(headers, request); if (notification != null) { Set
services = new LinkedHashSet<>(); for (String path : notification.getPaths()) { services.addAll(guessServiceName(path)); } if (this.applicationEventPublisher != null) { for (String service : services) { log.info("Refresh for: " + service); this.applicationEventPublisher .publishEvent(new RefreshRemoteApplicationEvent(this, this.contextId, service)); } return services; } } return Collections.emptySet(); }}

##MQ接收RefreshRemoteApplicationEvent发布message(BusAutoConfiguration)

@EventListener(classes = RemoteApplicationEvent.class)	public void acceptLocal(RemoteApplicationEvent event) {		if (this.serviceMatcher.isFromSelf(event)				&& !(event instanceof AckRemoteApplicationEvent)) {			this.cloudBusOutboundChannel.send(MessageBuilder.withPayload(event).build());		}	}

##每个client的MQ接收message

@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()));		}	}

##本地监听RefreshListener

public class RefreshListener		implements ApplicationListener
{ private static Log log = LogFactory.getLog(RefreshListener.class); private ContextRefresher contextRefresher; public RefreshListener(ContextRefresher contextRefresher) { this.contextRefresher = contextRefresher; } @Override public void onApplicationEvent(RefreshRemoteApplicationEvent event) { Set
keys = contextRefresher.refresh(); log.info("Received remote refresh request. Keys refreshed " + keys); }}

##docs

转载于:https://my.oschina.net/go4it/blog/799960

你可能感兴趣的文章
实现一个简单的编译器
查看>>
用应用封装来提高移动安全,这合适吗?
查看>>
NFV转发吞吐量达120GB 赛特斯要造中国NFV新格局
查看>>
细述 Java垃圾回收机制→Types of Java Garbage Collectors
查看>>
“趣冷淡”怎么看?网络电话寻求共同语言
查看>>
PaaS模式创新:实现应用可移植性
查看>>
《Servlet和JSP学习指南》一第2章 Session管理 2.1 网址重写
查看>>
可用性追踪是虚拟化网络的关键
查看>>
把软件架构演进体现在栈上
查看>>
Win10创意者更新刚发布 夜间模式就出BUG
查看>>
文化致胜的时代 协同OA可以这样助力企业文化建设
查看>>
OpenStack开源精神-让企业做到真正自主可控
查看>>
聊聊数据挖掘竞赛中的套路与深度学习的局限
查看>>
《Drupal实战》——1.4 常见配置
查看>>
谁说90后不靠谱?95后都做CEO了
查看>>
在移动安全领域,人工智能未来该扮演怎样的角色?
查看>>
《Oracle高性能SQL引擎剖析:SQL优化与调优机制详解》一2.2 内部函数与操作
查看>>
爱立信:SDN/NFV助力面向5G以及工业互联网的ICT转型
查看>>
工业云计算在中国工业领域的发展与应用趋势
查看>>
网络传播侵权认定启用“服务器标准”
查看>>