Closed xuexiaoao closed 5 years ago
spring cloud动态刷新日志为什么还要加一个PostConstruct,只是监听logging.level.配置不就可以了吗
public class LoggerLevelRefresher implements ApplicationContextAware { private ApplicationContext applicationContext; @ApolloConfig private Config config; @PostConstruct private void initialize() { refreshLoggingLevels(config.getPropertyNames()); } @ApolloConfigChangeListener(interestedKeyPrefixes = {"logging.level."}) private void onChange(ConfigChangeEvent changeEvent) { refreshLoggingLevels(changeEvent.changedKeys()); } private void refreshLoggingLevels(Set<String> changedKeys) { System.out.println("Refreshing logging levels"); /** * refresh logging levels * @see org.springframework.cloud.logging.LoggingRebinder#onApplicationEvent */ this.applicationContext.publishEvent(new EnvironmentChangeEvent(changedKeys)); System.out.println("Logging levels refreshed"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
这个样例是logging先初始化,后加载apollo的例子,所以加载完apollo需要刷新一下。
如果使用apollo的eagerLoad,那么post contruct就不需要刷新了
这个样例是logging先初始化,后加载apollo的例子,所以加载完apollo需要刷新一下。 如果使用apollo的eagerLoad,那么post contruct就不需要刷新了
了解了
spring cloud动态刷新日志为什么还要加一个PostConstruct,只是监听logging.level.配置不就可以了吗