Apollo自动加载热更新

摘要

  • Apollo默认会对Value和ApolloJsonValue注解的数据进行热更新。
  • 其他数据需要通过ApplicationContext.publishEven刷新到应用中。
  • 当多个namespace时,优先使用先加载的数据,如namespaces: zone-dev,application,使用zone-dev中数据。
  • 方案一:通过ApolloConfigChangeListener监听指定namespace,无法满足根据分区配置namespace。
  • 方案二:通过Config.addChangeListener监听指定namespace,动态监听不同的namespace配置。

方案一

/**
* 监听apollo的namespace变动,当apollo发布时触发,获取变动的参数
* 支持同时监听多个namespace
* @author lizz
* @Date 2020-02-13 3:35 下午
*/
@ApolloConfigChangeListener("redis-config")                      
private void configChangeListter(ConfigChangeEvent changeEvent) {          
    logger.info("**************Apollo动态修改配置**************");               
    for (String changedKey : changeEvent.changedKeys()) {                  
        logger.info("changedKey :{}",changedKey);                          
        logger.info("changedValue :{}",changeEvent.getChange(changedKey)); 
    }                                                                      
    this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));                                
}                                                                          

方案二

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 热更新apollo配置,监听apollo中{apollo.bootstrap.namespaces}配置的namespaces
 * ConditionalOnClass,ConditionalOnProperty满足后进行加载
 * @author lizz
 * @Date 2020-04-13 3:35 下午
 */
@Configuration
@ConditionalOnClass(ConfigService.class)
@ConditionalOnProperty(prefix = ApolloConfigRefreshConfiguration.APOLLO_CONFIG_PREFIX, name = "enabled",
        havingValue = "true")
public class ApolloConfigRefreshConfiguration implements ApplicationContextAware {
    private static final Logger logger = LoggerFactory.getLogger(ApolloConfigRefreshConfiguration.class);

    public static final String APOLLO_CONFIG_PREFIX = "apollo.bootstrap";
    /**
     * spring控制器
     */
    private ApplicationContext applicationContext;

    @Value("${apollo.bootstrap.namespaces}")
    private String[] namespaces;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 启动时增加监听
     */
    @PostConstruct
    public void addRefreshListener() {
        for (String namespace : namespaces) {
            Config config = ConfigService.getConfig(namespace);
            //对namespace增加监听方法
            config.addChangeListener(changeEvent -> {
                for (String key : changeEvent.changedKeys()) {
                    logger.info("**************刷新Apollo配置:{}**************", changeEvent.getChange(key));
                }
                //将变动的配置刷新到应用中
                this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
            });
        }
    }

}

 

 

 

你可能感兴趣的:(配置中心,SpringCloud)