先来一张流程图:
服务器
Spring Cloud Bus 如何触发 Refresh?
注:配置中内心我是配置的mq发送消息this
RefreshBusEndpoint中加了@Endpoint(
id = "bus-refresh"
)注解,他就会监控这个/actuator/bus-refresh这个路径code
而后调用到了busRefresh方法,发布了RefreshRemoteApplicationEvent事件。blog
这个事件发布过程当中会使用到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()); }