SpringBoot——》Apollo客户端监听配置变化、动态刷新

74 篇文章 6 订阅
19 篇文章 1 订阅

@Value ,动态刷新
@ConfigurationProperties , 需要添加apollo配置监听器 @ApolloConfigChangeListener 实现动态刷新


参考:SpringBoot——》学习使用Apollo配置中心

一、配置

1、pom.xml

<dependency>
     <groupId>com.ctrip.framework.apollo</groupId>
     <artifactId>apollo-client</artifactId>
     <version>1.6.0</version>
 </dependency>

2.application.yml

app:
    id: 5ff576fef194fa22505f4331
apollo:
    cluster: default
    meta: http://dev.apollo.ejuops.com:8080,http://dev.apollo.ejuops.com:8081
    cacheDir: /opt/app/data
    bootstrap:
        enabled: true
        eagerLoad:
            enabled: true
        namespaces: application,.mysql-config

3、@EnableApolloConfig

@EnableApolloConfig 不一定要加在启动类上,加在被spring管理的类上即可

二、属性映射

1、@Value:直接属性映射

@Value ,动态刷新

@Value("${user.userName}")
private String userName;

2、@ConfigurationProperties:bean属性映射

@ConfigurationProperties , 需要添加apollo配置监听器 @ApolloConfigChangeListener 实现动态刷新

  • @ApolloConfigChangeListener():默认监听的是application命名空间
  • @ApolloConfigChangeListener(“mysql-config”):指定mysql-config命名空间
  • @ApolloConfigChangeListener("${apollo.bootstrap.namespaces}"):指定参数所配置的命名空间(多个用逗号分隔)
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author Created by xiaoxian on 2021/1/18.
 * @version v0.1.0
 * 自动刷新 ConfigurationProperties 标注的类属性
 */
@Slf4j
@Component
public class ApolloConfigChanged implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    RefreshScope refreshScope;

    /**
     * 刷新的namespace的名字:devGroup.coupon.application
     * apollo自定义的几个namespace:{@link com.ctrip.framework.apollo.core.ConfigConsts}
     * 已重写不需要配置value,默认所有的namespaces
     *
     * @param changeEvent
     */
    @ApolloConfigChangeListener("${apollo.bootstrap.namespaces}")
    private void someChangeHandler(ConfigChangeEvent changeEvent) {
        log.info("================Apollo 自动刷新值 开始 ===========================");

        for (String changedKey : changeEvent.changedKeys()) {

            ConfigChange configChange = changeEvent.getChange(changedKey);
            String oldValue = configChange.getOldValue();
            String newValue = configChange.getNewValue();
            log.info("【changedKey:{},oldValue={}, newValue:{}】", changedKey, oldValue, newValue);
        }

        refreshProperties(changeEvent);

        log.info("================Apollo 自动刷新值 结束 ===========================");
    }

    public void refreshProperties(ConfigChangeEvent changeEvent) {
        // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
        refreshScope.refreshAll();
    }

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

也可以用下面这种方式:

Config config = ConfigService.getAppConfig();
config.addChangeListener(new ConfigChangeListener() {
  @Override
  public void onChange(ConfigChangeEvent changeEvent) {
    for (String key : changeEvent.changedKeys()) {
      ConfigChange change = changeEvent.getChange(key);
      System.out.println(String.format(
        "Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s",
        change.getPropertyName(), change.getOldValue(),
        change.getNewValue(), change.getChangeType()));
     }
  }
});
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值