先来一张流程图:
Spring Cloud Bus 如何触发 Refresh?
注:配置中心里我是配置的mq发送消息
RefreshBusEndpoint中加了@Endpoint(
id = "bus-refresh"
)注解,他就会监控这个/actuator/bus-refresh这个路径
然后调用到了busRefresh方法,发布了RefreshRemoteApplicationEvent事件。
这个事件发布过程中会使用到ApplicationListenerMethodAdapter来处理,部分代码如下:
protected Object doInvoke(Object... args) {
Object bean = getTargetBean();
ReflectionUtils.makeAccessible(this.method);
try {
//反射执行的方法是BusAutoConfiguration#acceptLocal()
return this.method.invoke(bean, args);
}
public void onApplicationEvent(RefreshRemoteApplicationEvent event) {
Set<String> keys = this.contextRefresher.refresh();
log.info("Received remote refresh request. Keys refreshed " + keys);
}
public synchronized Set<String> refresh() {
Set<String> keys = refreshEnvironment();
////对应的bean刷新
this.scope.refreshAll();
return keys;
}
public synchronized Set<String> refreshEnvironment() {
//获取之前的配置
Map<String, Object> before = extract(
this.context.getEnvironment().getPropertySources());
//重新创建一个SpringApplication并获取新的配置
addConfigFilesToEnvironment();
//比较配置不同的key
Set<String> keys = changes(before,
extract(this.context.getEnvironment().getPropertySources())).keySet();
//发布配置改变事件
this.context.publishEvent(new EnvironmentChangeEvent(context, keys));
return keys;
}
@ManagedOperation
public void rebind() {
this.errors.clear();
for (String name : this.beans.getBeanNames()) {
//初始化bean
rebind(name);
}
}
public void refreshAll() {
//销毁bean
super.destroy();
this.context.publishEvent(new RefreshScopeRefreshedEvent());
}
原文:https://www.cnblogs.com/javammc/p/12853647.html