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;
}
}
还有其他的方案,欢迎提供 谢谢