配置中心数据的监听
在章节 配置中心数据的获取-上、配置中心数据的获取-下 讲解了如何从配置中心来读取数据,而本章节将详解讲解如何对配置中心进行监听
ConfigEventHandler
@AllArgsConstructor
@Slf4j
public class ConfigEventHandler implements ApplicationListener<ConfigEvent> {
private final MetaDataOperation metaDataOperation;
private final RemoteConfigRuleOperation remoteConfigRuleOperation;
@Override
public void onApplicationEvent(ConfigEvent event) {
getConfig();
remoteConfigRuleOperation.listenConfig();
}
//省略... ...
}
getConfig() 方法是从配置中心读取数据的,而 remoteConfigRuleOperation.listenConfig() 方法就是用来监听数据的
public interface RemoteConfigRuleOperation {
/**
* 监听配置
* */
void listenConfig();
/**
* 得到配置
* @param generalConfig 配置标识
* @return 结果
* @throws Exception 异常
* */
String getConfig(boolean generalConfig) throws Exception;
}
同样 Nacos 和 Apollo 配置中心的具体监听方式是不同的,依旧要分层设计