From 3fc56d404ea4b911b93b7491dcf9c715c43cb13d Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 9 Oct 2023 21:39:42 +0800 Subject: [PATCH] fix:fix system env variable read bug. --- CHANGELOG.md | 1 + README-zh.md | 2 +- README.md | 2 +- pom.xml | 2 +- .../PolarisConfigChangeEventListener.java | 20 +++++++++++++++++-- spring-cloud-tencent-dependencies/pom.xml | 2 +- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e32d607e6..d81acfc18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,3 +28,4 @@ - [fix:upgrade spring version.](https://github.com/Tencent/spring-cloud-tencent/pull/1087) - [fix:Update README-zh.md](https://github.com/Tencent/spring-cloud-tencent/pull/1093). - [feature: support Polaris configuration center extension plugin interface and support dynamic modification of log levels.](https://github.com/Tencent/spring-cloud-tencent/pull/1104). +- fix:fix system env variable read bug. diff --git a/README-zh.md b/README-zh.md index eacc4f860..57bc7003d 100644 --- a/README-zh.md +++ b/README-zh.md @@ -78,7 +78,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 com.tencent.cloud spring-cloud-tencent-dependencies - 1.12.2-2020.0.6 + 1.12.3-2020.0.6 pom import diff --git a/README.md b/README.md index 1b7aacce2..d8faebece 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ For example: com.tencent.cloud spring-cloud-tencent-dependencies - 1.12.2-2020.0.6 + 1.12.3-2020.0.6 pom import diff --git a/pom.xml b/pom.xml index 9aa13a150..c8eb11a34 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ - 1.12.2-2020.0.6 + 1.12.3-2020.0.6 5.3.25 diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java index d472f0a97..330e62fb0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java @@ -26,6 +26,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import com.google.common.collect.Maps; import com.tencent.cloud.polaris.config.spring.event.ConfigChangeSpringEvent; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; @@ -37,6 +39,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.StandardEnvironment; import org.springframework.lang.NonNull; import static com.tencent.cloud.polaris.config.listener.PolarisConfigListenerContext.fireConfigChange; @@ -50,6 +53,8 @@ import static com.tencent.cloud.polaris.config.listener.PolarisConfigListenerCon */ public final class PolarisConfigChangeEventListener implements ApplicationListener, ApplicationEventPublisherAware { + private static final Logger LOG = LoggerFactory.getLogger(PolarisConfigChangeEventListener.class); + private static final AtomicBoolean started = new AtomicBoolean(); private ApplicationEventPublisher eventPublisher; @@ -96,12 +101,23 @@ public final class PolarisConfigChangeEventListener implements ApplicationListen Map ret = Maps.newHashMap(); MutablePropertySources sources = environment.getPropertySources(); sources.iterator().forEachRemaining(propertySource -> { + // Don't read system env variable. + if (StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME.equals(propertySource.getName())) { + return; + } + Object o = propertySource.getSource(); if (o instanceof Map) { for (Map.Entry entry : ((Map) o).entrySet()) { String key = entry.getKey(); - String value = environment.getProperty(key); - ret.put(key, value); + try { + String value = environment.getProperty(key); + ret.put(key, value); + } + catch (Exception e) { + LOG.warn("Read property from {} with key {} failed.", propertySource.getName(), key, e); + } + } } else if (o instanceof Collection) { diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index f9de26470..19d60905d 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.12.2-2020.0.6 + 1.12.3-2020.0.6 1.14.1