Skip to content

Commit

Permalink
optimize ApolloRefresher
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhom1314 committed Dec 13, 2023
1 parent df7db65 commit 032afc0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private DtpProperties() { }
private boolean enabledBanner = true;

/**
* Config file type.
* Config file type, for zookeeper and etcd.
*/
private String configType;

Expand Down Expand Up @@ -79,11 +79,6 @@ private DtpProperties() { }
*/
private List<NotifyPlatform> platforms;

/**
* Apollo config.
*/
private Apollo apollo;

/**
* Zookeeper config.
*/
Expand Down Expand Up @@ -168,12 +163,6 @@ public static DtpProperties getInstance() {
return Holder.INSTANCE;
}

@Data
public static class Apollo {

private String namespace;
}

@Data
public static class Zookeeper {

Expand Down
13 changes: 4 additions & 9 deletions example/example-apollo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ spring:
name: dynamic-tp-apollo-demo
profiles:
active: dev
dynamic:
tp:
tomcatTp: # tomcat webserver线程池配置
corePoolSize: 100
maximumPoolSize: 200
keepAliveTime: 60

logging:
level:
Expand All @@ -23,8 +17,9 @@ logging:
apollo:
bootstrap:
enabled: true
namespaces: dynamic-tp-apollo-demo-dtp.yml, application.properties
meta: http://localhost:8080
config-service: http://localhost:8080
eagerLoad:
enabled: true
namespaces: dynamic-tp-apollo-demo-dtp.yml,application
meta: http://81.68.181.139:8080
app:
id: dynamic-tp-apollo-demo
Binary file modified jvmti/jvmti-runtime/src/main/resources/libJniLibrary-x64.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void bindDtpProperties(Map<?, Object> properties, DtpProperties dtpProper
try {
Class.forName("org.springframework.boot.context.properties.bind.Binder");
doBindIn2X(properties, dtpProperties);
} catch (Exception e) {
} catch (ClassNotFoundException e) {
doBindIn1X(properties, dtpProperties);
}
}
Expand All @@ -62,7 +62,7 @@ public void bindDtpProperties(Environment environment, DtpProperties dtpProperti
try {
Class.forName("org.springframework.boot.context.properties.bind.Binder");
doBindIn2X(environment, dtpProperties);
} catch (Exception e) {
} catch (ClassNotFoundException e) {
doBindIn1X(environment, dtpProperties);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.dromara.dynamictp.common.em.ConfigFileTypeEnum;
import org.dromara.dynamictp.core.handler.ConfigHandler;
import org.dromara.dynamictp.core.refresher.AbstractRefresher;
import org.springframework.beans.factory.InitializingBean;

import java.io.IOException;
import java.util.List;

import static org.dromara.dynamictp.common.constant.DynamicTpConst.MAIN_PROPERTIES_PREFIX;

/**
* ApolloRefresher related
* <p>Listen for configuration file changes</p>
Expand All @@ -48,22 +53,24 @@ public class ApolloRefresher extends AbstractRefresher implements ConfigFileChan
public void onChange(ConfigFileChangeEvent changeEvent) {
String namespace = changeEvent.getNamespace();
String newValue = changeEvent.getNewValue();
ConfigFileFormat configFileFormat = determineFileFormat(namespace);
ConfigFileTypeEnum configFileType = ConfigFileTypeEnum.of(configFileFormat.getValue());
ConfigFileTypeEnum configFileType = deduceConfigFileType(namespace);
refresh(newValue, configFileType);
}

@Override
public void afterPropertiesSet() {
String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES,
ConfigConsts.NAMESPACE_APPLICATION);
log.debug("Apollo bootstrap namespaces: {}", namespaces);
List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);

for (String namespace : namespaceList) {
ConfigFileFormat format = determineFileFormat(namespace);
String actualNamespaceName = trimNamespaceFormat(namespace, format);
ConfigFile configFile = ConfigService
.getConfigFile(actualNamespaceName, format);
ConfigFile configFile = ConfigService.getConfigFile(actualNamespaceName, format);
if (!isDtpNamespace(configFile.getContent(), ConfigFileTypeEnum.of(format.getValue()))) {
continue;
}
try {
configFile.addChangeListener(this);
log.info("DynamicTp refresher, add listener success, namespace: {}", actualNamespaceName);
Expand All @@ -80,7 +87,6 @@ private ConfigFileFormat determineFileFormat(String namespaceName) {
return format;
}
}

return ConfigFileFormat.Properties;
}

Expand All @@ -89,8 +95,21 @@ private String trimNamespaceFormat(String namespaceName, ConfigFileFormat format
if (!namespaceName.toLowerCase().endsWith(extension)) {
return namespaceName;
}

return namespaceName.substring(0, namespaceName.length() - extension.length());
}

private ConfigFileTypeEnum deduceConfigFileType(String namespace) {
ConfigFileFormat configFileFormat = determineFileFormat(namespace);
return ConfigFileTypeEnum.of(configFileFormat.getValue());
}

private boolean isDtpNamespace(String content, ConfigFileTypeEnum configFileType) {
val configHandler = ConfigHandler.getInstance();
try {
val properties = configHandler.parseConfig(content, configFileType);
return properties.keySet().stream().anyMatch(key -> key.toString().startsWith(MAIN_PROPERTIES_PREFIX));
} catch (IOException e) {
return false;
}
}
}

0 comments on commit 032afc0

Please sign in to comment.