diff --git a/CHANGELOG.md b/CHANGELOG.md index c06d0cfc4..e2d842a70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,3 +10,4 @@ - [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/112) - [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/133) - [Feature: Remove spring-javaformat-maven-plugin](https://github.com/Tencent/spring-cloud-tencent/pull/152) +- [feat:optimize config server address](https://github.com/Tencent/spring-cloud-tencent/pull/150) 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 6c46f3c89..495d6ec3a 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 @@ -18,16 +18,19 @@ package com.tencent.cloud.polaris.config; +import java.util.ArrayList; import java.util.List; import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.cloud.polaris.context.PolarisContextProperties; 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. @@ -36,22 +39,38 @@ import org.springframework.util.StringUtils; */ public class ConfigurationModifier implements PolarisConfigModifier { - @Autowired - private PolarisConfigProperties polarisConfigProperties; + private final PolarisConfigProperties polarisConfigProperties; + + private final PolarisContextProperties polarisContextProperties; + + public ConfigurationModifier(PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { + this.polarisConfigProperties = polarisConfigProperties; + this.polarisContextProperties = polarisContextProperties; + } @Override public void modify(ConfigurationImpl configuration) { + // set connector type configuration.getConfigFile().getServerConnector().setConnectorType("polaris"); - if (StringUtils.isEmpty(polarisConfigProperties.getAddress())) { - return; + // set config server address + List configAddresses; + String configAddressesStr = polarisConfigProperties.getAddress(); + + if (StringUtils.isNotEmpty(configAddressesStr)) { + configAddresses = AddressUtils.parseAddressList(polarisConfigProperties.getAddress()); + } + else { + configAddresses = resolveConfigAddressFromPolarisAddress(polarisContextProperties.getAddress()); } - // override polaris config server address - List addresses = AddressUtils - .parseAddressList(polarisConfigProperties.getAddress()); + if (CollectionUtils.isEmpty(configAddresses)) { + 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 @@ -59,4 +78,24 @@ public class ConfigurationModifier implements PolarisConfigModifier { 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 resolveConfigAddressFromPolarisAddress(String polarisAddress) { + if (StringUtils.isEmpty(polarisAddress)) { + return null; + } + + List polarisAddresses = AddressUtils.parseAddressList(polarisAddress); + List configAddresses = new ArrayList<>(polarisAddresses.size()); + + for (String address : polarisAddresses) { + String ip = StringUtils.substringBeforeLast(address, ":"); + configAddresses.add(ip + ":" + polarisConfigProperties.getPort()); + } + + return configAddresses; + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java index 8c7044a8c..544ee78ac 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java @@ -70,8 +70,9 @@ public class PolarisConfigBootstrapAutoConfiguration { } @Bean - public ConfigurationModifier configurationModifier() { - return new ConfigurationModifier(); + public ConfigurationModifier configurationModifier(PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { + return new ConfigurationModifier(polarisConfigProperties, polarisContextProperties); } } 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 6ddc49f5f..a2c9032fe 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 @@ -39,6 +39,11 @@ public class PolarisConfigProperties { */ private String address; + /** + * Polaris config grpc port. + */ + private int port = 8093; + /** * Whether to automatically update to the spring context when the configuration file. * is updated @@ -66,6 +71,14 @@ public class PolarisConfigProperties { this.address = address; } + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + public boolean isAutoRefresh() { return autoRefresh; } 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 152d24d64..ee84cbd47 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 @@ -14,6 +14,13 @@ "description": "The polaris configuration server address.", "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", "type": "java.lang.Boolean", 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 b8ab4e08a..bdbd643f7 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 @@ -5,9 +5,9 @@ spring: name: polaris-config-example cloud: polaris: + address: grpc://127.0.0.1:8091 namespace: default config: - address: grpc://127.0.0.1:8093 auto-refresh: true # auto refresh when config file changed groups: - name: ${spring.application.name} # group name