Merge pull request #149 from lepdou/2020/config_address

optimize config server address
pull/159/head
Haotian Zhang 3 years ago committed by GitHub
commit aeb71f7aba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,3 +9,4 @@
- [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/118) - [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/118)
- [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/115) - [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/115)
- [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/129) - [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/129)
- [feat:optimize config server address.](https://github.com/Tencent/spring-cloud-tencent/pull/149)

@ -18,16 +18,19 @@
package com.tencent.cloud.polaris.config; package com.tencent.cloud.polaris.config;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.common.util.AddressUtils;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.PolarisContextProperties;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
/** /**
* Read configuration from spring cloud's configuration file and override polaris.yaml. * Read configuration from spring cloud's configuration file and override polaris.yaml.
@ -36,21 +39,38 @@ import org.springframework.util.StringUtils;
*/ */
public class ConfigurationModifier implements PolarisConfigModifier { public class ConfigurationModifier implements PolarisConfigModifier {
@Autowired private final PolarisConfigProperties polarisConfigProperties;
private PolarisConfigProperties polarisConfigProperties;
private final PolarisContextProperties polarisContextProperties;
public ConfigurationModifier(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties) {
this.polarisConfigProperties = polarisConfigProperties;
this.polarisContextProperties = polarisContextProperties;
}
@Override @Override
public void modify(ConfigurationImpl configuration) { public void modify(ConfigurationImpl configuration) {
// set connector type
configuration.getConfigFile().getServerConnector().setConnectorType("polaris"); configuration.getConfigFile().getServerConnector().setConnectorType("polaris");
if (StringUtils.isEmpty(polarisConfigProperties.getAddress())) { // set config server address
return; List<String> configAddresses;
String configAddressesStr = polarisConfigProperties.getAddress();
if (StringUtils.isNotEmpty(configAddressesStr)) {
configAddresses = AddressUtils.parseAddressList(polarisConfigProperties.getAddress());
}
else {
configAddresses = resolveConfigAddressFromPolarisAddress(polarisContextProperties.getAddress());
} }
// override polaris config server address if (CollectionUtils.isEmpty(configAddresses)) {
List<String> addresses = AddressUtils.parseAddressList(polarisConfigProperties.getAddress()); throw new RuntimeException("Config server address is blank. Please check your config in bootstrap.yml"
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address");
}
configuration.getConfigFile().getServerConnector().setAddresses(addresses); configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);
} }
@Override @Override
@ -58,4 +78,24 @@ public class ConfigurationModifier implements PolarisConfigModifier {
return ContextConstant.ModifierOrder.CONFIG_ORDER; return ContextConstant.ModifierOrder.CONFIG_ORDER;
} }
/**
* In most cases, the address of the configuration center is the same as that of Polaris, but the port is different.
* Therefore, the address of the configuration center can be deduced directly from the Polaris address.
*
*/
private List<String> resolveConfigAddressFromPolarisAddress(String polarisAddress) {
if (StringUtils.isEmpty(polarisAddress)) {
return null;
}
List<String> polarisAddresses = AddressUtils.parseAddressList(polarisAddress);
List<String> configAddresses = new ArrayList<>(polarisAddresses.size());
for (String address : polarisAddresses) {
String ip = StringUtils.substringBeforeLast(address, ":");
configAddresses.add(ip + ":" + polarisConfigProperties.getPort());
}
return configAddresses;
}
} }

@ -67,8 +67,9 @@ public class PolarisConfigBootstrapAutoConfiguration {
} }
@Bean @Bean
public ConfigurationModifier configurationModifier() { public ConfigurationModifier configurationModifier(PolarisConfigProperties polarisConfigProperties,
return new ConfigurationModifier(); PolarisContextProperties polarisContextProperties) {
return new ConfigurationModifier(polarisConfigProperties, polarisContextProperties);
} }
} }

@ -39,6 +39,11 @@ public class PolarisConfigProperties {
*/ */
private String address; private String address;
/**
* Polaris config grpc port.
*/
private int port = 8093;
/** /**
* Whether to automatically update to the spring context when the configuration file. * Whether to automatically update to the spring context when the configuration file.
* is updated * is updated
@ -66,6 +71,14 @@ public class PolarisConfigProperties {
this.address = address; this.address = address;
} }
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean isAutoRefresh() { public boolean isAutoRefresh() {
return autoRefresh; return autoRefresh;
} }

@ -14,6 +14,13 @@
"description": "The polaris configuration server address.", "description": "The polaris configuration server address.",
"sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" "sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties"
}, },
{
"name": "spring.cloud.polaris.config.port",
"type": "java.lang.Integer",
"defaultValue": "8093",
"description": "The polaris configuration server port.",
"sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties"
},
{ {
"name": "spring.cloud.polaris.config.auto-refresh", "name": "spring.cloud.polaris.config.auto-refresh",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",

@ -5,9 +5,9 @@ spring:
name: polaris-config-example name: polaris-config-example
cloud: cloud:
polaris: polaris:
address: grpc://127.0.0.1:8091
namespace: default namespace: default
config: config:
address: grpc://127.0.0.1:8093
auto-refresh: true # auto refresh when config file changed auto-refresh: true # auto refresh when config file changed
groups: groups:
- name: ${spring.application.name} # group name - name: ${spring.application.name} # group name

Loading…
Cancel
Save