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

pull/651/head
Haotian Zhang 2 years ago committed by GitHub
parent 1dd2a5a4b3
commit 08a16d81d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,3 +11,4 @@
- [optimize:optimize PolarisRouterContext and constants.](https://github.com/Tencent/spring-cloud-tencent/pull/628) - [optimize:optimize PolarisRouterContext and constants.](https://github.com/Tencent/spring-cloud-tencent/pull/628)
- [support spring-retry router](https://github.com/Tencent/spring-cloud-tencent/pull/631) - [support spring-retry router](https://github.com/Tencent/spring-cloud-tencent/pull/631)
- [feat:Transfer http headers specified by environment variables](https://github.com/Tencent/spring-cloud-tencent/pull/638) - [feat:Transfer http headers specified by environment variables](https://github.com/Tencent/spring-cloud-tencent/pull/638)
- [feat:support read config file from local file system.](https://github.com/Tencent/spring-cloud-tencent/pull/649)

@ -41,6 +41,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;
@ -53,6 +56,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");
@ -75,6 +99,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

@ -55,6 +55,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.
*/ */
@ -65,6 +71,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;
} }
@ -120,4 +140,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;
}
} }

@ -53,6 +53,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": "The root path of config files, only used in local mode."
} }
] ]
} }

@ -71,7 +71,7 @@
<properties> <properties>
<revision>1.8.0-Hoxton.SR12-SNAPSHOT</revision> <revision>1.8.0-Hoxton.SR12-SNAPSHOT</revision>
<polaris.version>1.8.0</polaris.version> <polaris.version>1.9.1</polaris.version>
<logback.version>1.2.11</logback.version> <logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version> <mocktio.version>4.5.1</mocktio.version>
<byte-buddy.version>1.12.10</byte-buddy.version> <byte-buddy.version>1.12.10</byte-buddy.version>

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