apolloconfig / apollo-use-cases

Show various usage scenarios and sample codes of the Apollo configuration center, welcome to share more configuration use cases in your daily work!
Apache License 2.0
945 stars 401 forks source link

动态刷新日志疑问 #32

Closed xuexiaoao closed 5 years ago

xuexiaoao commented 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;
  }
}
nobodyiam commented 5 years ago

这个样例是logging先初始化,后加载apollo的例子,所以加载完apollo需要刷新一下。

如果使用apollo的eagerLoad,那么post contruct就不需要刷新了

xuexiaoao commented 5 years ago

这个样例是logging先初始化,后加载apollo的例子,所以加载完apollo需要刷新一下。

如果使用apollo的eagerLoad,那么post contruct就不需要刷新了

了解了