Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ApolloConfigChangeListener 无效 #51

Open
Sam0913 opened this issue Jan 12, 2022 · 7 comments
Open

@ApolloConfigChangeListener 无效 #51

Sam0913 opened this issue Jan 12, 2022 · 7 comments

Comments

@Sam0913
Copy link

Sam0913 commented Jan 12, 2022

`
@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
Copy link
Contributor

Spring Boot version?

run

mvn dependency:tree > tree.log

get it.

@Sam0913
Copy link
Author

Sam0913 commented Jan 13, 2022

Spring Boot version?

run

mvn dependency:tree > tree.log

get it.

2.1.5.RELEASE

@Sam0913
Copy link
Author

Sam0913 commented Jan 13, 2022

Spring Boot version?

run

mvn dependency:tree > tree.log

get it.

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

@nobodyiam
Copy link
Member

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
Copy link
Author

Sam0913 commented Jan 17, 2022

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
Copy link
Member

nobodyiam commented Jan 19, 2022

恩,是我只写取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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants