SpringCloud Config自动刷新配置源码分析

一、配置中心

先来一张流程图:

Spring Cloud Bus 如何触发 Refresh?
注:配置中心里我是配置的mq发送消息

  1. RefreshBusEndpoint中加了@Endpoint(
    id = "bus-refresh"
    )注解,他就会监控这个/actuator/bus-refresh这个路径

  2. 然后调用到了busRefresh方法,发布了RefreshRemoteApplicationEvent事件。

  3. 这个事件发布过程中会使用到ApplicationListenerMethodAdapter来处理,部分代码如下:

protected Object doInvoke(Object... args) {
		Object bean = getTargetBean();
		ReflectionUtils.makeAccessible(this.method);
		try {
		//反射执行的方法是BusAutoConfiguration#acceptLocal()
			return this.method.invoke(bean, args);
		}
  1. 反射执行方法会发送mq消息,然后由各个配置中心客户端接收,接受方法为BusAutoConfiguration#acceptRemote
  2. 这个方法会发布RefreshRemoteApplicationEvent事件
  3. 这个事件由RefreshListener负责接收,然后去处理刷新配置
public void onApplicationEvent(RefreshRemoteApplicationEvent event) {
        Set<String> keys = this.contextRefresher.refresh();
        log.info("Received remote refresh request. Keys refreshed " + keys);
    }
  1. 监听触发后,调用到ContextRefresher的refresh()方法
public synchronized Set<String> refresh() {
		Set<String> keys = refreshEnvironment();
		////对应的bean刷新
		this.scope.refreshAll();
		return keys;
	}
  1. 先看 refreshEnvironment()方法
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;
	}
  1. EnvironmentChangeEvent事件由ConfigurationPropertiesRebinder监听,监听到之后会调用rebind方法
@ManagedOperation
	public void rebind() {
		this.errors.clear();
		for (String name : this.beans.getBeanNames()) {
		    //初始化bean
			rebind(name);
		}
	}
  1. 再点到this.scope.refreshAll()方法
public void refreshAll() {
        //销毁bean
		super.destroy();
		this.context.publishEvent(new RefreshScopeRefreshedEvent());
	}
  1. RefreshScopeRefreshedEvent事件由EurekaClientConfigurationRefresher监听,进行对eureka服务器重连的操作。
原文地址:https://www.cnblogs.com/javammc/p/12853647.html