diff --git a/CHANGELOG.md b/CHANGELOG.md index e2ba0556..d74a8718 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,3 +10,5 @@ - [send heartbeat if healthcheck url not configured](https://github.com/Tencent/spring-cloud-tencent/pull/54) - [feat:optimize project structure and checkstyle](https://github.com/Tencent/spring-cloud-tencent/pull/56) - [feat:divide storage and transfer of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/63) +- [feat:support polaris config center.](https://github.com/Tencent/spring-cloud-tencent/pull/69) + diff --git a/pom.xml b/pom.xml index bc98fa02..d70a4568 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ spring-cloud-tencent-dependencies spring-cloud-tencent-examples spring-cloud-tencent-coverage + spring-cloud-starter-tencent-polaris-config @@ -292,4 +293,4 @@ - \ No newline at end of file + diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java new file mode 100644 index 00000000..3718ba4b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java @@ -0,0 +1,58 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.circuitbreaker; + +import com.tencent.cloud.common.constant.ContextConstant; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.polaris.factory.config.ConfigurationImpl; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Auto configuration at bootstrap phase. + * + * @author lepdou 2022-03-29 + */ +@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", + havingValue = "true", matchIfMissing = true) +@Configuration(proxyBeanMethods = false) +public class PolarisCircuitBreakerBootstrapConfiguration { + + @Bean + public CircuitBreakerConfigModifier circuitBreakerConfigModifier() { + return new CircuitBreakerConfigModifier(); + } + + public static class CircuitBreakerConfigModifier implements PolarisConfigModifier { + + @Override + public void modify(ConfigurationImpl configuration) { + // Turn on circuitbreaker configuration + configuration.getConsumer().getCircuitBreaker().setEnable(true); + } + + @Override + public int getOrder() { + return ContextConstant.ModifierOrder.CIRCUIT_BREAKER_ORDER; + } + + } + +} diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java index 634abf6e..b03d1491 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java @@ -17,14 +17,11 @@ package com.tencent.cloud.polaris.circuitbreaker; -import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder; import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor; -import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisContextConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.api.DiscoveryAPIFactory; -import com.tencent.polaris.factory.config.ConfigurationImpl; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; @@ -61,24 +58,4 @@ public class PolarisFeignClientAutoConfiguration { return new PolarisFeignBeanPostProcessor(consumerAPI); } - @Bean - public CircuitBreakerConfigModifier circuitBreakerConfigModifier() { - return new CircuitBreakerConfigModifier(); - } - - public static class CircuitBreakerConfigModifier implements PolarisConfigModifier { - - @Override - public void modify(ConfigurationImpl configuration) { - // Enable circuit breaker. - configuration.getConsumer().getCircuitBreaker().setEnable(true); - } - - @Override - public int getOrder() { - return ModifierOrder.CIRCUIT_BREAKER_ORDER; - } - - } - } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring.factories index 4ceef0fb..04fa47a1 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring.factories @@ -1,2 +1,4 @@ +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.polaris.circuitbreaker.PolarisCircuitBreakerBootstrapConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfiguration diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java index b37c1792..757e1200 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.circuitbreaker.feign; import com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextConfiguration; import feign.Client; import org.junit.Test; import org.junit.jupiter.api.Assertions; @@ -36,7 +37,7 @@ import org.springframework.test.context.junit4.SpringRunner; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = TestPolarisFeignApp.class) -@ContextConfiguration(classes = { PolarisFeignClientAutoConfiguration.class }) +@ContextConfiguration(classes = { PolarisFeignClientAutoConfiguration.class, PolarisContextConfiguration.class }) public class PolarisFeignClientTest { @Autowired diff --git a/spring-cloud-starter-tencent-polaris-config/pom.xml b/spring-cloud-starter-tencent-polaris-config/pom.xml new file mode 100644 index 00000000..d257cd08 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/pom.xml @@ -0,0 +1,38 @@ + + + + spring-cloud-tencent + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-starter-tencent-polaris-config + + + + + com.tencent.cloud + spring-cloud-tencent-polaris-context + + + + + + com.tencent.polaris + polaris-configuration-factory + + + + + + org.springframework.cloud + spring-cloud-context + + + + + 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 new file mode 100644 index 00000000..a02d090a --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -0,0 +1,62 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config; + +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.polaris.factory.config.ConfigurationImpl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; + +/** + * Read configuration from spring cloud's configuration file and override polaris.yaml. + * + * @author lepdou 2022-03-10 + */ +public class ConfigurationModifier implements PolarisConfigModifier { + + @Autowired + private PolarisConfigProperties polarisConfigProperties; + + @Override + public void modify(ConfigurationImpl configuration) { + configuration.getConfigFile().getServerConnector().setConnectorType("polaris"); + + if (StringUtils.isEmpty(polarisConfigProperties.getAddresses())) { + return; + } + + // override polaris config server address + List addresses = AddressUtils + .parseAddressList(polarisConfigProperties.getAddresses()); + + configuration.getConfigFile().getServerConnector().setAddresses(addresses); + } + + @Override + public int getOrder() { + return ContextConstant.ModifierOrder.CONFIG_ORDER; + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java new file mode 100644 index 00000000..eab848b5 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config; + +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceAutoRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.context.refresh.ContextRefresher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * polaris config module auto configuration at init application context phase. + * + * @author lepdou 2022-03-28 + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", + matchIfMissing = true) +public class PolarisConfigAutoConfiguration { + + @Bean + public PolarisPropertySourceAutoRefresher polarisPropertySourceAutoRefresher( + PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, + ContextRefresher contextRefresher) { + return new PolarisPropertySourceAutoRefresher(polarisConfigProperties, + polarisPropertySourceManager, contextRefresher); + } + +} 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 new file mode 100644 index 00000000..8ca4ad98 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java @@ -0,0 +1,73 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ +package com.tencent.cloud.polaris.config; + +import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; +import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.context.PolarisContextProperties; +import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.configuration.api.core.ConfigFileService; +import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * polaris config module auto configuration at bootstrap phase. + * + * @author lepdou 2022-03-10 + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", + matchIfMissing = true) +public class PolarisConfigBootstrapAutoConfiguration { + + @Bean + public PolarisConfigProperties polarisProperties() { + return new PolarisConfigProperties(); + } + + @Bean + public ConfigFileService configFileService(SDKContext sdkContext) { + return ConfigFileServiceFactory.createConfigFileService(sdkContext); + } + + @Bean + public PolarisPropertySourceManager polarisPropertySourceManager() { + return new PolarisPropertySourceManager(); + } + + @Bean + public PolarisConfigFileLocator polarisConfigFileLocator( + PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties, + ConfigFileService configFileService, + PolarisPropertySourceManager polarisPropertySourceManager) { + return new PolarisConfigFileLocator(polarisConfigProperties, + polarisContextProperties, configFileService, + polarisPropertySourceManager); + } + + @Bean + public ConfigurationModifier configurationModifier() { + return new ConfigurationModifier(); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java new file mode 100644 index 00000000..6ab0b453 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java @@ -0,0 +1,153 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import com.tencent.cloud.polaris.config.config.ConfigFileGroup; +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; +import com.tencent.cloud.polaris.context.PolarisContextProperties; +import com.tencent.polaris.configuration.api.core.ConfigFileService; +import com.tencent.polaris.configuration.api.core.ConfigKVFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.cloud.bootstrap.config.PropertySourceLocator; +import org.springframework.core.annotation.Order; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +/** + * Spring cloud reserved core configuration loading SPI. + *

+ * This SPI is implemented to interface with Polaris configuration center + * + * @author lepdou 2022-03-10 + */ +@Order(0) +public class PolarisConfigFileLocator implements PropertySourceLocator { + + private static final Logger LOGGER = LoggerFactory + .getLogger(PolarisConfigFileLocator.class); + + private static final String POLARIS_CONFIG_PROPERTY_SOURCE_NAME = "polaris-config"; + + private final PolarisConfigProperties polarisConfigProperties; + + private final PolarisContextProperties polarisContextProperties; + + private final ConfigFileService configFileService; + + private final PolarisPropertySourceManager polarisPropertySourceManager; + + public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties, + ConfigFileService configFileService, + PolarisPropertySourceManager polarisPropertySourceManager) { + this.polarisConfigProperties = polarisConfigProperties; + this.polarisContextProperties = polarisContextProperties; + this.configFileService = configFileService; + this.polarisPropertySourceManager = polarisPropertySourceManager; + } + + @Override + public PropertySource locate(Environment environment) { + CompositePropertySource compositePropertySource = new CompositePropertySource( + POLARIS_CONFIG_PROPERTY_SOURCE_NAME); + + List configFileGroups = polarisConfigProperties.getGroups(); + if (CollectionUtils.isEmpty(configFileGroups)) { + return compositePropertySource; + } + + initPolarisConfigFiles(compositePropertySource, configFileGroups); + + return compositePropertySource; + } + + private void initPolarisConfigFiles(CompositePropertySource compositePropertySource, + List configFileGroups) { + String namespace = polarisContextProperties.getNamespace(); + + for (ConfigFileGroup configFileGroup : configFileGroups) { + String group = configFileGroup.getName(); + + if (StringUtils.isEmpty(group)) { + throw new IllegalArgumentException( + "polaris config group name cannot be empty."); + } + + List files = configFileGroup.getFiles(); + if (CollectionUtils.isEmpty(files)) { + return; + } + + for (String fileName : files) { + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( + namespace, group, fileName); + + compositePropertySource.addPropertySource(polarisPropertySource); + + polarisPropertySourceManager.addPropertySource(polarisPropertySource); + + LOGGER.info( + "[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", + namespace, group, fileName); + } + } + } + + private PolarisPropertySource loadPolarisPropertySource(String namespace, + String group, String fileName) { + ConfigKVFile configKVFile; + // unknown extension is resolved as properties file + if (ConfigFileFormat.isPropertyFile(fileName) + || ConfigFileFormat.isUnknownFile(fileName)) { + configKVFile = configFileService.getConfigPropertiesFile(namespace, group, + fileName); + } + else if (ConfigFileFormat.isYamlFile(fileName)) { + configKVFile = configFileService.getConfigYamlFile(namespace, group, + fileName); + } + else { + LOGGER.warn( + "[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", + namespace, group, fileName); + + throw new IllegalStateException( + "Only configuration files in the format of properties / yaml / yaml" + + " can be injected into the spring context"); + } + + Map map = new ConcurrentHashMap<>(); + for (String key : configKVFile.getPropertyNames()) { + map.put(key, configKVFile.getProperty(key, null)); + } + + return new PolarisPropertySource(namespace, group, fileName, configKVFile, map); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java new file mode 100644 index 00000000..76b234e7 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java @@ -0,0 +1,78 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.Map; + +import com.tencent.polaris.configuration.api.core.ConfigKVFile; + +import org.springframework.core.env.MapPropertySource; + +/** + * a polaris config file will be wrapped as polaris property source. + * + * @author lepdou 2022-03-10 + */ +public class PolarisPropertySource extends MapPropertySource { + + private final String namespace; + + private final String group; + + private final String fileName; + + private final ConfigKVFile configKVFile; + + public PolarisPropertySource(String namespace, String group, String fileName, + ConfigKVFile configKVFile, Map source) { + super(namespace + "-" + group + "-" + fileName, source); + + this.namespace = namespace; + this.group = group; + this.fileName = fileName; + this.configKVFile = configKVFile; + } + + public String getNamespace() { + return namespace; + } + + public String getGroup() { + return group; + } + + public String getFileName() { + return fileName; + } + + public String getPropertySourceName() { + return namespace + "-" + group + "-" + fileName; + } + + ConfigKVFile getConfigKVFile() { + return configKVFile; + } + + @Override + public String toString() { + return "PolarisPropertySource{" + "namespace='" + namespace + '\'' + ", group='" + + group + '\'' + ", fileName='" + fileName + '\'' + '}'; + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java new file mode 100644 index 00000000..fac3ba25 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java @@ -0,0 +1,141 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; +import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener; +import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.BeansException; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.cloud.context.refresh.ContextRefresher; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationListener; +import org.springframework.util.CollectionUtils; + +/** + * 1. Listen to the Polaris server configuration publishing event 2. Write the changed + * configuration content to propertySource 3. Refresh the context through contextRefresher + * + * @author lepdou 2022-03-28 + */ +public class PolarisPropertySourceAutoRefresher + implements ApplicationListener, ApplicationContextAware { + + private static final Logger LOGGER = LoggerFactory + .getLogger(PolarisPropertySourceAutoRefresher.class); + + private final PolarisConfigProperties polarisConfigProperties; + + private final PolarisPropertySourceManager polarisPropertySourceManager; + + private ApplicationContext applicationContext; + + private final ContextRefresher contextRefresher; + + private final AtomicBoolean registered = new AtomicBoolean(false); + + public PolarisPropertySourceAutoRefresher( + PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, + ContextRefresher contextRefresher) { + this.polarisConfigProperties = polarisConfigProperties; + this.polarisPropertySourceManager = polarisPropertySourceManager; + this.contextRefresher = contextRefresher; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + public void onApplicationEvent(ApplicationReadyEvent event) { + registerPolarisConfigPublishEvent(); + } + + private void registerPolarisConfigPublishEvent() { + if (!polarisConfigProperties.isAutoRefresh()) { + return; + } + + List polarisPropertySources = polarisPropertySourceManager + .getAllPropertySources(); + if (CollectionUtils.isEmpty(polarisPropertySources)) { + return; + } + + if (!registered.compareAndSet(false, true)) { + return; + } + + // register polaris config publish event + for (PolarisPropertySource polarisPropertySource : polarisPropertySources) { + polarisPropertySource.getConfigKVFile() + .addChangeListener(new ConfigKVFileChangeListener() { + @Override + public void onChange( + ConfigKVFileChangeEvent configKVFileChangeEvent) { + LOGGER.info( + "[SCT Config] received polaris config change event and will refresh spring context." + + "namespace = {}, group = {}, fileName = {}", + polarisPropertySource.getNamespace(), + polarisPropertySource.getGroup(), + polarisPropertySource.getFileName()); + + Map source = polarisPropertySource + .getSource(); + + for (String changedKey : configKVFileChangeEvent + .changedKeys()) { + ConfigPropertyChangeInfo configPropertyChangeInfo = configKVFileChangeEvent + .getChangeInfo(changedKey); + + LOGGER.info("[SCT Config] changed property = {}", + configPropertyChangeInfo); + + switch (configPropertyChangeInfo.getChangeType()) { + case MODIFIED: + case ADDED: + source.put(changedKey, + configPropertyChangeInfo.getNewValue()); + break; + case DELETED: + source.remove(changedKey); + break; + } + } + + // rebuild beans with @RefreshScope annotation + contextRefresher.refresh(); + } + }); + } + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java new file mode 100644 index 00000000..6fff4906 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java @@ -0,0 +1,44 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The manager of polaris property source. + * + * @author lepdou 2022-03-28 + */ +public class PolarisPropertySourceManager { + + private final Map polarisPropertySources = new ConcurrentHashMap<>(); + + public void addPropertySource(PolarisPropertySource polarisPropertySource) { + polarisPropertySources.putIfAbsent(polarisPropertySource.getPropertySourceName(), + polarisPropertySource); + } + + public List getAllPropertySources() { + return new ArrayList<>(polarisPropertySources.values()); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java new file mode 100644 index 00000000..05f1d7c5 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java @@ -0,0 +1,60 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ +package com.tencent.cloud.polaris.config.config; + +import java.util.List; + +/** + * the config file group. + * + * @author lepdou 2022-03-28 + */ +public class ConfigFileGroup { + + /** + * group name. + */ + private String name; + + /** + * the files belong to group. + */ + private List files; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getFiles() { + return files; + } + + public void setFiles(List files) { + this.files = files; + } + + @Override + public String toString() { + return "ConfigFileGroup{" + "name='" + name + '\'' + ", file=" + files + '}'; + } + +} 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 new file mode 100644 index 00000000..8ef59261 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -0,0 +1,85 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ +package com.tencent.cloud.polaris.config.config; + +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * polaris config module bootstrap configs. + * + * @author lepdou 2022-03-10 + */ +@ConfigurationProperties("spring.cloud.polaris.config") +public class PolarisConfigProperties { + + /** + * Whether to open the configuration center. + */ + private boolean enabled = true; + + /** + * Configuration center service address list. + */ + private String addresses; + + /** + * Whether to automatically update to the spring context when the configuration file. + * is updated + */ + private boolean autoRefresh = true; + + /** + * List of injected configuration files. + */ + private List groups; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getAddresses() { + return addresses; + } + + public void setAddresses(String addresses) { + this.addresses = addresses; + } + + public boolean isAutoRefresh() { + return autoRefresh; + } + + public void setAutoRefresh(boolean autoRefresh) { + this.autoRefresh = autoRefresh; + } + + public List getGroups() { + return groups; + } + + public void setGroups(List groups) { + this.groups = groups; + } + +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/ConfigFileFormat.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/ConfigFileFormat.java new file mode 100644 index 00000000..2b7be577 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/ConfigFileFormat.java @@ -0,0 +1,84 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.enums; + +/** + * the format of config file. + * + * @author lepdou 2022-03-28 + */ +public enum ConfigFileFormat { + + /** + * property format. + */ + PROPERTY(".property"), + /** + * yaml format. + */ + YAML(".yaml"), + /** + * yml format. + */ + YML(".yml"), + /** + * xml format. + */ + XML(".xml"), + /** + * json format. + */ + JSON(".json"), + /** + * text format. + */ + TEXT(".text"), + /** + * html format. + */ + html(".html"), + /** + * unknown format. + */ + UNKNOWN(".unknown"); + + private final String extension; + + ConfigFileFormat(String extension) { + this.extension = extension; + } + + public static boolean isPropertyFile(String fileName) { + return fileName.endsWith(PROPERTY.extension); + } + + public static boolean isYamlFile(String fileName) { + return fileName.endsWith(YAML.extension) || fileName.endsWith(YML.extension); + } + + public static boolean isUnknownFile(String fileName) { + for (ConfigFileFormat format : ConfigFileFormat.values()) { + if (fileName.endsWith(format.extension)) { + return false; + } + } + return true; + } + +} 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 new file mode 100644 index 00000000..d2461eb7 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,32 @@ +{ + "properties": [ + { + "name": "spring.cloud.polaris.config.enabled", + "type": "java.lang.Boolean", + "defaultValue": "true", + "description": "The switch of polaris configuration module.", + "sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" + }, + { + "name": "spring.cloud.polaris.config.addresses", + "type": "java.lang.String", + "defaultValue": "", + "description": "The polaris configuration server addresses.", + "sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" + }, + { + "name": "spring.cloud.polaris.config.auto-refresh", + "type": "java.lang.Boolean", + "defaultValue": "true", + "description": "Whether to automatically update to the spring context when the configuration file is updated.", + "sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" + }, + { + "name": "spring.cloud.polaris.config.groups", + "type": "com.tencent.cloud.polaris.config.config.ConfigFileGroup", + "defaultValue": "", + "description": "List of imported config files.", + "sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" + } + ] +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..a0c33067 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/spring.factories @@ -0,0 +1,4 @@ +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java index f9e4e39d..f387cf14 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java @@ -43,7 +43,7 @@ public class PolarisProperties { /** * Namespace, separation registry of different environments. */ - @Value("${spring.cloud.polaris.discovery.namespace:#{'default'}}") + @Value("${spring.cloud.polaris.discovery.namespace:${spring.cloud.polaris.namespace:#{'default'}}}") private String namespace; /** diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java index b8d6b319..9c864be9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java @@ -48,7 +48,8 @@ public class PolarisDiscoveryAutoConfigurationTest { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, PolarisDiscoveryAutoConfiguration.class, - PolarisDiscoveryClientConfiguration.class)) + PolarisDiscoveryClientConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081"); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java index c4738115..0de51124 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java @@ -44,7 +44,8 @@ public class PolarisDiscoveryClientConfigurationTest { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, - PolarisDiscoveryClientConfiguration.class)) + PolarisDiscoveryClientConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081"); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java index de38a3b1..abd3ac98 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java @@ -53,7 +53,8 @@ public class PolarisServiceDiscoveryTest { .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, PolarisServiceDiscoveryTest.PolarisPropertiesConfiguration.class, PolarisDiscoveryClientConfiguration.class, - PolarisDiscoveryAutoConfiguration.class)) + PolarisDiscoveryAutoConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java index 25dff7d4..1069b364 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java @@ -46,7 +46,8 @@ public class PolarisReactiveDiscoveryClientConfigurationTest { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, PolarisReactiveDiscoveryClientConfiguration.class, - PolarisDiscoveryClientConfiguration.class)) + PolarisDiscoveryClientConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081"); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfigurationTest.java index 99660fb6..eea15316 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfigurationTest.java @@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.ribbon; import com.netflix.client.config.DefaultClientConfigImpl; import com.netflix.client.config.IClientConfig; +import com.tencent.cloud.polaris.context.PolarisContextConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import org.junit.Test; @@ -46,7 +47,8 @@ public class PolarisRibbonServerListConfigurationTest { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisRibbonClientTest.class, - PolarisDiscoveryClientConfiguration.class)) + PolarisDiscoveryClientConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java index b7ffb7e6..92cc4fb3 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java @@ -58,7 +58,8 @@ public class PolarisServerListTest { .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, PolarisServerListTest.PolarisPropertiesConfiguration.class, PolarisDiscoveryClientConfiguration.class, - PolarisDiscoveryAutoConfiguration.class)) + PolarisDiscoveryAutoConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java index cd92a08d..af4645e6 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java @@ -17,6 +17,7 @@ package com.tencent.cloud.polaris.router.config; +import com.tencent.cloud.polaris.context.PolarisContextConfiguration; import com.tencent.polaris.router.api.core.RouterAPI; import org.junit.Test; @@ -38,7 +39,8 @@ public class PolarisRibbonAutoConfigurationTest { private ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class, - PolarisRibbonAutoConfiguration.class)) + PolarisRibbonAutoConfiguration.class, + PolarisContextConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081"); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java index 7b0c0b85..54f20387 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java @@ -44,6 +44,11 @@ public final class ContextConstant { */ public static Integer CIRCUIT_BREAKER_ORDER = 1; + /** + * Order of configuration modifier. + */ + public static Integer CONFIG_ORDER = 1; + } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java new file mode 100644 index 00000000..4cfcb11a --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java @@ -0,0 +1,48 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.common.util; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +/** + * the utils of parse address. + * + * @author lepdou 2022-03-29 + */ +public final class AddressUtils { + + private static final String ADDRESS_SEPARATOR = ","; + + private AddressUtils() { + + } + + public static List parseAddressList(String addressInfo) { + List addressList = new ArrayList<>(); + String[] addresses = addressInfo.split(ADDRESS_SEPARATOR); + for (String address : addresses) { + URI uri = URI.create(address.trim()); + addressList.add(uri.getAuthority()); + } + return addressList; + } + +} diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index d16a6fb6..c20150a2 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -96,7 +96,6 @@ ${revision} - com.tencent.cloud spring-cloud-starter-tencent-polaris-ratelimit @@ -115,6 +114,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-config + ${revision} + + com.tencent.cloud spring-cloud-tencent-polaris-context @@ -239,4 +244,4 @@ - \ No newline at end of file + diff --git a/spring-cloud-tencent-examples/polaris-config-example/pom.xml b/spring-cloud-tencent-examples/polaris-config-example/pom.xml new file mode 100644 index 00000000..d782d5d8 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/pom.xml @@ -0,0 +1,36 @@ + + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + polaris-config-example + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-config + + + + + diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/ConfigController.java b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/ConfigController.java new file mode 100644 index 00000000..d06a732e --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/ConfigController.java @@ -0,0 +1,55 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package com.tencent.cloud.polaris.config.example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * the endpoint for get config. + * + * @author lepdou 2022-03-10 + */ +@RestController +@RefreshScope +public class ConfigController { + + @Value("${timeout:1000}") + private int timeout; + + @Autowired + private Person person; + + @Autowired + private Environment environment; + + @GetMapping("/timeout") + public int timeout() { + environment.getProperty("timeout", "1000"); + return timeout; + } + + @GetMapping("/person") + public String person() { + return person.toString(); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java new file mode 100644 index 00000000..492af0a8 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java @@ -0,0 +1,58 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.config.example; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * example property object. + * + * @author lepdou 2022-03-28 + */ +@Component +@ConfigurationProperties(prefix = "teacher") +public class Person { + + private String name; + + private int age; + + String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } + + int getAge() { + return age; + } + + void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; + } + +} diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigExampleApplication.java b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigExampleApplication.java new file mode 100644 index 00000000..a632cf23 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/PolarisConfigExampleApplication.java @@ -0,0 +1,35 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.config.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * example application starter. + * + * @author lepdou 2022-03-10 + */ +@SpringBootApplication +public class PolarisConfigExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(PolarisConfigExampleApplication.class, args); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/README-zh.md b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/README-zh.md new file mode 100644 index 00000000..3b1048a6 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/README-zh.md @@ -0,0 +1,76 @@ +# Polaris Config Example 使用指南 + +## 1. bootstrap.yml 配置 + +> 注意是在 bootstrap.yml 里配置,而不是在 application.yml 里配置。因为配置中心相关的配置是在 bootstrap 阶段依赖的配置。 + +```` yaml +spring: + application: + name: polaris-config-example + cloud: + polaris: + namespace: dev + config: + addresses: grpc://9.134.122.18:8093 # the address of polaris config server + auto-refresh: true # auto refresh when config file changed + groups: + - name: ${spring.application.name} # group name + files: [ "config/application.properties", "config/bootstrap.yml" ] # config/application.properties takes precedence over config/bootstrap.yml +```` + +## 2. 在北极星服务端创建配置文件 + +### 2.1 创建 namespace (dev) +### 2.2 创建配置文件分组(polaris-config-example) + +北极星的配置文件分组概念为一组配置文件的集合,推荐应用名=分组名,例如在我们的示例中,新建一个 polaris-config-example 的分组。 +把 polaris-config-example 应用的配置文件都放在 polaris-config-example 分组下,这样便于配置管理。 + +### 2.3 创建两个配置文件 config/application.properties 、config/bootstrap.yml + +北极星配置中心的控制台,配置文件名可以通过 / 来按树状目录结构展示,通过树状结构可以清晰的管理配置文件。 + +#### 2.3.1 config/application.properties 文件内容 + + ```` properties +timeout = 3000 +```` + +#### 2.3.2 config/bootstrap.yml 文件内容 + +````yaml +teacher: + name : 张三 + age: 38 +```` + +页面样例如下图所示: + +![](polaris-config-ui.png) + +## 3. 运行 PolarisConfigExampleApplication + +## 4. 访问接口 + +```` +curl "http://localhost:48084/timeout" + +curl "http://localhost:48084/person" +```` + +## 5. 动态推送能力 + +### 5.1 管控台动态修改并发布 config/application.properties + + ```` properties +timeout = 5000 +```` + +### 5.2 再次访问接口 +```` +curl "http://localhost:48084/timeout" +```` + + + diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/polaris-config-ui.png b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/polaris-config-ui.png new file mode 100644 index 00000000..ebc5a0c0 Binary files /dev/null and b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/polaris-config-ui.png differ 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 new file mode 100644 index 00000000..613370cc --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml @@ -0,0 +1,14 @@ +server: + port: 48084 +spring: + application: + name: polaris-config-example + cloud: + polaris: + namespace: dev + config: + addresses: grpc://127.0.0.1:8093 # the address of polaris config server + auto-refresh: true # auto refresh when config file changed + groups: + - name: ${spring.application.name} # group name + files: [ "config/application.properties", "config/bootstrap.yml" ] # config/application.properties takes precedence over config/bootstrap.yml diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/log4j.properties b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/log4j.properties new file mode 100644 index 00000000..b0b20e72 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/log4j.properties @@ -0,0 +1,15 @@ +log4j.rootLogger=DEBUG,console,FILE + +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.threshold=INFO +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n + +log4j.appender.FILE=org.apache.log4j.RollingFileAppender +log4j.appender.FILE.Append=true + +log4j.appender.FILE.File=applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log +log4j.appender.FILE.Threshold=INFO +log4j.appender.FILE.layout=org.apache.log4j.PatternLayout +log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n +log4j.appender.FILE.MaxFileSize=10MB diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/logback-spring.xml b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/logback-spring.xml new file mode 100644 index 00000000..0f47f0c1 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/logback-spring.xml @@ -0,0 +1,28 @@ + + + logback + + + %d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n + + + + + true + + + applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log + + + + + %d{yyyy-MM-dd HH:mm:ss} -%msg%n + + + + + + + + + diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index 66478159..cc7b8178 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -8,6 +8,8 @@ spring: address: grpc://127.0.0.1:8091 discovery: ip-address: 127.0.0.1 + inetutils: + default-ip-address: 198.1.1.1 # consul: # port: 8500 # host: 127.0.0.1 diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml index 55d11638..865038c7 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml @@ -35,4 +35,4 @@ spring-cloud-starter-openfeign - \ No newline at end of file + diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index 73e7d84e..61106d50 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -20,10 +20,11 @@ polaris-ratelimit-example polaris-circuitbreaker-example polaris-gateway-example - + polaris-config-example + true - \ No newline at end of file + diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index a97c9420..a6fe5ac9 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -37,11 +37,6 @@ connector-polaris-grpc - - com.tencent.polaris - connector-polaris-grpc - - com.tencent.polaris connector-consul @@ -110,4 +105,4 @@ - \ No newline at end of file + diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/InetUtilsProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/InetUtilsProperties.java new file mode 100644 index 00000000..cad84c34 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/InetUtilsProperties.java @@ -0,0 +1,52 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.context; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Because polaris-context is initialized in the bootstrap phase, the initialization of + * InetUtilsProperties is required. The impact on user usage is that + * spring.cloud.inetutils.defaultIpAddress needs to be configured in bootstrap.yml to take + * effect. + * + * @see org.springframework.cloud.commons.util.InetUtilsProperties + */ +@ConfigurationProperties(InetUtilsProperties.PREFIX) +public class InetUtilsProperties { + + /** + * Prefix for the Inet Utils properties. + */ + public static final String PREFIX = "spring.cloud.inetutils"; + + /** + * The default IP address. Used in case of errors. + */ + private String defaultIpAddress = "127.0.0.1"; + + String getDefaultIpAddress() { + return defaultIpAddress; + } + + void setDefaultIpAddress(String defaultIpAddress) { + this.defaultIpAddress = defaultIpAddress; + } + +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextConfiguration.java index 94bd493c..e54768eb 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextConfiguration.java @@ -17,11 +17,10 @@ package com.tencent.cloud.polaris.context; -import java.net.URI; -import java.util.ArrayList; import java.util.List; import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder; +import com.tencent.cloud.common.util.AddressUtils; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.config.ConfigurationImpl; @@ -37,11 +36,10 @@ import org.springframework.context.annotation.Bean; * * @author Haotian Zhang */ -@EnableConfigurationProperties(PolarisContextProperties.class) +@EnableConfigurationProperties({ PolarisContextProperties.class, + InetUtilsProperties.class }) public class PolarisContextConfiguration { - private static final String ADDRESS_SEPARATOR = ","; - @Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy") @ConditionalOnMissingBean public SDKContext polarisContext(PolarisContextProperties properties) @@ -62,10 +60,14 @@ public class PolarisContextConfiguration { @Override public void modify(ConfigurationImpl configuration) { - if (!StringUtils.isBlank(properties.getAddress())) { - configuration.getGlobal().getServerConnector() - .setAddresses(getAddressList(properties.getAddress())); + if (StringUtils.isBlank(properties.getAddress())) { + return; } + + List addresses = AddressUtils + .parseAddressList(properties.getAddress()); + + configuration.getGlobal().getServerConnector().setAddresses(addresses); } @Override @@ -73,16 +75,6 @@ public class PolarisContextConfiguration { return ModifierOrder.FIRST; } - private List getAddressList(String addressInfo) { - List addressList = new ArrayList<>(); - String[] addresses = addressInfo.split(ADDRESS_SEPARATOR); - for (String address : addresses) { - URI uri = URI.create(address.trim()); - addressList.add(uri.getAuthority()); - } - return addressList; - } - } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java index 9235de4a..a3cce9e4 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java @@ -29,8 +29,8 @@ import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.cloud.commons.util.InetUtilsProperties; import org.springframework.core.env.Environment; import org.springframework.util.CollectionUtils; @@ -43,10 +43,16 @@ import org.springframework.util.CollectionUtils; public class PolarisContextProperties { /** - * polaris server adress. + * polaris server address. */ private String address; + /** + * polaris namespace. + */ + @Value("${spring.cloud.polaris.namespace:#{'default'}}") + private String namespace; + @Autowired private InetUtilsProperties inetUtilsProperties; @@ -56,14 +62,6 @@ public class PolarisContextProperties { @Autowired private List modifierList; - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - protected Configuration configuration() { ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory .defaultConfig(ConfigProvider.DEFAULT_CONFIG); @@ -91,4 +89,20 @@ public class PolarisContextProperties { return environment.getProperty("spring.cloud.client.ip-address"); } + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + } diff --git a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json index 8c326e0c..7274683d 100644 --- a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json @@ -12,6 +12,12 @@ "type": "java.lang.String", "description": "polaris server address list that can be separated by \",\"", "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + }, + { + "name": "spring.cloud.polaris.namespace", + "type": "java.lang.String", + "description": "polaris namespace", + "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" } ], "hints": [] diff --git a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories index 3d931b1c..609b809d 100644 --- a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories @@ -1,2 +1 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.tencent.cloud.polaris.context.PolarisContextConfiguration +org.springframework.cloud.bootstrap.BootstrapConfiguration=com.tencent.cloud.polaris.context.PolarisContextConfiguration diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java index 0ead596e..dce22530 100644 --- a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java @@ -24,12 +24,14 @@ import org.junit.platform.commons.util.StringUtils; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = PolarisContextApplication.class, - properties = { "spring.config.location = classpath:application-test.yml" }) + properties = { "spring.config.location = classpath:bootstrap.yml" }) +@ImportAutoConfiguration({ PolarisContextConfiguration.class }) public class PolarisContextGetHostTest { @Autowired @@ -39,7 +41,7 @@ public class PolarisContextGetHostTest { public void testGetConfigHost() { String bindIP = polarisContext.getConfig().getGlobal().getAPI().getBindIP(); Assert.assertFalse(StringUtils.isBlank(bindIP)); - Assert.assertNotEquals(bindIP, "127.0.0.1"); + Assert.assertEquals(bindIP, "192.168.1.1"); } } diff --git a/spring-cloud-tencent-polaris-context/src/test/resources/application-test.yml b/spring-cloud-tencent-polaris-context/src/test/resources/application-test.yml deleted file mode 100644 index 512acd15..00000000 --- a/spring-cloud-tencent-polaris-context/src/test/resources/application-test.yml +++ /dev/null @@ -1,4 +0,0 @@ -spring: - cloud: - polaris: - address: grpc://127.0.0.1:8091 \ No newline at end of file diff --git a/spring-cloud-tencent-polaris-context/src/test/resources/bootstrap.yml b/spring-cloud-tencent-polaris-context/src/test/resources/bootstrap.yml new file mode 100644 index 00000000..f85882ee --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/test/resources/bootstrap.yml @@ -0,0 +1,7 @@ +spring: + cloud: + polaris: + address: grpc://127.0.0.1:8091 + namespace: dev + inetutils: + defaultIpAddress: 192.168.1.1