From 41d6eeca86c830989a394833e8b11e655060806b Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Wed, 28 May 2025 19:13:10 +0800 Subject: [PATCH] feat:support config empty protection. (#1587) Co-authored-by: Haotian Zhang --- CHANGELOG.md | 1 + .../polaris/config/ConfigurationModifier.java | 11 ++++--- .../config/PolarisConfigProperties.java | 29 +++++++++++++++++++ spring-cloud-tencent-dependencies/pom.xml | 2 +- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd437bcff..15f3df0b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,4 @@ - feat:support spring cloud 2024. - [fix: add gateway context config example.](https://github.com/Tencent/spring-cloud-tencent/pull/1584) +- [feat:support config empty protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1587) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java index efa425b9d..dd7ad73c9 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -80,11 +80,12 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier } private void initDataSource(ConfigurationImpl configuration) { + ConnectorConfigImpl connectorConfig = configuration.getConfigFile().getServerConnector(); // set connector type - configuration.getConfigFile().getServerConnector().setConnectorType(polarisConfigProperties.getDataSource()); + connectorConfig.setConnectorType(polarisConfigProperties.getDataSource()); if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), LOCAL_FILE_CONNECTOR_TYPE)) { String localFileRootPath = polarisConfigProperties.getLocalFileRootPath(); - configuration.getConfigFile().getServerConnector().setPersistDir(localFileRootPath); + connectorConfig.setPersistDir(localFileRootPath); LOGGER.info("[SCT] Run spring cloud tencent config with local data source. localFileRootPath = {}", localFileRootPath); return; } @@ -110,13 +111,15 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier checkAddressAccessible(configAddresses); } - configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); + connectorConfig.setAddresses(configAddresses); if (StringUtils.isNotEmpty(polarisConfigProperties.getToken())) { - ConnectorConfigImpl connectorConfig = configuration.getConfigFile().getServerConnector(); connectorConfig.setToken(polarisConfigProperties.getToken()); } + connectorConfig.setEmptyProtectionEnable(polarisConfigProperties.isEmptyProtectionEnabled()); + connectorConfig.setEmptyProtectionExpiredInterval(polarisConfigProperties.getEmptyProtectionExpiredInterval()); + LOGGER.info("[SCT] Run spring cloud tencent config in polaris data source."); } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index 2b330ea19..8c8341216 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -100,6 +100,17 @@ public class PolarisConfigProperties { */ private boolean checkAddress = true; + /** + * if empty protection is enabled. + */ + private boolean emptyProtectionEnabled = true; + + /** + * interval of empty protection. 7 days by default. + */ + private long emptyProtectionExpiredInterval = 7 * 24 * 3600 * 1000L; + + public boolean isEnabled() { return enabled; } @@ -204,6 +215,22 @@ public class PolarisConfigProperties { this.checkAddress = checkAddress; } + public boolean isEmptyProtectionEnabled() { + return emptyProtectionEnabled; + } + + public void setEmptyProtectionEnabled(boolean emptyProtectionEnabled) { + this.emptyProtectionEnabled = emptyProtectionEnabled; + } + + public long getEmptyProtectionExpiredInterval() { + return emptyProtectionExpiredInterval; + } + + public void setEmptyProtectionExpiredInterval(long emptyProtectionExpiredInterval) { + this.emptyProtectionExpiredInterval = emptyProtectionExpiredInterval; + } + @Override public String toString() { return "PolarisConfigProperties{" + @@ -220,6 +247,8 @@ public class PolarisConfigProperties { ", localFileRootPath='" + localFileRootPath + '\'' + ", internalEnabled=" + internalEnabled + ", checkAddress=" + checkAddress + + ", emptyProtectionEnabled=" + emptyProtectionEnabled + + ", emptyProtectionExpiredInterval=" + emptyProtectionExpiredInterval + '}'; } } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 106937e37..65e6b404c 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -74,7 +74,7 @@ 2.0.2.0-2024.0.1-SNAPSHOT - 2.0.1.0 + 2.0.2.0-SNAPSHOT 2.2.0