diff --git a/CHANGELOG.md b/CHANGELOG.md index fc7796085..42918fbe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ - [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/117) - [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/114) - [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) diff --git a/pom.xml b/pom.xml index 136f7dd35..97241ca13 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,7 @@ 0.8.3 3.2.0 1.2.7 + 3.0.1 true @@ -249,6 +250,7 @@ ${maven-gpg-plugin.version} + sign-artifacts verify sign diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java index 51740c1cf..589875875 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java @@ -209,7 +209,8 @@ public class MetadataTransferAutoConfiguration { // Avoid setting interceptor repeatedly. if (null != interceptors && !interceptors .contains(encodeTransferMedataRestTemplateInterceptor)) { - interceptors.add(this.encodeTransferMedataRestTemplateInterceptor); + interceptors + .add(this.encodeTransferMedataRestTemplateInterceptor); restTemplate.setInterceptors(interceptors); } } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java index 418716e87..1391004e9 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataReactiveFilterTest.java @@ -38,13 +38,11 @@ import org.springframework.web.server.WebFilterChain; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK; /** - * * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = MOCK, - classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, - properties = {"spring.config.location = classpath:application-test.yml"}) +@SpringBootTest(webEnvironment = MOCK, classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, properties = { + "spring.config.location = classpath:application-test.yml" }) public class DecodeTransferMetadataReactiveFilterTest { @Autowired diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java index 42b5598eb..191f03875 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/DecodeTransferMetadataServletFilterTest.java @@ -40,13 +40,11 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; /** - * * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, - classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, - properties = {"spring.config.location = classpath:application-test.yml"}) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, properties = { + "spring.config.location = classpath:application-test.yml" }) public class DecodeTransferMetadataServletFilterTest { @Autowired diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java index a60eb5130..a3d924158 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java @@ -52,8 +52,8 @@ public class MetadataTransferAutoConfigurationTest { .hasSingleBean(EncodeTransferMedataFeignInterceptor.class); Assertions.assertThat(context).hasSingleBean( MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.class); - Assertions.assertThat(context) - .hasSingleBean(EncodeTransferMedataRestTemplateInterceptor.class); + Assertions.assertThat(context).hasSingleBean( + EncodeTransferMedataRestTemplateInterceptor.class); Assertions.assertThat(context).hasSingleBean( MetadataTransferAutoConfiguration.MetadataTransferRestTemplateConfig.EncodeTransferMetadataRestTemplatePostProcessor.class); Assertions.assertThat(context).hasSingleBean( diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java index 13ffead51..c7e636692 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataFeignInterceptorTest.java @@ -51,10 +51,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = DEFINED_PORT, - classes = EncodeTransferMedataFeignInterceptorTest.TestApplication.class, - properties = { "server.port=8081", - "spring.config.location = classpath:application-test.yml" }) +@SpringBootTest(webEnvironment = DEFINED_PORT, classes = EncodeTransferMedataFeignInterceptorTest.TestApplication.class, properties = { + "server.port=8081", "spring.config.location = classpath:application-test.yml" }) public class EncodeTransferMedataFeignInterceptorTest { @Autowired diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java index 6a7f98b25..432050de6 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java @@ -52,9 +52,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen * @author Haotian Zhang */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, - classes = EncodeTransferMedataRestTemplateInterceptorTest.TestApplication.class, - properties = { "spring.config.location = classpath:application-test.yml" }) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EncodeTransferMedataRestTemplateInterceptorTest.TestApplication.class, properties = { + "spring.config.location = classpath:application-test.yml" }) public class EncodeTransferMedataRestTemplateInterceptorTest { @Autowired 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 index fab355c29..23b4c9f7d 100644 --- 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 @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.circuitbreaker; import com.tencent.cloud.common.constant.ContextConstant; +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.polaris.factory.config.ConfigurationImpl; @@ -30,6 +31,7 @@ import org.springframework.context.annotation.Configuration; * * @author lepdou 2022-03-29 */ +@ConditionalOnPolarisEnabled @ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true) @Configuration public class PolarisCircuitBreakerBootstrapConfiguration { 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 6973dd661..8fae677c2 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 @@ -18,7 +18,7 @@ package com.tencent.cloud.polaris.circuitbreaker; import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.api.DiscoveryAPIFactory; @@ -41,7 +41,7 @@ import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE; */ @ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true) @Configuration -@AutoConfigureAfter(PolarisContextConfiguration.class) +@AutoConfigureAfter(PolarisContextAutoConfiguration.class) @AutoConfigureBefore(FeignAutoConfiguration.class) public class PolarisFeignClientAutoConfiguration { diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/additional-spring-configuration-metadata.json similarity index 63% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring-configuration-metadata.json rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 195c3cb2a..99dc05518 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -3,7 +3,7 @@ { "name": "spring.cloud.polaris.circuitbreaker.enabled", "type": "java.lang.Boolean", - "sourceType": "com.tencent.cloud.polaris.circuitbreaker.PolarisFeignProperties" + "defaultValue": "true" } ], "hints": [] 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 bc1e56f7f..806a2b3ac 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,7 +18,7 @@ package com.tencent.cloud.polaris.circuitbreaker.feign; import com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfiguration; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import feign.Client; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(classes = TestPolarisFeignApp.class) @ContextConfiguration(classes = { PolarisFeignClientAutoConfiguration.class, - PolarisContextConfiguration.class }) + PolarisContextAutoConfiguration.class }) public class PolarisFeignClientTest { @Autowired 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 index 54d4cd6df..1a5ff2726 100644 --- 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 @@ -21,6 +21,7 @@ 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 com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.context.refresh.ContextRefresher; @@ -33,6 +34,7 @@ import org.springframework.context.annotation.Configuration; * @author lepdou 2022-03-28 */ @Configuration +@ConditionalOnPolarisEnabled @ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", matchIfMissing = true) public class PolarisConfigAutoConfiguration { 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 2dca5d17a..8c7044a8c 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 @@ -20,7 +20,8 @@ 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.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.context.PolarisContextProperties; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.configuration.api.core.ConfigFileService; @@ -37,8 +38,9 @@ import org.springframework.context.annotation.Import; * @author lepdou 2022-03-10 */ @Configuration +@ConditionalOnPolarisEnabled @ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled", matchIfMissing = true) -@Import(PolarisContextConfiguration.class) +@Import(PolarisContextAutoConfiguration.class) public class PolarisConfigBootstrapAutoConfiguration { @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java new file mode 100644 index 000000000..58721d87b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java @@ -0,0 +1,97 @@ +/* + * 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; + +import javax.annotation.PostConstruct; + +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; +import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; +import com.tencent.polaris.api.core.ConsumerAPI; +import com.tencent.polaris.api.core.ProviderAPI; +import com.tencent.polaris.api.exception.PolarisException; +import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.factory.api.DiscoveryAPIFactory; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * Common configuration of discovery. + * + * @author Haotian Zhang + */ +@Configuration +@ConditionalOnPolarisEnabled +@Import({ PolarisDiscoveryProperties.class, ConsulContextProperties.class }) +public class DiscoveryPropertiesAutoConfiguration { + + @Autowired(required = false) + private PolarisDiscoveryProperties polarisDiscoveryProperties; + + @Autowired(required = false) + private ConsulContextProperties consulContextProperties; + + private boolean registerEnabled = false; + + private boolean discoveryEnabled = false; + + @Bean(name = "polarisProvider") + @ConditionalOnMissingBean + public ProviderAPI polarisProvider(SDKContext polarisContext) + throws PolarisException { + return DiscoveryAPIFactory.createProviderAPIByContext(polarisContext); + } + + @Bean(name = "polarisConsumer") + @ConditionalOnMissingBean + public ConsumerAPI polarisConsumer(SDKContext polarisContext) + throws PolarisException { + return DiscoveryAPIFactory.createConsumerAPIByContext(polarisContext); + } + + @Bean + @ConditionalOnMissingBean + public PolarisDiscoveryHandler polarisDiscoveryHandler() { + return new PolarisDiscoveryHandler(); + } + + @PostConstruct + public void init() { + if (null != polarisDiscoveryProperties) { + registerEnabled |= polarisDiscoveryProperties.isRegisterEnabled(); + discoveryEnabled |= polarisDiscoveryProperties.isEnabled(); + } + if (null != consulContextProperties && consulContextProperties.isEnabled()) { + registerEnabled |= consulContextProperties.isRegister(); + discoveryEnabled |= consulContextProperties.isDiscoveryEnabled(); + } + } + + public boolean isRegisterEnabled() { + return registerEnabled; + } + + public boolean isDiscoveryEnabled() { + return discoveryEnabled; + } + +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java new file mode 100644 index 000000000..22ceda9ad --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java @@ -0,0 +1,34 @@ +/* + * 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; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * Common configuration of discovery. + * + * @author Haotian Zhang + */ +@Configuration +@ConditionalOnProperty("spring.cloud.polaris.enabled") +@Import(DiscoveryPropertiesAutoConfiguration.class) +public class DiscoveryPropertiesBootstrapAutoConfiguration { + +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java index c6647f156..37b0ad45f 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java @@ -20,11 +20,18 @@ package com.tencent.cloud.polaris; import javax.annotation.PostConstruct; +import com.tencent.cloud.common.constant.ContextConstant; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl; +import com.tencent.polaris.factory.config.provider.RegisterConfigImpl; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; /** @@ -75,10 +82,15 @@ public class PolarisDiscoveryProperties { @Value("${server.port:}") private int port; + /** + * Enable polaris discovery or not. + */ + private Boolean enabled = true; + /** * If instance registered. */ - @Value("${spring.cloud.polaris.discovery.register.enabled:#{true}}") + @Value("${spring.cloud.polaris.discovery.register:#{true}}") private Boolean registerEnabled; /** @@ -150,6 +162,14 @@ public class PolarisDiscoveryProperties { this.service = service; } + public Boolean isEnabled() { + return enabled; + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } + public boolean isRegisterEnabled() { return registerEnabled; } @@ -208,4 +228,39 @@ public class PolarisDiscoveryProperties { + healthCheckUrl + ", environment=" + environment + '}'; } + @Bean + @ConditionalOnMissingBean + public PolarisDiscoveryConfigModifier polarisDiscoveryConfigModifier() { + return new PolarisDiscoveryConfigModifier(); + } + + private static class PolarisDiscoveryConfigModifier implements PolarisConfigModifier { + + private final String ID = "polaris"; + + @Autowired(required = false) + private PolarisDiscoveryProperties polarisDiscoveryProperties; + + @Override + public void modify(ConfigurationImpl configuration) { + if (polarisDiscoveryProperties != null) { + DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl(); + discoveryConfig.setServerConnectorId(ID); + discoveryConfig.setEnable(polarisDiscoveryProperties.enabled); + configuration.getConsumer().getDiscoveries().add(discoveryConfig); + + RegisterConfigImpl registerConfig = new RegisterConfigImpl(); + registerConfig.setServerConnectorId(ID); + registerConfig.setEnable(polarisDiscoveryProperties.registerEnabled); + configuration.getProvider().getRegisters().add(registerConfig); + } + } + + @Override + public int getOrder() { + return ContextConstant.ModifierOrder.LAST; + } + + } + } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java index 7e5fe70ad..97f478683 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java @@ -22,8 +22,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; + import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; +import org.springframework.context.annotation.Conditional; /** * @author Haotian Zhang, Andrew Shan, Jie Cheng @@ -31,7 +33,8 @@ import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @ConditionalOnDiscoveryEnabled -@ConditionalOnProperty(value = "spring.cloud.polaris.discovery.enabled", matchIfMissing = true) +@ConditionalOnPolarisEnabled +@Conditional(DiscoveryEnabledCondition.class) public @interface ConditionalOnPolarisDiscoveryEnabled { } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java new file mode 100644 index 000000000..84041557f --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.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.discovery; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +/** + * Condition for checking if discovery enabled. + * + * @author Haotian Zhang + */ +public class DiscoveryEnabledCondition implements Condition { + + @Override + public boolean matches(ConditionContext conditionContext, + AnnotatedTypeMetadata annotatedTypeMetadata) { + + boolean isDiscoveryEnabled = Boolean + .parseBoolean(conditionContext.getEnvironment() + .getProperty("spring.cloud.polaris.discovery.enabled", "true")); + + boolean isConsulDiscoveryEnabled = Boolean + .parseBoolean(conditionContext.getEnvironment() + .getProperty("spring.cloud.consul.enabled", "false")) + && Boolean.parseBoolean(conditionContext.getEnvironment() + .getProperty("spring.cloud.consul.discovery.enabled", "true")); + + isDiscoveryEnabled |= isConsulDiscoveryEnabled; + return isDiscoveryEnabled; + } + +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java index 590f5a326..3c28371e8 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java @@ -18,14 +18,6 @@ package com.tencent.cloud.polaris.discovery; -import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; -import com.tencent.polaris.api.core.ConsumerAPI; -import com.tencent.polaris.api.core.ProviderAPI; -import com.tencent.polaris.api.exception.PolarisException; -import com.tencent.polaris.client.api.SDKContext; -import com.tencent.polaris.factory.api.DiscoveryAPIFactory; - import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -38,35 +30,9 @@ import org.springframework.context.annotation.Import; */ @Configuration @ConditionalOnPolarisDiscoveryEnabled -@Import({ PolarisDiscoveryClientConfiguration.class, ConsulContextProperties.class }) +@Import({ PolarisDiscoveryClientConfiguration.class }) public class PolarisDiscoveryAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public PolarisDiscoveryProperties polarisDiscoveryProperties() { - return new PolarisDiscoveryProperties(); - } - - @Bean(name = "polarisProvider") - @ConditionalOnMissingBean - public ProviderAPI polarisProvider(SDKContext polarisContext) - throws PolarisException { - return DiscoveryAPIFactory.createProviderAPIByContext(polarisContext); - } - - @Bean(name = "polarisConsumer") - @ConditionalOnMissingBean - public ConsumerAPI polarisConsumer(SDKContext polarisContext) - throws PolarisException { - return DiscoveryAPIFactory.createConsumerAPIByContext(polarisContext); - } - - @Bean - @ConditionalOnMissingBean - public PolarisDiscoveryHandler polarisDiscoveryHandler() { - return new PolarisDiscoveryHandler(); - } - @Bean @ConditionalOnMissingBean public PolarisServiceDiscovery polarisServiceDiscovery( diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java index e9b0e7fcb..cfea83cf3 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java @@ -25,13 +25,14 @@ import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.polaris.api.config.plugin.DefaultPlugins; import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl; import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl; +import com.tencent.polaris.factory.config.provider.RegisterConfigImpl; import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -42,8 +43,6 @@ import org.springframework.util.CollectionUtils; * * @author Haotian Zhang */ -@ConditionalOnExpression("'true'.equals('${spring.cloud.consul.enabled:true}')" - + " && 'true'.equals('${spring.cloud.consul.discovery.enabled:true}')") @ConfigurationProperties("spring.cloud.consul") public class ConsulContextProperties { @@ -54,7 +53,7 @@ public class ConsulContextProperties { private int port; - private boolean enabled; + private boolean enabled = false; @Value("${spring.cloud.consul.discovery.register:#{'true'}}") private boolean register; @@ -82,10 +81,22 @@ public class ConsulContextProperties { this.port = port; } + public boolean isEnabled() { + return enabled; + } + public void setEnabled(boolean enabled) { this.enabled = enabled; } + public boolean isRegister() { + return register; + } + + public boolean isDiscoveryEnabled() { + return discoveryEnabled; + } + @Bean @ConditionalOnMissingBean public ConsulConfigModifier consulConfigModifier() { @@ -94,21 +105,26 @@ public class ConsulContextProperties { private static class ConsulConfigModifier implements PolarisConfigModifier { + private final String ID = "consul"; + @Autowired(required = false) private ConsulContextProperties consulContextProperties; @Override public void modify(ConfigurationImpl configuration) { - if (consulContextProperties != null && consulContextProperties.enabled - && consulContextProperties.discoveryEnabled - && consulContextProperties.register) { + if (consulContextProperties != null && consulContextProperties.enabled) { if (CollectionUtils .isEmpty(configuration.getGlobal().getServerConnectors())) { configuration.getGlobal().setServerConnectors(new ArrayList<>()); } - configuration.getGlobal().getServerConnectors() - .add(configuration.getGlobal().getServerConnector()); + if (CollectionUtils + .isEmpty(configuration.getGlobal().getServerConnectors()) + && null != configuration.getGlobal().getServerConnector()) { + configuration.getGlobal().getServerConnectors() + .add(configuration.getGlobal().getServerConnector()); + } ServerConnectorConfigImpl serverConnectorConfig = new ServerConnectorConfigImpl(); + serverConnectorConfig.setId(ID); serverConnectorConfig.setAddresses( Collections.singletonList(consulContextProperties.host + ":" + consulContextProperties.port)); @@ -131,6 +147,16 @@ public class ConsulContextProperties { } configuration.getGlobal().getServerConnectors() .add(serverConnectorConfig); + + DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl(); + discoveryConfig.setServerConnectorId(ID); + discoveryConfig.setEnable(consulContextProperties.discoveryEnabled); + configuration.getConsumer().getDiscoveries().add(discoveryConfig); + + RegisterConfigImpl registerConfig = new RegisterConfigImpl(); + registerConfig.setServerConnectorId(ID); + registerConfig.setEnable(consulContextProperties.register); + configuration.getProvider().getRegisters().add(registerConfig); } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/ConditionalOnPolarisRegisterEnabled.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/ConditionalOnPolarisRegisterEnabled.java new file mode 100644 index 000000000..440312cab --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/ConditionalOnPolarisRegisterEnabled.java @@ -0,0 +1,38 @@ +/* + * 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.registry; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; + +import org.springframework.context.annotation.Conditional; + +/** + * @author Haotian Zhang, Andrew Shan, Jie Cheng + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnPolarisEnabled +@Conditional(RegisterEnabledCondition.class) +public @interface ConditionalOnPolarisRegisterEnabled { + +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java index 727b38a58..bfba996f7 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java @@ -61,7 +61,7 @@ public class PolarisAutoServiceRegistration @Override protected void register() { - if (!this.registration.getPolarisProperties().isRegisterEnabled()) { + if (!this.registration.isRegisterEnabled()) { log.debug("Registration disabled."); return; } @@ -73,7 +73,7 @@ public class PolarisAutoServiceRegistration @Override protected void registerManagement() { - if (!this.registration.getPolarisProperties().isRegisterEnabled()) { + if (!this.registration.isRegisterEnabled()) { return; } super.registerManagement(); @@ -87,7 +87,7 @@ public class PolarisAutoServiceRegistration @Override protected boolean isEnabled() { - return this.registration.getPolarisProperties().isRegisterEnabled(); + return this.registration.isRegisterEnabled(); } @Override diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 38fedcb0b..9ca6b9e08 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -22,6 +22,7 @@ import java.net.URI; import java.util.Map; import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.polaris.client.api.SDKContext; import org.apache.commons.lang.StringUtils; @@ -37,12 +38,16 @@ import org.springframework.cloud.client.serviceregistry.Registration; */ public class PolarisRegistration implements Registration, ServiceInstance { + private final DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration; + private final PolarisDiscoveryProperties polarisDiscoveryProperties; private final SDKContext polarisContext; - public PolarisRegistration(PolarisDiscoveryProperties polarisDiscoveryProperties, - SDKContext context) { + public PolarisRegistration( + DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, + PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context) { + this.discoveryPropertiesAutoConfiguration = discoveryPropertiesAutoConfiguration; this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisContext = context; } @@ -86,6 +91,10 @@ public class PolarisRegistration implements Registration, ServiceInstance { return polarisDiscoveryProperties; } + public boolean isRegisterEnabled() { + return discoveryPropertiesAutoConfiguration.isRegisterEnabled(); + } + @Override public String toString() { return "PolarisRegistration{" + "polarisDiscoveryProperties=" diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 2f5c9f6d6..83f8967f0 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -71,11 +71,12 @@ public class PolarisServiceRegistry implements ServiceRegistry { this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryHandler = polarisDiscoveryHandler; this.metadataLocalProperties = metadataLocalProperties; - + if (polarisDiscoveryProperties.isHeartbeatEnabled()) { - this.heartbeatExecutor = Executors - .newSingleThreadScheduledExecutor(new NamedThreadFactory("spring-cloud-heartbeat")); - } else { + this.heartbeatExecutor = Executors.newSingleThreadScheduledExecutor( + new NamedThreadFactory("spring-cloud-heartbeat")); + } + else { this.heartbeatExecutor = null; } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java index 2e43df07e..3ec88d554 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java @@ -19,8 +19,8 @@ package com.tencent.cloud.polaris.registry; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; +import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.polaris.client.api.SDKContext; @@ -42,7 +42,7 @@ import org.springframework.context.annotation.Configuration; */ @Configuration @EnableConfigurationProperties -@ConditionalOnPolarisDiscoveryEnabled +@ConditionalOnPolarisRegisterEnabled @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) @AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class, AutoServiceRegistrationAutoConfiguration.class, @@ -61,8 +61,10 @@ public class PolarisServiceRegistryAutoConfiguration { @Bean @ConditionalOnBean(AutoServiceRegistrationProperties.class) public PolarisRegistration polarisRegistration( + DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context) { - return new PolarisRegistration(polarisDiscoveryProperties, context); + return new PolarisRegistration(discoveryPropertiesAutoConfiguration, + polarisDiscoveryProperties, context); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java new file mode 100644 index 000000000..c7820fb6a --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java @@ -0,0 +1,47 @@ +/* + * 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.registry; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +/** + * Condition for checking if register enabled. + * + * @author Haotian Zhang + */ +public class RegisterEnabledCondition implements Condition { + + @Override + public boolean matches(ConditionContext conditionContext, + AnnotatedTypeMetadata annotatedTypeMetadata) { + boolean isRegisterEnabled = Boolean.parseBoolean(conditionContext.getEnvironment() + .getProperty("spring.cloud.polaris.discovery.register", "true")); + + boolean isConsulRegisterEnabled = Boolean + .parseBoolean(conditionContext.getEnvironment() + .getProperty("spring.cloud.consul.enabled", "false")) + && Boolean.parseBoolean(conditionContext.getEnvironment() + .getProperty("spring.cloud.consul.discovery.register", "true")); + + isRegisterEnabled |= isConsulRegisterEnabled; + return isRegisterEnabled; + } + +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java index 15d72323f..866cdcf1d 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java @@ -56,7 +56,8 @@ public class PolarisServerList extends AbstractServerList { } private List getServers() { - InstancesResponse allInstances = polarisDiscoveryHandler.getFilteredInstances(serviceId); + InstancesResponse allInstances = polarisDiscoveryHandler + .getFilteredInstances(serviceId); ServiceInstances serviceInstances = allInstances.toServiceInstances(); List polarisServers = new ArrayList<>(); for (Instance instance : serviceInstances.getInstances()) { diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 6608a3b97..eb160ae93 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -18,6 +18,12 @@ "defaultValue": true, "description": "enable polaris discovery or not." }, + { + "name": "spring.cloud.polaris.discovery.register", + "type": "java.lang.Boolean", + "defaultValue": true, + "description": "enable polaris registration or not." + }, { "name": "spring.cloud.polaris.discovery.health-check-url", "type": "java.lang.String", diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories index a9cafd058..83b8e001f 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories @@ -1,4 +1,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration,\ com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration,\ com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration,\ com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration 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 517c7e464..fc6ed91fe 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 @@ -19,7 +19,7 @@ package com.tencent.cloud.polaris.discovery; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.test.mock.discovery.NamingServer; @@ -47,10 +47,11 @@ public class PolarisDiscoveryAutoConfigurationTest { private static NamingServer namingServer; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, - PolarisDiscoveryAutoConfiguration.class, - PolarisDiscoveryClientConfiguration.class, - PolarisContextConfiguration.class)) + .withConfiguration( + AutoConfigurations.of(PolarisContextAutoConfiguration.class, + PolarisDiscoveryAutoConfiguration.class, + PolarisDiscoveryClientConfiguration.class, + PolarisContextAutoConfiguration.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 c2bb22e83..0cc1efe42 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 @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.discovery; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.test.mock.discovery.NamingServer; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -43,9 +43,10 @@ public class PolarisDiscoveryClientConfigurationTest { private static NamingServer namingServer; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, - PolarisDiscoveryClientConfiguration.class, - PolarisContextConfiguration.class)) + .withConfiguration( + AutoConfigurations.of(PolarisContextAutoConfiguration.class, + PolarisDiscoveryClientConfiguration.class, + PolarisContextAutoConfiguration.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 abd3ac98b..56ff83db7 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 @@ -19,7 +19,7 @@ package com.tencent.cloud.polaris.discovery; import java.util.List; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.test.mock.discovery.NamingServer; @@ -50,11 +50,12 @@ public class PolarisServiceDiscoveryTest { private static NamingServer namingServer; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, + .withConfiguration(AutoConfigurations.of( + PolarisContextAutoConfiguration.class, PolarisServiceDiscoveryTest.PolarisPropertiesConfiguration.class, PolarisDiscoveryClientConfiguration.class, PolarisDiscoveryAutoConfiguration.class, - PolarisContextConfiguration.class)) + PolarisContextAutoConfiguration.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/registry/PolarisServiceRegistryAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java index 12f7d6a78..e1645d80c 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.registry; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.polaris.test.mock.discovery.NamingServer; @@ -46,9 +46,10 @@ public class PolarisServiceRegistryAutoConfigurationTest { private static NamingServer namingServer; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, - PolarisServiceRegistryAutoConfiguration.class, - PolarisDiscoveryClientConfiguration.class)) + .withConfiguration( + AutoConfigurations.of(PolarisContextAutoConfiguration.class, + PolarisServiceRegistryAutoConfiguration.class, + PolarisDiscoveryClientConfiguration.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/registry/PolarisServiceRegistryTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java index 76aa9e6f0..4371e32ac 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.registry; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.polaris.api.pojo.ServiceKey; @@ -50,10 +50,11 @@ public class PolarisServiceRegistryTest { private static NamingServer namingServer; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, - PolarisPropertiesConfiguration.class, - PolarisDiscoveryClientConfiguration.class, - PolarisDiscoveryAutoConfiguration.class)) + .withConfiguration( + AutoConfigurations.of(PolarisContextAutoConfiguration.class, + PolarisPropertiesConfiguration.class, + PolarisDiscoveryClientConfiguration.class, + PolarisDiscoveryAutoConfiguration.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 eea153169..d429da7b4 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,7 +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.context.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import org.junit.Test; @@ -48,7 +48,7 @@ public class PolarisRibbonServerListConfigurationTest { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisRibbonClientTest.class, PolarisDiscoveryClientConfiguration.class, - PolarisContextConfiguration.class)) + PolarisContextAutoConfiguration.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 92cc4fb30..9cd01db87 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 @@ -21,7 +21,7 @@ import java.util.List; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.Server; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; @@ -55,11 +55,12 @@ public class PolarisServerListTest { private static NamingServer namingServer; private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class, - PolarisServerListTest.PolarisPropertiesConfiguration.class, - PolarisDiscoveryClientConfiguration.class, - PolarisDiscoveryAutoConfiguration.class, - PolarisContextConfiguration.class)) + .withConfiguration( + AutoConfigurations.of(PolarisContextAutoConfiguration.class, + PolarisServerListTest.PolarisPropertiesConfiguration.class, + PolarisDiscoveryClientConfiguration.class, + PolarisDiscoveryAutoConfiguration.class, + PolarisContextAutoConfiguration.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-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java index 39499e538..f45b5cd0f 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java @@ -13,11 +13,11 @@ * 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.ratelimit.config; +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckReactiveFilter; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilter; @@ -45,6 +45,7 @@ import static javax.servlet.DispatcherType.REQUEST; * @author Haotian Zhang */ @Configuration +@ConditionalOnPolarisEnabled @ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true) public class RateLimitConfiguration { diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java index 833f75af0..0db616f7f 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java @@ -91,8 +91,10 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { if (!CollectionUtils.isEmpty(customLabels)) { labels.putAll(customLabels); } - } catch (Throwable e) { - LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); + } + catch (Throwable e) { + LOG.error("resolve custom label failed. resolver = {}", + labelResolver.getClass().getName(), e); } } @@ -104,8 +106,9 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { ServerHttpResponse response = exchange.getResponse(); response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); - DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write( - RateLimitConstant.QUOTA_LIMITED_INFO.getBytes(StandardCharsets.UTF_8)); + DataBuffer dataBuffer = response.bufferFactory().allocateBuffer() + .write(RateLimitConstant.QUOTA_LIMITED_INFO + .getBytes(StandardCharsets.UTF_8)); return response.writeWith(Mono.just(dataBuffer)); } } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java index 7b36652a4..85706d944 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java @@ -57,9 +57,11 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { .getLogger(QuotaCheckServletFilter.class); private final LimitAPI limitAPI; + private final PolarisRateLimiterLabelServletResolver labelResolver; - public QuotaCheckServletFilter(LimitAPI limitAPI, PolarisRateLimiterLabelServletResolver labelResolver) { + public QuotaCheckServletFilter(LimitAPI limitAPI, + PolarisRateLimiterLabelServletResolver labelResolver) { this.limitAPI = limitAPI; this.labelResolver = labelResolver; } @@ -87,8 +89,10 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { if (!CollectionUtils.isEmpty(customLabels)) { labels.putAll(customLabels); } - } catch (Throwable e) { - LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); + } + catch (Throwable e) { + LOG.error("resolve custom label failed. resolver = {}", + labelResolver.getClass().getName(), e); } } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java index 790c7c0d5..4ebcc1921 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java @@ -31,7 +31,6 @@ public interface PolarisRateLimiterLabelReactiveResolver { /** * Resolve custom label from request. - * * @param exchange the http request * @return resolved labels */ diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java index 8fe859092..253b67b5c 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java @@ -31,7 +31,6 @@ public interface PolarisRateLimiterLabelServletResolver { /** * Resolve custom label from request. - * * @param request the http request * @return resolved labels */ diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java index 00ac5badf..64960f7e4 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java @@ -17,6 +17,7 @@ package com.tencent.cloud.polaris.router.config; +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.api.RouterAPIFactory; @@ -38,6 +39,7 @@ import org.springframework.context.annotation.Configuration; */ @Configuration @EnableConfigurationProperties +@ConditionalOnPolarisEnabled @ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true) @AutoConfigureAfter(RibbonAutoConfiguration.class) @RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class) diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java index fb805d9ad..e65b0a2ea 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java @@ -57,19 +57,21 @@ public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule { @Override public Server choose(Object key) { - //1. filter by router + // 1. filter by router List serversAfterRouter = getLoadBalancer().getReachableServers(); if (CollectionUtils.isEmpty(serversAfterRouter)) { return null; } - ServiceInstances serviceInstances = transferServersToServiceInstances(serversAfterRouter); + ServiceInstances serviceInstances = transferServersToServiceInstances( + serversAfterRouter); - //2. filter by load balance + // 2. filter by load balance ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest(); request.setDstInstances(serviceInstances); request.setLbPolicy(POLICY); - ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request); + ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter + .processLoadBalance(request); Instance targetInstance = processLoadBalanceResponse.getTargetInstance(); return new PolarisServer(serviceInstances, targetInstance); @@ -90,7 +92,8 @@ public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule { } } - ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName); + ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, + serviceName); return new DefaultServiceInstances(serviceKey, instances); } 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 af4645e6e..4a3ab3384 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,7 +17,7 @@ package com.tencent.cloud.polaris.router.config; -import com.tencent.cloud.polaris.context.PolarisContextConfiguration; +import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.router.api.core.RouterAPI; import org.junit.Test; @@ -40,7 +40,7 @@ public class PolarisRibbonAutoConfigurationTest { private ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class, PolarisRibbonAutoConfiguration.class, - PolarisContextConfiguration.class)) + PolarisContextAutoConfiguration.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/metadata/MetadataContext.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java index 573574a78..6f06e811b 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java @@ -55,17 +55,20 @@ public class MetadataContext { private final Map systemMetadata; static { - String namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace"); + String namespace = ApplicationContextAwareUtils + .getProperties("spring.cloud.polaris.namespace"); if (StringUtils.isEmpty(namespace)) { - namespace = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.discovery.namespace", "default"); + namespace = ApplicationContextAwareUtils + .getProperties("spring.cloud.polaris.discovery.namespace", "default"); } LOCAL_NAMESPACE = namespace; - String serviceName = ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service"); + String serviceName = ApplicationContextAwareUtils + .getProperties("spring.cloud.polaris.service"); if (StringUtils.isEmpty(serviceName)) { serviceName = ApplicationContextAwareUtils.getProperties( - "spring.cloud.polaris.discovery.service", - ApplicationContextAwareUtils.getProperties("spring.application.name", null)); + "spring.cloud.polaris.discovery.service", ApplicationContextAwareUtils + .getProperties("spring.application.name", null)); } LOCAL_SERVICE = serviceName; } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 26afaa987..10caaedf1 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -46,6 +46,14 @@ https://github.com/SkyeBeFreeman/ + + lepdou + lepdou + lepdou@126.com + Tencent + https://github.com/lepdou + + Andrew Shan samshan08@126.com @@ -63,7 +71,7 @@ 1.3.0-Greenwich-SNAPSHOT - 1.3.1 + 1.4.0 1.2.7 @@ -73,9 +81,8 @@ 3.2.0 - 3.1.1 1.2.7 - 1.6 + 3.0.1 diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml index 08b117235..c47268676 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml @@ -7,6 +7,9 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true + circuitbreaker: + enabled: true feign: hystrix: enabled: true diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml index ee986b845..2a28f8cf4 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml @@ -7,4 +7,5 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true is-throw-runtime-exception: false diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml index 7d1e1b776..50120e55a 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b2/src/main/resources/bootstrap.yml @@ -7,4 +7,5 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true is-throw-runtime-exception: true 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 991d77a34..868201dde 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 @@ -7,14 +7,18 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true + discovery: + enabled: true + register: true # consul: # port: 8500 # host: 127.0.0.1 # enabled: true # discovery: +# enabled: true # register: true # instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} -# enabled: true # service-name: ${spring.application.name} # ip-address: localhost # prefer-ip-address: true diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index d0cb0ec1e..74305ef1a 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -8,7 +8,10 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true discovery: + enabled: true + register: true heartbeat: enabled: true health-check-url: /discovery/service/caller/healthCheck @@ -17,11 +20,11 @@ spring: # host: 127.0.0.1 # enabled: true # discovery: +# enabled: true # register: true # health-check-path: /actuator/health # health-check-interval: 10s # instance-id: ${spring.application.name}:${server.port} -# enabled: true # service-name: ${spring.application.name} # ip-address: localhost # prefer-ip-address: true diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml index d41fcf960..b820b2ee6 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml @@ -14,6 +14,7 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true gateway: discovery: locator: diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java index 837de09c5..f441f4d2b 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java @@ -30,18 +30,19 @@ import org.springframework.stereotype.Component; /** * resolver custom label from request. * - *@author lepdou 2022-03-31 + * @author lepdou 2022-03-31 */ @Component public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver { @Override public Map resolve(HttpServletRequest request) { - //rate limit by some request params. such as query params, headers .. + // rate limit by some request params. such as query params, headers .. Map labels = new HashMap<>(); labels.put("user", "zhangsan"); return labels; } + } diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml index 6b8ba88a0..8825b0cea 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml @@ -7,3 +7,6 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true + ratelimit: + enabled: true diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java index 4692d6847..6b481e472 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java @@ -23,7 +23,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Router callee application. - *@author lepdou 2022-04-06 + * + * @author lepdou 2022-04-06 */ @SpringBootApplication public class RouterCalleeApplication1 { @@ -31,4 +32,5 @@ public class RouterCalleeApplication1 { public static void main(String[] args) { SpringApplication.run(RouterCalleeApplication1.class, args); } + } diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java index 4a9f19394..f02f1887d 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java @@ -49,4 +49,5 @@ public class RouterCalleeController { LOG.info("Discovery Service Callee [{}] is called.", port); return String.format("Discovery Service Callee [%s] is called.", port); } + } diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml index 1d352ec85..dabaa7577 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml @@ -11,3 +11,4 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java index 5f0bc6300..9664f46d3 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java @@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Router callee application. + * * @author lepdou 2022-04-06 */ @SpringBootApplication @@ -31,4 +32,5 @@ public class RouterCalleeApplication2 { public static void main(String[] args) { SpringApplication.run(RouterCalleeApplication2.class, args); } + } diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml index 138c119f6..6ba79be1f 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml @@ -11,3 +11,4 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java index f7b114a41..824a1226f 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java @@ -29,7 +29,7 @@ import org.springframework.web.client.RestTemplate; /** * Router caller application. * - *@author lepdou 2022-04-06 + * @author lepdou 2022-04-06 */ @SpringBootApplication @EnableDiscoveryClient @@ -45,4 +45,5 @@ public class RouterCallerApplication { public RestTemplate restTemplate() { return new RestTemplate(); } + } diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml index 0c90a3d48..ff1b89568 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml @@ -7,3 +7,6 @@ spring: polaris: address: grpc://127.0.0.1:8091 namespace: default + enabled: true + loadbalancer: + enabled: true diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ConditionalOnPolarisEnabled.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ConditionalOnPolarisEnabled.java new file mode 100644 index 000000000..7d2b960b9 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ConditionalOnPolarisEnabled.java @@ -0,0 +1,37 @@ +/* + * 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 java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * Condition that if Polaris enabled. + * + * @author Haotian Zhang + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(value = "spring.cloud.polaris.enabled", matchIfMissing = true) +public @interface ConditionalOnPolarisEnabled { + +} 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/PolarisContextAutoConfiguration.java similarity index 92% rename from spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextConfiguration.java rename to spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfiguration.java index 9b4d3c5f3..11e081287 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/PolarisContextAutoConfiguration.java @@ -26,12 +26,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; /** - * Configuration for Polaris {@link SDKContext}. + * Autoconfiguration for Polaris {@link SDKContext}. * * @author Haotian Zhang */ +@ConditionalOnPolarisEnabled @EnableConfigurationProperties({ PolarisContextProperties.class }) -public class PolarisContextConfiguration { +public class PolarisContextAutoConfiguration { @Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy") @ConditionalOnMissingBean diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextBootstrapAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextBootstrapAutoConfiguration.java new file mode 100644 index 000000000..c5b4efbd9 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextBootstrapAutoConfiguration.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.context; + +import com.tencent.polaris.client.api.SDKContext; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Import; + +/** + * Bootstrap autoconfiguration for Polaris {@link SDKContext}. + * + * @author Haotian Zhang + */ +@ConditionalOnProperty("spring.cloud.polaris.enabled") +@Import(PolarisContextAutoConfiguration.class) +public class PolarisContextBootstrapAutoConfiguration { + +} 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 47602434a..a7942a349 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 @@ -54,6 +54,11 @@ public class PolarisContextProperties { @Value("${spring.cloud.polaris.local-ip-address:}") private String localIpAddress; + /** + * If polaris enabled. + */ + private Boolean enabled; + /** * polaris namespace. */ @@ -111,6 +116,14 @@ public class PolarisContextProperties { this.localIpAddress = localIpAddress; } + public Boolean getEnabled() { + return enabled; + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } + public String getNamespace() { return namespace; } @@ -126,4 +139,5 @@ public class PolarisContextProperties { void setService(String service) { this.service = service; } + } 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/additional-spring-configuration-metadata.json similarity index 83% rename from spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring-configuration-metadata.json rename to spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json index e6e2ae7fa..52c48aa8b 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/additional-spring-configuration-metadata.json @@ -27,6 +27,13 @@ "default": "${spring.application.name}", "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" }, + { + "name": "spring.cloud.polaris.enabled", + "type": "java.lang.Boolean", + "description": "polaris enabled", + "default": "true", + "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + }, { "name": "spring.cloud.polaris.local-ip-address", "type": "java.lang.String", 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 5b3b188e7..b53316b9a 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 +1,4 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tencent.cloud.polaris.context.PolarisContextConfiguration +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.polaris.context.PolarisContextBootstrapAutoConfiguration diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextConfigurationTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java similarity index 91% rename from spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextConfigurationTest.java rename to spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java index d81c849be..0f8ca746d 100644 --- a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextConfigurationTest.java +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java @@ -28,11 +28,12 @@ import org.springframework.cloud.commons.util.UtilAutoConfiguration; /** * @author liaochuntao */ -public class PolarisContextConfigurationTest { +public class PolarisContextAutoConfigurationTest { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class)) - .withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class)) + .withConfiguration( + AutoConfigurations.of(PolarisContextAutoConfiguration.class)) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:8083"); @Test 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 3a60549f5..0aa4682c4 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 @@ -31,7 +31,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = PolarisContextApplication.class, properties = { "spring.config.location = classpath:bootstrap.yml" }) -@ImportAutoConfiguration({ PolarisContextConfiguration.class }) +@ImportAutoConfiguration({ PolarisContextAutoConfiguration.class }) public class PolarisContextGetHostTest { @Autowired