Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dyx1234 committed Oct 27, 2024
1 parent 82d61d7 commit 2f5ddf6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 41 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ Apollo Java 2.4.0

------------------

* [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79)
* [Fix the Cannot enhance @Configuration bean definition issue](https://github.com/apolloconfig/apollo-java/pull/82)
* [Feature openapi query namespace support not fill item](https://github.com/apolloconfig/apollo-java/pull/83)
* [Add more observability in apollo config client](https://github.com/apolloconfig/apollo-java/pull/74)
* [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79)


------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1)
1 change: 1 addition & 0 deletions apollo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,6 @@ public class K8sConfigMapConfigRepository extends AbstractConfigRepository
private volatile ConfigRepository upstream;
private volatile ConfigSourceType sourceType = ConfigSourceType.CONFIGMAP;

/**
* Constructor
*
* @param namespace the namespace
*/
public K8sConfigMapConfigRepository(String namespace) {
this(namespace, (ConfigRepository) null);
}

public K8sConfigMapConfigRepository(String namespace, KubernetesManager kubernetesManager) {
this.namespace = namespace;
configUtil = ApolloInjector.getInstance(ConfigUtil.class);
k8sNamespace = configUtil.getK8sNamespace();
this.kubernetesManager = kubernetesManager;

this.setConfigMapKey(configUtil.getCluster(), namespace);
this.setConfigMapName(configUtil.getAppId(), false);
this.setUpstreamRepository(upstream);
}

public K8sConfigMapConfigRepository(String namespace, ConfigRepository upstream) {
this.namespace = namespace;
Expand All @@ -85,27 +66,22 @@ public K8sConfigMapConfigRepository(String namespace, ConfigRepository upstream)
this.setUpstreamRepository(upstream);
}

public String getConfigMapKey() {
return configMapKey;
}

public String getConfigMapName() {
String getConfigMapName() {
return configMapName;
}

void setConfigMapKey(String cluster, String namespace) {
// cluster: 用户定义>idc>default
// cluster: User Definition >idc>default
if (StringUtils.isBlank(cluster)) {
configMapKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join("default", namespace);
configMapKey = Joiner.on(ConfigConsts.CONFIGMAP_KEY_SEPARATOR).join("default", namespace);
return;
}
configMapKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(cluster, namespace);
configMapKey = Joiner.on(ConfigConsts.CONFIGMAP_KEY_SEPARATOR).join(cluster, namespace);
}

void setConfigMapName(String appId, boolean syncImmediately) {
Preconditions.checkNotNull(appId, "AppId cannot be null");
configMapName = ConfigConsts.APOLLO_CONFIG_CACHE + appId;
// 初始化configmap
this.checkConfigMapName(configMapName);
if (syncImmediately) {
this.sync();
Expand Down Expand Up @@ -141,7 +117,6 @@ public Properties getConfig() {
}
Properties result = propertiesFactory.getPropertiesInstance();
result.putAll(configMapProperties);
logger.info("configmap properties: {}", configMapProperties);
return result;
}

Expand All @@ -152,7 +127,6 @@ public Properties getConfig() {
*/
@Override
public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) {
// 设置上游数据源
if (upstreamConfigRepository == null) {
return;
}
Expand Down Expand Up @@ -191,7 +165,6 @@ protected void sync() {
Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
transaction.setStatus(ex);
exception = ex;
throw new ApolloConfigException("Load config from Kubernetes ConfigMap failed!", ex);
} finally {
transaction.complete();
}
Expand All @@ -210,7 +183,7 @@ public Properties loadFromK8sConfigMap() {
String jsonConfig = kubernetesManager.getValueFromConfigMap(k8sNamespace, configMapName, configMapKey);
if (jsonConfig == null) {
// TODO 这样重试访问idc,default是否正确
String retryKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join("default", namespace);
String retryKey = Joiner.on(ConfigConsts.CONFIGMAP_KEY_SEPARATOR).join("default", namespace);
jsonConfig = kubernetesManager.getValueFromConfigMap(k8sNamespace, configMapName, retryKey);
}

Expand All @@ -235,7 +208,6 @@ public boolean trySyncFromUpstream() {
return false;
}
try {
logger.info("Start sync from the upstream data source, upstream.getConfig:{}, upstream.getSourceType():{}", upstream.getConfig(), upstream.getSourceType());
updateConfigMapProperties(upstream.getConfig(), upstream.getSourceType());
return true;
} catch (Throwable ex) {
Expand All @@ -248,7 +220,7 @@ public boolean trySyncFromUpstream() {

private synchronized void updateConfigMapProperties(Properties newProperties, ConfigSourceType sourceType) {
this.sourceType = sourceType;
if (newProperties.equals(configMapProperties)) {
if (newProperties == null || newProperties.equals(configMapProperties)) {
return;
}
this.configMapProperties = newProperties;
Expand All @@ -263,7 +235,7 @@ private synchronized void updateConfigMapProperties(Properties newProperties, Co
*/
@Override
public void onRepositoryChange(String namespace, Properties newProperties) {
if (newProperties.equals(configMapProperties)) {
if (newProperties == null || newProperties.equals(configMapProperties)) {
return;
}
Properties newFileProperties = propertiesFactory.getPropertiesInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.models.*;
import io.kubernetes.client.util.Config;
import org.slf4j.Logger;
Expand All @@ -41,7 +40,6 @@ public class KubernetesManager {
public KubernetesManager() {
try {
client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
coreV1Api = new CoreV1Api(client);
} catch (Exception e) {
String errorMessage = "Failed to initialize Kubernetes client: " + e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@
import com.ctrip.framework.apollo.PropertiesCompatibleConfigFile;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.*;
import com.ctrip.framework.apollo.internals.ConfigRepository;
import com.ctrip.framework.apollo.internals.DefaultConfig;
import com.ctrip.framework.apollo.internals.JsonConfigFile;
import com.ctrip.framework.apollo.internals.LocalFileConfigRepository;
import com.ctrip.framework.apollo.internals.PropertiesCompatibleFileConfigRepository;
import com.ctrip.framework.apollo.internals.PropertiesConfigFile;
import com.ctrip.framework.apollo.internals.RemoteConfigRepository;
import com.ctrip.framework.apollo.internals.TxtConfigFile;
import com.ctrip.framework.apollo.internals.XmlConfigFile;
import com.ctrip.framework.apollo.internals.YamlConfigFile;
import com.ctrip.framework.apollo.internals.YmlConfigFile;
import com.ctrip.framework.apollo.internals.K8sConfigMapConfigRepository;
import com.ctrip.framework.apollo.util.ConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void setUp() {
kubernetesManager = new KubernetesManager(coreV1Api);
MockInjector.setInstance(KubernetesManager.class, kubernetesManager);

configmapRepo = new K8sConfigMapConfigRepository(someNamespace, kubernetesManager);
//configmapRepo = new K8sConfigMapConfigRepository(someNamespace, kubernetesManager);
MockInjector.setInstance(K8sConfigMapConfigRepository.class, configmapRepo);

PropertiesFactory propertiesFactory = mock(PropertiesFactory.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
public interface ConfigConsts {
String NAMESPACE_APPLICATION = "application";
String CLUSTER_NAME_DEFAULT = "default";
String CLUSTER_NAMESPACE_SEPARATOR = "-";
String CLUSTER_NAMESPACE_SEPARATOR = "+";
String CONFIGMAP_KEY_SEPARATOR = "-";
String APOLLO_CONFIG_CACHE = "apollo-configcache-";
String APOLLO_CLUSTER_KEY = "apollo.cluster";
String APOLLO_META_KEY = "apollo.meta";
Expand Down

0 comments on commit 2f5ddf6

Please sign in to comment.