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服务器重连的操作。

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...