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

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

Loading…
Cancel
Save