Apollo配置中心动态的刷新和获取配置

1、某些简单的属性可以直接使用@Value注解的配置会自动刷新配置(非bean) 

@Value("config.test")
private Strign test;

2、bean管理(或者说复杂的配置各种配置)加一个配置类即可

@Component("systemConfig")
@ConfigurationProperties(prefix = "cword")
@RefreshScope
@EnableApolloConfig("cword")
public class SystemConfig {
 
    private Resource filePath;
    private Resource tempFilePath;
 
    @Autowired
    private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope;
    @ApolloConfigChangeListener("cword")
    private void configChange(ConfigChangeEvent changeEvent){
        refreshScope.refresh("systemConfig");
    }
}

 

3、基于EnvironmentChangeEvent实现刷新

利用spring的事件驱动配合@ApolloConfigChangeListener监听实现bean的动态刷新,其代码如下

@Component
@Slf4j
public class UserPropertiesRefresh implements ApplicationContextAware {

 private ApplicationContext applicationContext;

 @ApolloConfigChangeListener(value="user.properties",interestedKeyPrefixes = {"user."})
 private void refresh(ConfigChangeEvent changeEvent){
 applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));

 PrintChangeKeyUtils.printChange(changeEvent);
 }

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

 

还有其他的方案,欢迎提供     谢谢

其他配置可以参考:https://blog.csdn.net/Iisongyu/article/details/104215802?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.edu_weight

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值