diff --git a/CHANGELOG.md b/CHANGELOG.md index 60a1af3c5..e8ab7bbf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,3 +11,4 @@ - [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/634) - [feat:Transfer http headers specified by environment variables](https://github.com/Tencent/spring-cloud-tencent/pull/637) - [feat:support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/641) +- [feat:support read config file from local file system.](https://github.com/Tencent/spring-cloud-tencent/pull/643) 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 d505d3788..7912787cc 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 @@ -42,6 +42,9 @@ import org.springframework.util.CollectionUtils; public class ConfigurationModifier implements PolarisConfigModifier { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class); + private static final String DATA_SOURCE_POLARIS = "polaris"; + private static final String DATA_SOURCE_LOCAL = "local"; + private final PolarisConfigProperties polarisConfigProperties; private final PolarisContextProperties polarisContextProperties; @@ -54,6 +57,27 @@ public class ConfigurationModifier implements PolarisConfigModifier { @Override public void modify(ConfigurationImpl configuration) { + if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) { + initByPolarisDataSource(configuration); + } + else if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_LOCAL)) { + initByLocalDataSource(configuration); + } + else { + throw new RuntimeException("Unsupported config data source"); + } + } + + private void initByLocalDataSource(ConfigurationImpl configuration) { + configuration.getConfigFile().getServerConnector().setConnectorType("localFile"); + + String localFileRootPath = polarisConfigProperties.getLocalFileRootPath(); + configuration.getConfigFile().getServerConnector().setPersistDir(localFileRootPath); + + LOGGER.info("[SCT] Run spring cloud tencent config with local data source. localFileRootPath = {}", localFileRootPath); + } + + private void initByPolarisDataSource(ConfigurationImpl configuration) { // set connector type configuration.getConfigFile().getServerConnector().setConnectorType("polaris"); @@ -76,6 +100,8 @@ public class ConfigurationModifier implements PolarisConfigModifier { checkAddressAccessible(configAddresses); configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); + + LOGGER.info("[SCT] Run spring cloud tencent config in polaris data source."); } @Override 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 3bc06f7c0..8786abb2a 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 @@ -54,6 +54,12 @@ public class PolarisConfigProperties { private boolean shutdownIfConnectToConfigServerFailed = true; + /** + * When the local configuration is consistent with the remote configuration, whether to + * preferentially load the remote configuration. + */ + private boolean preference = true; + /** * Attribute refresh type. */ @@ -64,6 +70,20 @@ public class PolarisConfigProperties { */ private List groups; + /** + * Where to load config file. default is polaris. + *
+ * polaris: load from polaris server. + *
+ * local: load from local file system. + */ + private String dataSource = "polaris"; + + /** + * The root path of config files, only used in local mode. + */ + private String localFileRootPath = "./polaris/backup/config"; + public boolean isEnabled() { return enabled; } @@ -119,4 +139,28 @@ public class PolarisConfigProperties { public void setGroups(List groups) { this.groups = groups; } + + public boolean isPreference() { + return preference; + } + + public void setPreference(boolean preference) { + this.preference = preference; + } + + public String getDataSource() { + return dataSource; + } + + public void setDataSource(String dataSource) { + this.dataSource = dataSource; + } + + public String getLocalFileRootPath() { + return localFileRootPath; + } + + public void setLocalFileRootPath(String localFileRootPath) { + this.localFileRootPath = localFileRootPath; + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 64172e637..b70a467c7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -71,6 +71,18 @@ "type": "java.lang.Boolean", "defaultValue": true, "description": "Whether shutdown server if connect to config server failed." + }, + { + "name": "spring.cloud.polaris.config.data-source", + "type": "java.lang.String", + "defaultValue": "polaris", + "description": "Where to load config file, polaris or local." + }, + { + "name": "spring.cloud.polaris.config.local-file-root-path", + "type": "java.lang.String", + "defaultValue": "./polaris/backup/config", + "description": "Where to load config file, polaris or local." } ] } diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml index 1f257a787..88ffbba8a 100644 --- a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml @@ -9,6 +9,8 @@ spring: namespace: default config: auto-refresh: true # auto refresh when config file changed + # data-source: local # config data source default is polaris. + # local-file-root-path: # set root path for reading config file, when in local data source groups: - name: ${spring.application.name} # group name files: [ "config/application.properties", "config/bootstrap.yml" ] # config/application.properties takes precedence over config/bootstrap.yml