Fix Apollo configuration change delay (#267)

pull/275/head
chen.ma 3 years ago
parent a0c72defcb
commit b260c67ca7

@ -17,6 +17,8 @@
package cn.hippo4j.common.api;
import java.util.Map;
/**
* Thread-pool dynamic refresh.
*/
@ -28,4 +30,13 @@ public interface ThreadPoolDynamicRefresh {
* @param content
*/
void dynamicRefresh(String content);
/**
* Dynamic refresh.
*
* @param content
* @param newValueChangeMap
*/
default void dynamicRefresh(String content, Map<String, Object> newValueChangeMap) {
}
}

@ -19,6 +19,7 @@ package cn.hippo4j.core.springboot.starter.refresher;
import cn.hippo4j.common.api.ThreadPoolDynamicRefresh;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.springboot.starter.parser.ConfigParserHandler;
@ -28,6 +29,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
/**
@ -50,8 +52,15 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool
@Override
public void dynamicRefresh(String configContent) {
dynamicRefresh(configContent, null);
}
public void dynamicRefresh(String configContent, Map<String, Object> newValueChangeMap) {
try {
Map<Object, Object> configInfo = ConfigParserHandler.getInstance().parseConfig(configContent, bootstrapCoreProperties.getConfigFileType());
if (CollectionUtil.isNotEmpty(newValueChangeMap)) {
Optional.ofNullable(configInfo).ifPresent(each -> each.putAll(newValueChangeMap));
}
BootstrapCoreProperties bindableCoreProperties = BootstrapCorePropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapCoreProperties);
ApplicationContextHolder.getInstance().publishEvent(new Hippo4jCoreDynamicRefreshEvent(this, bindableCoreProperties));
} catch (Exception ex) {

@ -22,9 +22,15 @@ import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import java.util.Map;
import static cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties.PREFIX;
/**
* @author : wh
* @date : 2022/2/28 21:32
@ -42,13 +48,18 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
public void afterPropertiesSet() {
String[] apolloNamespaces = this.namespace.split(",");
this.namespace = apolloNamespaces[0];
Config config = ConfigService.getConfig(namespace);
Config config = ConfigService.getConfig(String.format("%s.%s", namespace, bootstrapCoreProperties.getConfigFileType().getValue()));
ConfigChangeListener configChangeListener = configChangeEvent -> {
ConfigFile configFile = ConfigService.getConfigFile(
this.namespace.replaceAll("." + bootstrapCoreProperties.getConfigFileType().getValue(), ""),
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue()));
String configInfo = configFile.getContent();
dynamicRefresh(configInfo);
String namespace = this.namespace.replaceAll("." + bootstrapCoreProperties.getConfigFileType().getValue(), "");
ConfigFileFormat configFileFormat = ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue());
ConfigFile configFile = ConfigService.getConfigFile(namespace, configFileFormat);
Map<String, Object> newChangeValueMap = Maps.newHashMap();
configChangeEvent.changedKeys().stream().filter(each -> each.contains(PREFIX)).forEach(each -> {
ConfigChange change = configChangeEvent.getChange(each);
String newValue = change.getNewValue();
newChangeValueMap.put(each, newValue);
});
dynamicRefresh(configFile.getContent(), newChangeValueMap);
};
config.addChangeListener(configChangeListener);
log.info("dynamic-thread-pool refresher, add apollo listener success, namespace: {}", namespace);

Loading…
Cancel
Save