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

@ApolloConfigChangeListener 无效 #51

Open Sam0913 opened 2 years ago

Sam0913 commented 2 years ago

` @Slf4j @Component public class ApolloConfig implements ApplicationContextAware {

private ApplicationContext applicationContext;

@ApolloConfigChangeListener
public void onChange(ConfigChangeEvent changeEvent){
    log.info("apollo conf update starts!");
    // 更新configrationProperties
    applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
    log.info("apollo conf update success!");
}

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

} `

这边能取到最新配置,但是使用@ConfigurationProperties(prefix = "spring.datasource")的配置还是久的值

Anilople commented 2 years ago

Spring Boot version?

run

mvn dependency:tree > tree.log

get it.

Sam0913 commented 2 years ago

Spring Boot version?

run

mvn dependency:tree > tree.log

get it.

2.1.5.RELEASE

Sam0913 commented 2 years ago

Spring Boot version?

run

mvn dependency:tree > tree.log

get it.

@RefreshScope 如果使用这种方式虽然可以动态更新了,但是会导致eureka 注册当前服务进行先down 在 up操作。

nobodyiam commented 2 years ago

You could refresh the specific bean with the bean name, e.g. https://github.com/apolloconfig/apollo/blob/96ee53a2c98da763ef0e7557af33c03a8d2ae297/apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/refresh/SpringBootApolloRefreshConfig.java#L47-L53

nobodyiam commented 2 years ago

BTW, I verified EnvironmentChangeEvent also works in spring boot 2.4.12 and spring cloud 2020.0.4. If it doesn't work for your scenario, please take a look at your ConfigurationProperties setup and make sure it is not annotated with RefreshScope.

Sam0913 commented 2 years ago

BTW, I verified EnvironmentChangeEvent also works in spring boot 2.4.12 and spring cloud 2020.0.4. If it doesn't work for your scenario, please take a look at your ConfigurationProperties setup and make sure it is not annotated with RefreshScope.

恩,是我只写取application错误了,请问@RefreshScope 会导致服务重启的问题您遇过吗?

nobodyiam commented 2 years ago

恩,是我只写取application错误了,请问@RefreshScope 会导致服务重启的问题您遇过吗?

@Sam0913 You need to refresh a specific bean instead of refresh all. But I think even refresh all will only trigger eureka to unregister and register itself but won't restart the service.