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

为什么ssm项目中使用@ApolloConfigChangeListener不能实时收到更新配置变化,而springboot项目可以呢 #38

Open
ls417316172 opened this issue Dec 4, 2020 · 9 comments

Comments

@ls417316172
Copy link

/**
 * 监听apollo中配置global 文件的变化来维护map集合
 * @param changeEvent
 */
@ApolloConfigChangeListener("global")
public void onChangeGlobal(ConfigChangeEvent changeEvent) {
	for (String key : changeEvent.changedKeys()) {
		ConfigChange change = changeEvent.getChange(key);
		System.err.println("==========global:"+key);
		map.put(key, change.getNewValue());
	}
}
@nobodyiam
Copy link
Member

参考下 apollo-demo

@ls417316172
Copy link
Author

@nobodyiam 我将apollo-demo中的关于实时监听代码放到spring 的项目,还是不能实时接收到变化更新的推送变更,只能在源码设计的每5分钟轮询拉取的时候才能获取到,请您帮忙解答,谢谢

@klboke
Copy link
Contributor

klboke commented Dec 7, 2020

@ls417316172 监听器所在的bean需要被spring管理才行,你检查下是否没有被spring管理

@ls417316172
Copy link
Author

@klboke 如下是我写的监听器代码:

@lazy(false)
@component
public class ConfigTest {

@ApolloConfigChangeListener
public void onChangeGlobal(ConfigChangeEvent changeEvent) {
    for (String key : changeEvent.changedKeys()) {
        ConfigChange change = changeEvent.getChange(key);
        System.err.println("==========application:" + key);

    }
}

}
我验证从ApplicationContext中可以获取监听器类,但是,配置管理页面对 其中的key修改时,监听器不能实时获取到,只能在过5分钟才能获取到,请你给解释,我同样的代码写在springboot的项目就可以实时获取到

@klboke
Copy link
Contributor

klboke commented Dec 7, 2020

确认在spring里有ConfigTest这个实例了么,我这边是可以实时生效的

@ls417316172
Copy link
Author

@klboke 发您的代码段我看看呢

@ls417316172
Copy link
Author

@klboke 以下是获取ConfigTest实力对象的验证代码(随便写了个测试类):

@RestController
@RequestMapping("/a/sys/register")
public class testConfigTest implements ApplicationContextAware {
private ApplicationContext applicationContext;

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

/**
 * 测试是否在spring容器中
 */
@RequestMapping("123")
public void test123() {
    System.out.println("application容器实体类:"+applicationContext.getBean(ConfigTest.class));
}

}
结果:application容器实体类:com.datangwealth.common.config.ConfigTest@714824d6
您看一下是否有问题,谢谢

@ls417316172
Copy link
Author

@klboke 我的微信号就是我的github账号(ls417316172),如果您有时间我希望加您一下微信,进行更深层次的学习和验证,谢谢

@klboke
Copy link
Contributor

klboke commented Dec 7, 2020

我用的是apollo项目里的demo测试的

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