feat:support read config file from local file system. (#643)

pull/650/head
Haotian Zhang 2 years ago committed by GitHub
parent af0845f01f
commit 31613408c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,3 +11,4 @@
- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/634) - [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: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 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)

@ -42,6 +42,9 @@ import org.springframework.util.CollectionUtils;
public class ConfigurationModifier implements PolarisConfigModifier { public class ConfigurationModifier implements PolarisConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class); 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 PolarisConfigProperties polarisConfigProperties;
private final PolarisContextProperties polarisContextProperties; private final PolarisContextProperties polarisContextProperties;
@ -54,6 +57,27 @@ public class ConfigurationModifier implements PolarisConfigModifier {
@Override @Override
public void modify(ConfigurationImpl configuration) { 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 // set connector type
configuration.getConfigFile().getServerConnector().setConnectorType("polaris"); configuration.getConfigFile().getServerConnector().setConnectorType("polaris");
@ -76,6 +100,8 @@ public class ConfigurationModifier implements PolarisConfigModifier {
checkAddressAccessible(configAddresses); checkAddressAccessible(configAddresses);
configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);
LOGGER.info("[SCT] Run spring cloud tencent config in polaris data source.");
} }
@Override @Override

@ -54,6 +54,12 @@ public class PolarisConfigProperties {
private boolean shutdownIfConnectToConfigServerFailed = true; 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. * Attribute refresh type.
*/ */
@ -64,6 +70,20 @@ public class PolarisConfigProperties {
*/ */
private List<ConfigFileGroup> groups; 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() { public boolean isEnabled() {
return enabled; return enabled;
} }
@ -119,4 +139,28 @@ public class PolarisConfigProperties {
public void setGroups(List<ConfigFileGroup> groups) { public void setGroups(List<ConfigFileGroup> groups) {
this.groups = 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;
}
} }

@ -71,6 +71,18 @@
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"defaultValue": true, "defaultValue": true,
"description": "Whether shutdown server if connect to config server failed." "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."
} }
] ]
} }

@ -9,6 +9,8 @@ spring:
namespace: default namespace: default
config: config:
auto-refresh: true # auto refresh when config file changed 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: groups:
- name: ${spring.application.name} # group name - name: ${spring.application.name} # group name
files: [ "config/application.properties", "config/bootstrap.yml" ] # config/application.properties takes precedence over config/bootstrap.yml files: [ "config/application.properties", "config/bootstrap.yml" ] # config/application.properties takes precedence over config/bootstrap.yml

Loading…
Cancel
Save