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()));
            });
        }
    }

}

 

 

 

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lizz666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值