support read config file from local file system (#640)

pull/642/head
lepdou 2 years ago committed by GitHub
parent a0737092e7
commit 83bf200a1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,5 +16,6 @@
- [Feature: support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/627)
- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629)
- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635)
- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639)
- [Optimize:optimize transfer](https://github.com/Tencent/spring-cloud-tencent/pull/636)
- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639)
- [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640)

@ -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

@ -70,6 +70,20 @@ public class PolarisConfigProperties {
*/
private List<ConfigFileGroup> groups;
/**
* Where to load config file. default is polaris.
* <br>
* polaris: load from polaris server.
* <br>
* 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;
}
@ -133,4 +147,20 @@ public class PolarisConfigProperties {
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;
}
}

@ -65,6 +65,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."
}
]
}

@ -73,7 +73,7 @@
<revision>1.8.0-2021.0.3-SNAPSHOT</revision>
<!-- Dependencies -->
<polaris.version>1.8.0</polaris.version>
<polaris.version>1.9.0-SNAPSHOT</polaris.version>
<guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version>

@ -11,6 +11,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

Loading…
Cancel
Save