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

Spring3.x集成Apollo后.无法监听到配置的变化 #2501

Closed
tanrui988 opened this issue Aug 7, 2019 · 7 comments
Closed

Spring3.x集成Apollo后.无法监听到配置的变化 #2501

tanrui988 opened this issue Aug 7, 2019 · 7 comments
Labels

Comments

@tanrui988
Copy link

已经增加了监听,在项目运行中,发现值还是没有变化. 是不是还需要做其他的配置?
Config apolloConfig = ConfigService.getAppConfig();

apolloConfig.addChangeListener(new ConfigChangeListener() {
@OverRide
public void onChange(ConfigChangeEvent configChangeEvent) {
for (String key : configChangeEvent.changedKeys()) {
ConfigChange change = configChangeEvent.getChange(key);
System.setProperty(String.valueOf(key), String.valueOf(change.getNewValue()));
}
}
});

@nobodyiam
Copy link
Member

先打开客户端的debug日志看看

@tanrui988
Copy link
Author

tanrui988 commented Aug 8, 2019

@nobodyiam 大佬. 我看了下源码发现点问题,想跟您确认下. config通过长连接获取配置然后与DefaultConfig.m_configProperties里做比对.比较出有差异的配置,然后再去调用自定义的ConfigChangeListener. 在方法DefaultConfig.updateAndCalcConfigChanges的177行里有一段代码:
change.setNewValue(this.getProperty(change.getPropertyName(), change.getNewValue()));
这里是设置新的值给change对象. 但是这个getProperty的获取方式有点问题.他的第一步居然是获取System.getProperty(key)的值. 然后才获取m_configProperties的值. 实际上m_configProperties对象里是在前面几行代码里更新的,肯定是最新的数据. 但是这里去先获取了System里的值. 由于我们项目是比较旧的,所以在工程启动的时候会将所有的配置信息全部加载到System里. 所以就导致这一步始终获取到的是旧值. 导致后面change.setNewValue时覆盖了新的值.后面比对值时肯定相同了. 从而导致了我的ConfigChangeListener没有生效.
实际上ConfigChange对象里已经存在了修改前和修改后的值.直接用即可.为何还要更新?不太理解.
请帮忙看看我的理解对否? 谢谢解答!

@nobodyiam
Copy link
Member

这个是设计的行为,system property的配置优先级最高

@tanrui988
Copy link
Author

tanrui988 commented Aug 16, 2019

如果是这样的话,那我们启动的时候就不能向system property赋值了. 这样就等于在system property存了旧值. 会导致在Apollo Protal里修改了配置后无法监听到了.
另外请问, 按照您的设计, system property 的值来自于哪里? 怎么设置的?在配置变化的时候,变更的值会先更新到system property ?
谢谢解答

@nobodyiam
Copy link
Member

system property一般认为是用于单机临时调整的配置,比如-D参数

@stale
Copy link

stale bot commented Nov 28, 2019

This issue has been automatically marked as stale because it has not had activity in the last 90 days. It will be closed in 14 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 28, 2019
@stale
Copy link

stale bot commented Dec 12, 2019

This issue has been automatically closed because it has not had activity in the last 14 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". Thank you for your contributions.

@stale stale bot closed this as completed Dec 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants