diff --git a/CHANGELOG.md b/CHANGELOG.md index 9be8aa0b4..c2bb1e4fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,3 +41,4 @@ - [feat:support concurrency rate limit.](https://github.com/Tencent/spring-cloud-tencent/pull/1455) - [feat:support auth.](https://github.com/Tencent/spring-cloud-tencent/pull/1479) - [feat:upgrade trace plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1480) +- [feat:support smooth upgrade from tsf.](https://github.com/Tencent/spring-cloud-tencent/pull/1482) diff --git a/spring-cloud-starter-tencent-all/pom.xml b/spring-cloud-starter-tencent-all/pom.xml index a2a5e53f0..31c46e67a 100644 --- a/spring-cloud-starter-tencent-all/pom.xml +++ b/spring-cloud-starter-tencent-all/pom.xml @@ -59,6 +59,17 @@ com.tencent.cloud spring-cloud-starter-tencent-polaris-auth + + + com.tencent.cloud + spring-cloud-starter-tencent-fault-tolerance + + + org.springframework.boot + spring-boot-starter-logging + + + diff --git a/spring-cloud-starter-tencent-metadata-transfer/pom.xml b/spring-cloud-starter-tencent-metadata-transfer/pom.xml index ca8350c50..119ddc843 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/pom.xml +++ b/spring-cloud-starter-tencent-metadata-transfer/pom.xml @@ -15,11 +15,6 @@ - - com.tencent.cloud - spring-cloud-tencent-commons - - com.tencent.cloud spring-cloud-tencent-rpc-enhancement @@ -56,7 +51,7 @@ spring-cloud-starter-loadbalancer test - + com.tencent.polaris polaris-test-mock-discovery diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptorTest.java index ccd89bfbb..cb461d789 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptorTest.java @@ -23,8 +23,8 @@ import java.util.Map; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.EnhancedRestTemplateInterceptor; import com.tencent.cloud.rpc.enhancement.plugin.DefaultEnhancedPluginRunner; -import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilterTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilterTest.java index f0c1eb8d0..6941dfc34 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilterTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilterTest.java @@ -27,8 +27,8 @@ import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; +import com.tencent.cloud.rpc.enhancement.instrument.scg.EnhancedGatewayGlobalFilter; import com.tencent.cloud.rpc.enhancement.plugin.DefaultEnhancedPluginRunner; -import com.tencent.cloud.rpc.enhancement.scg.EnhancedGatewayGlobalFilter; import org.assertj.core.util.Maps; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; diff --git a/spring-cloud-starter-tencent-polaris-auth/src/main/java/org/springframework/tsf/auth/annotation/EnableTsfAuth.java b/spring-cloud-starter-tencent-polaris-auth/src/main/java/org/springframework/tsf/auth/annotation/EnableTsfAuth.java new file mode 100644 index 000000000..f84e640de --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-auth/src/main/java/org/springframework/tsf/auth/annotation/EnableTsfAuth.java @@ -0,0 +1,40 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.tsf.auth.annotation; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfAuth { + +} diff --git a/spring-cloud-starter-tencent-polaris-auth/src/test/java/com/tencent/cloud/polaris/auth/config/PolarisAuthPropertiesTest.java b/spring-cloud-starter-tencent-polaris-auth/src/test/java/com/tencent/cloud/polaris/auth/config/PolarisAuthPropertiesTest.java new file mode 100644 index 000000000..9c7493f40 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-auth/src/test/java/com/tencent/cloud/polaris/auth/config/PolarisAuthPropertiesTest.java @@ -0,0 +1,45 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.auth.config; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link PolarisAuthProperties}. + * + * @author Haotian Zhang + */ +public class PolarisAuthPropertiesTest { + + private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PolarisAuthPropertiesAutoConfiguration.class)) + .withPropertyValues("spring.cloud.polaris.auth.enabled=false"); + + @Test + public void testGetAndSet() { + this.applicationContextRunner.run(context -> { + PolarisAuthProperties properties = context.getBean(PolarisAuthProperties.class); + assertThat(properties.isEnabled()).isFalse(); + }); + } +} diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/ReactivePolarisCircuitBreaker.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/ReactivePolarisCircuitBreaker.java index e67c58506..d7e171b0e 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/ReactivePolarisCircuitBreaker.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/ReactivePolarisCircuitBreaker.java @@ -21,7 +21,7 @@ import java.util.function.Function; import com.tencent.cloud.polaris.circuitbreaker.common.PolarisCircuitBreakerConfigBuilder; import com.tencent.cloud.polaris.circuitbreaker.common.PolarisResultToErrorCode; -import com.tencent.cloud.polaris.circuitbreaker.reactor.PolarisCircuitBreakerReactorTransformer; +import com.tencent.cloud.polaris.circuitbreaker.instrument.reactor.PolarisCircuitBreakerReactorTransformer; import com.tencent.cloud.polaris.circuitbreaker.util.PolarisCircuitBreakerUtils; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.ServiceKey; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/GatewayPolarisCircuitBreakerAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/GatewayPolarisCircuitBreakerAutoConfiguration.java index a84942e56..27cbd0183 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/GatewayPolarisCircuitBreakerAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/GatewayPolarisCircuitBreakerAutoConfiguration.java @@ -18,7 +18,7 @@ package com.tencent.cloud.polaris.circuitbreaker.config; import com.tencent.cloud.polaris.circuitbreaker.ReactivePolarisCircuitBreakerFactory; -import com.tencent.cloud.polaris.circuitbreaker.gateway.PolarisCircuitBreakerFilterFactory; +import com.tencent.cloud.polaris.circuitbreaker.instrument.gateway.PolarisCircuitBreakerFilterFactory; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; @@ -44,8 +44,8 @@ import org.springframework.web.reactive.DispatcherHandler; */ @Configuration(proxyBeanMethods = false) @ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true) -@AutoConfigureAfter({ReactivePolarisCircuitBreakerAutoConfiguration.class }) -@ConditionalOnClass({ DispatcherHandler.class, ReactivePolarisCircuitBreakerAutoConfiguration.class, +@AutoConfigureAfter({ReactivePolarisCircuitBreakerAutoConfiguration.class}) +@ConditionalOnClass({DispatcherHandler.class, ReactivePolarisCircuitBreakerAutoConfiguration.class, ReactiveCircuitBreakerFactory.class, ReactivePolarisCircuitBreakerFactory.class, GatewayAutoConfiguration.class}) public class GatewayPolarisCircuitBreakerAutoConfiguration { diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfiguration.java index aab020b48..e4d45bab8 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfiguration.java @@ -22,9 +22,9 @@ import java.util.List; import com.tencent.cloud.polaris.circuitbreaker.PolarisCircuitBreakerFactory; import com.tencent.cloud.polaris.circuitbreaker.common.CircuitBreakerConfigModifier; +import com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate.PolarisCircuitBreakerRestTemplateBeanPostProcessor; import com.tencent.cloud.polaris.circuitbreaker.reporter.ExceptionCircuitBreakerReporter; import com.tencent.cloud.polaris.circuitbreaker.reporter.SuccessCircuitBreakerReporter; -import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreakerRestTemplateBeanPostProcessor; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerFeignClientAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerFeignClientAutoConfiguration.java index d56a33af0..160ac400a 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerFeignClientAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerFeignClientAutoConfiguration.java @@ -17,9 +17,9 @@ package com.tencent.cloud.polaris.circuitbreaker.config; -import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisCircuitBreakerNameResolver; -import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignCircuitBreaker; -import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignCircuitBreakerTargeter; +import com.tencent.cloud.polaris.circuitbreaker.instrument.feign.PolarisCircuitBreakerNameResolver; +import com.tencent.cloud.polaris.circuitbreaker.instrument.feign.PolarisFeignCircuitBreaker; +import com.tencent.cloud.polaris.circuitbreaker.instrument.feign.PolarisFeignCircuitBreakerTargeter; import feign.Feign; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -39,7 +39,7 @@ import org.springframework.context.annotation.Scope; * @author seansyyu 2023-02-28 */ @Configuration(proxyBeanMethods = false) -@ConditionalOnClass({ Feign.class, FeignClientFactoryBean.class }) +@ConditionalOnClass({Feign.class, FeignClientFactoryBean.class}) @ConditionalOnPolarisCircuitBreakerEnabled public class PolarisCircuitBreakerFeignClientAutoConfiguration { diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFallbackFactory.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerFallbackFactory.java similarity index 97% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFallbackFactory.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerFallbackFactory.java index 648ffd841..4429fcabc 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFallbackFactory.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerFallbackFactory.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import java.io.IOException; import java.lang.reflect.Method; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerNameResolver.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerNameResolver.java similarity index 97% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerNameResolver.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerNameResolver.java index 9293b6667..191d2c2a6 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerNameResolver.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerNameResolver.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import java.lang.reflect.Method; import java.net.URI; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreaker.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreaker.java similarity index 97% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreaker.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreaker.java index 847da9911..b0ef6b41c 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreaker.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreaker.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import feign.Feign; import feign.Target; @@ -47,15 +47,13 @@ public final class PolarisFeignCircuitBreaker { */ public static final class Builder extends Feign.Builder { - public Builder() { - } - private CircuitBreakerFactory circuitBreakerFactory; - private String feignClientName; - private CircuitBreakerNameResolver circuitBreakerNameResolver; + public Builder() { + } + public PolarisFeignCircuitBreaker.Builder circuitBreakerFactory(CircuitBreakerFactory circuitBreakerFactory) { this.circuitBreakerFactory = circuitBreakerFactory; return this; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerInvocationHandler.java similarity index 99% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerInvocationHandler.java index 19c7d0eba..067dd10f1 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerInvocationHandler.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; @@ -77,6 +77,24 @@ public class PolarisFeignCircuitBreakerInvocationHandler implements InvocationHa this.decoder = decoder; } + /** + * If the method param of {@link InvocationHandler#invoke(Object, Method, Object[])} + * is not accessible, i.e in a package-private interface, the fallback call will cause + * of access restrictions. But methods in dispatch are copied methods. So setting + * access to dispatch method doesn't take effect to the method in + * InvocationHandler.invoke. Use map to store a copy of method to invoke the fallback + * to bypass this and reducing the count of reflection calls. + * @return cached methods map for fallback invoking + */ + static Map toFallbackMethod(Map dispatch) { + Map result = new LinkedHashMap<>(); + for (Method method : dispatch.keySet()) { + method.setAccessible(true); + result.put(method, method); + } + return result; + } + @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { // early exit if the invoked method is from java.lang.Object @@ -167,24 +185,6 @@ public class PolarisFeignCircuitBreakerInvocationHandler implements InvocationHa }; } - /** - * If the method param of {@link InvocationHandler#invoke(Object, Method, Object[])} - * is not accessible, i.e in a package-private interface, the fallback call will cause - * of access restrictions. But methods in dispatch are copied methods. So setting - * access to dispatch method doesn't take effect to the method in - * InvocationHandler.invoke. Use map to store a copy of method to invoke the fallback - * to bypass this and reducing the count of reflection calls. - * @return cached methods map for fallback invoking - */ - static Map toFallbackMethod(Map dispatch) { - Map result = new LinkedHashMap<>(); - for (Method method : dispatch.keySet()) { - method.setAccessible(true); - result.put(method, method); - } - return result; - } - @Override public boolean equals(Object obj) { if (obj instanceof PolarisFeignCircuitBreakerInvocationHandler) { diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerTargeter.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerTargeter.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerTargeter.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerTargeter.java index f64cc53a4..60a8df756 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerTargeter.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerTargeter.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import feign.Feign; import feign.Target; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerFilterFactory.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/gateway/PolarisCircuitBreakerFilterFactory.java similarity index 99% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerFilterFactory.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/gateway/PolarisCircuitBreakerFilterFactory.java index d46f491b8..f63c39844 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerFilterFactory.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/gateway/PolarisCircuitBreakerFilterFactory.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.gateway; +package com.tencent.cloud.polaris.circuitbreaker.instrument.gateway; import java.net.URI; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerFluxOperator.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerFluxOperator.java similarity index 96% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerFluxOperator.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerFluxOperator.java index 0b0daca50..0d760c7ac 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerFluxOperator.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerFluxOperator.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.reactor; +package com.tencent.cloud.polaris.circuitbreaker.instrument.reactor; import com.tencent.polaris.circuitbreak.api.InvokeHandler; import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerMonoOperator.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerMonoOperator.java similarity index 96% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerMonoOperator.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerMonoOperator.java index 8b478b81c..129f49e79 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerMonoOperator.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerMonoOperator.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.reactor; +package com.tencent.cloud.polaris.circuitbreaker.instrument.reactor; import com.tencent.polaris.circuitbreak.api.InvokeHandler; import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerReactorSubscriber.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerReactorSubscriber.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerReactorSubscriber.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerReactorSubscriber.java index 20fdc4f3e..ea876250d 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerReactorSubscriber.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerReactorSubscriber.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.reactor; +package com.tencent.cloud.polaris.circuitbreaker.instrument.reactor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerReactorTransformer.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerReactorTransformer.java similarity index 93% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerReactorTransformer.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerReactorTransformer.java index 0d7eb74c9..0a40e2b01 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/reactor/PolarisCircuitBreakerReactorTransformer.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/reactor/PolarisCircuitBreakerReactorTransformer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.reactor; +package com.tencent.cloud.polaris.circuitbreaker.instrument.reactor; import java.util.function.Function; @@ -46,7 +46,8 @@ public class PolarisCircuitBreakerReactorTransformer implements Function((Flux) publisher, invokeHandler); } else { - throw new IllegalStateException("Publisher type is not supported: " + publisher.getClass().getCanonicalName()); + throw new IllegalStateException("Publisher type is not supported: " + publisher.getClass() + .getCanonicalName()); } } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreaker.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreaker.java similarity index 93% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreaker.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreaker.java index 3934e1bff..79d95ee96 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreaker.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreaker.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.resttemplate; +package com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -31,9 +31,10 @@ import java.lang.annotation.Target; * * @author sean yu */ -@Target({ ElementType.METHOD }) +@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented +@Deprecated(since = "2.0.0.0") public @interface PolarisCircuitBreaker { /** diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerFallback.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerFallback.java similarity index 89% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerFallback.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerFallback.java index ccbc3073b..329be80d0 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerFallback.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerFallback.java @@ -15,13 +15,14 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.resttemplate; +package com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate; /** * PolarisCircuitBreakerFallback. * * @author sean yu */ +@Deprecated(since = "2.0.0.0") public interface PolarisCircuitBreakerFallback { PolarisCircuitBreakerHttpResponse fallback(); diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerHttpResponse.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerHttpResponse.java similarity index 97% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerHttpResponse.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerHttpResponse.java index e4623a43c..6e6f1ccde 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerHttpResponse.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerHttpResponse.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.resttemplate; +package com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate; import java.io.ByteArrayInputStream; import java.io.IOException; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateBeanPostProcessor.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateBeanPostProcessor.java similarity index 61% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateBeanPostProcessor.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateBeanPostProcessor.java index 8f6bb5916..2c05c3600 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateBeanPostProcessor.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateBeanPostProcessor.java @@ -15,9 +15,11 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.resttemplate; +package com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate; -import java.util.concurrent.ConcurrentHashMap; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; @@ -26,9 +28,9 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.ApplicationContext; import org.springframework.core.type.MethodMetadata; -import org.springframework.core.type.StandardMethodMetadata; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; @@ -40,56 +42,27 @@ import org.springframework.web.client.RestTemplate; public class PolarisCircuitBreakerRestTemplateBeanPostProcessor implements MergedBeanDefinitionPostProcessor { private final ApplicationContext applicationContext; + private final Set cache = Collections.synchronizedSet(new HashSet<>()); public PolarisCircuitBreakerRestTemplateBeanPostProcessor(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } - private final ConcurrentHashMap cache = new ConcurrentHashMap<>(); - - private void checkPolarisCircuitBreakerRestTemplate(PolarisCircuitBreaker polarisCircuitBreaker) { - if ( - StringUtils.hasText(polarisCircuitBreaker.fallback()) && - !PolarisCircuitBreakerFallback.class.toGenericString().equals(polarisCircuitBreaker.fallbackClass().toGenericString()) - ) { - throw new IllegalArgumentException("PolarisCircuitBreaker's fallback and fallbackClass could not set at sametime !"); - } - } - @Override public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { if (checkAnnotated(beanDefinition, beanType, beanName)) { - PolarisCircuitBreaker polarisCircuitBreaker; - if (beanDefinition.getSource() instanceof StandardMethodMetadata) { - polarisCircuitBreaker = ((StandardMethodMetadata) beanDefinition.getSource()).getIntrospectedMethod() - .getAnnotation(PolarisCircuitBreaker.class); - } - else { - polarisCircuitBreaker = beanDefinition.getResolvedFactoryMethod() - .getAnnotation(PolarisCircuitBreaker.class); - } - checkPolarisCircuitBreakerRestTemplate(polarisCircuitBreaker); - cache.put(beanName, polarisCircuitBreaker); + cache.add(beanName); } } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - if (cache.containsKey(beanName)) { - // add interceptor for each RestTemplate with @PolarisCircuitBreaker annotation - StringBuilder interceptorBeanNamePrefix = new StringBuilder(); - PolarisCircuitBreaker polarisCircuitBreaker = cache.get(beanName); - interceptorBeanNamePrefix - .append(StringUtils.uncapitalize( - PolarisCircuitBreaker.class.getSimpleName())) - .append("_") - .append(polarisCircuitBreaker.fallback()) - .append("_") - .append(polarisCircuitBreaker.fallbackClass().getSimpleName()); + if (cache.contains(beanName)) { + String interceptorBeanNamePrefix = StringUtils.uncapitalize("PolarisCircuitBreaker"); RestTemplate restTemplate = (RestTemplate) bean; String interceptorBeanName = interceptorBeanNamePrefix + "@" + bean; CircuitBreakerFactory circuitBreakerFactory = this.applicationContext.getBean(CircuitBreakerFactory.class); - registerBean(interceptorBeanName, polarisCircuitBreaker, applicationContext, circuitBreakerFactory, restTemplate); + registerBean(interceptorBeanName, applicationContext, circuitBreakerFactory, restTemplate); PolarisCircuitBreakerRestTemplateInterceptor polarisCircuitBreakerRestTemplateInterceptor = applicationContext .getBean(interceptorBeanName, PolarisCircuitBreakerRestTemplateInterceptor.class); restTemplate.getInterceptors().add(0, polarisCircuitBreakerRestTemplateInterceptor); @@ -102,18 +75,16 @@ public class PolarisCircuitBreakerRestTemplateBeanPostProcessor implements Merge return beanName != null && beanType == RestTemplate.class && beanDefinition.getSource() instanceof MethodMetadata && ((MethodMetadata) beanDefinition.getSource()) - .isAnnotated(PolarisCircuitBreaker.class.getName()); + .isAnnotated(LoadBalanced.class.getName()); } - private void registerBean(String interceptorBeanName, PolarisCircuitBreaker polarisCircuitBreaker, - ApplicationContext applicationContext, CircuitBreakerFactory circuitBreakerFactory, RestTemplate restTemplate) { + private void registerBean(String interceptorBeanName, ApplicationContext applicationContext, + CircuitBreakerFactory circuitBreakerFactory, RestTemplate restTemplate) { // register PolarisCircuitBreakerRestTemplateInterceptor bean DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext .getAutowireCapableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder .genericBeanDefinition(PolarisCircuitBreakerRestTemplateInterceptor.class); - beanDefinitionBuilder.addConstructorArgValue(polarisCircuitBreaker); - beanDefinitionBuilder.addConstructorArgValue(applicationContext); beanDefinitionBuilder.addConstructorArgValue(circuitBreakerFactory); beanDefinitionBuilder.addConstructorArgValue(restTemplate); BeanDefinition interceptorBeanDefinition = beanDefinitionBuilder diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java similarity index 71% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java index deefa4065..120725f17 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java @@ -15,10 +15,9 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.resttemplate; +package com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate; import java.io.IOException; -import java.lang.reflect.Method; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.polaris.circuitbreaker.exception.FallbackWrapperException; @@ -26,13 +25,10 @@ import com.tencent.polaris.api.pojo.CircuitBreakerStatus; import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException; import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; -import org.springframework.context.ApplicationContext; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; -import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.RestTemplate; @@ -43,22 +39,14 @@ import org.springframework.web.client.RestTemplate; */ public class PolarisCircuitBreakerRestTemplateInterceptor implements ClientHttpRequestInterceptor { - private final PolarisCircuitBreaker polarisCircuitBreaker; - - private final ApplicationContext applicationContext; - private final CircuitBreakerFactory circuitBreakerFactory; private final RestTemplate restTemplate; public PolarisCircuitBreakerRestTemplateInterceptor( - PolarisCircuitBreaker polarisCircuitBreaker, - ApplicationContext applicationContext, CircuitBreakerFactory circuitBreakerFactory, RestTemplate restTemplate ) { - this.polarisCircuitBreaker = polarisCircuitBreaker; - this.applicationContext = applicationContext; this.circuitBreakerFactory = circuitBreakerFactory; this.restTemplate = restTemplate; } @@ -82,16 +70,6 @@ public class PolarisCircuitBreakerRestTemplateInterceptor implements ClientHttpR } }, t -> { - if (StringUtils.hasText(polarisCircuitBreaker.fallback())) { - CircuitBreakerStatus.FallbackInfo fallbackInfo = new CircuitBreakerStatus.FallbackInfo(200, null, polarisCircuitBreaker.fallback()); - return new PolarisCircuitBreakerHttpResponse(fallbackInfo); - } - if (!PolarisCircuitBreakerFallback.class.toGenericString() - .equals(polarisCircuitBreaker.fallbackClass().toGenericString())) { - Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerFallback.class, "fallback"); - PolarisCircuitBreakerFallback polarisCircuitBreakerFallback = applicationContext.getBean(polarisCircuitBreaker.fallbackClass()); - return (PolarisCircuitBreakerHttpResponse) ReflectionUtils.invokeMethod(method, polarisCircuitBreakerFallback); - } if (t instanceof CallAbortedException) { CircuitBreakerStatus.FallbackInfo fallbackInfo = ((CallAbortedException) t).getFallbackInfo(); if (fallbackInfo != null) { diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/org/springframework/cloud/tsf/circuitbreaker/annotation/EnableTsfCircuitBreaker.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/org/springframework/cloud/tsf/circuitbreaker/annotation/EnableTsfCircuitBreaker.java new file mode 100644 index 000000000..babea559f --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/org/springframework/cloud/tsf/circuitbreaker/annotation/EnableTsfCircuitBreaker.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.cloud.tsf.circuitbreaker.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfCircuitBreaker { +} diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerFeignIntegrationTest.java similarity index 99% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerFeignIntegrationTest.java index 01975f1c7..6a73f91ba 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerFeignIntegrationTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import java.io.BufferedReader; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerNameResolverTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerNameResolverTest.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerNameResolverTest.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerNameResolverTest.java index 42fa1f2f6..86fccc9ea 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerNameResolverTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisCircuitBreakerNameResolverTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import java.lang.reflect.Method; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerTargeterTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerTargeterTest.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerTargeterTest.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerTargeterTest.java index 3b0fdce62..3ef60adb1 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerTargeterTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/feign/PolarisFeignCircuitBreakerTargeterTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.circuitbreaker.instrument.feign; import feign.Feign; import feign.RequestLine; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java similarity index 99% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java index e71dc4645..6fbad2e16 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.gateway; +package com.tencent.cloud.polaris.circuitbreaker.instrument.gateway; import java.io.BufferedReader; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java similarity index 59% rename from spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java rename to spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java index 61e95ae7e..f21fc754a 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/instrument/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.resttemplate; +package com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate; import java.io.BufferedReader; import java.io.InputStream; @@ -23,14 +23,12 @@ import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.stream.Collectors; import com.google.protobuf.util.JsonFormat; import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerFeignClientAutoConfiguration; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.polaris.api.pojo.ServiceKey; -import com.tencent.polaris.client.util.Utils; import com.tencent.polaris.specification.api.v1.fault.tolerance.CircuitBreakerProto; import com.tencent.polaris.test.mock.discovery.NamingServer; import org.junit.jupiter.api.AfterAll; @@ -45,7 +43,6 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; @@ -57,6 +54,7 @@ import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.DefaultUriBuilderFactory; @@ -95,25 +93,6 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest { @Qualifier("restTemplateFallbackFromPolaris") private RestTemplate restTemplateFallbackFromPolaris; - @Autowired - @Qualifier("restTemplateFallbackFromCode") - private RestTemplate restTemplateFallbackFromCode; - - @Autowired - @Qualifier("restTemplateFallbackFromCode2") - private RestTemplate restTemplateFallbackFromCode2; - - @Autowired - @Qualifier("restTemplateFallbackFromCode3") - private RestTemplate restTemplateFallbackFromCode3; - - @Autowired - @Qualifier("restTemplateFallbackFromCode4") - private RestTemplate restTemplateFallbackFromCode4; - - @Autowired - private ApplicationContext applicationContext; - @BeforeAll static void beforeAll() throws Exception { PolarisSDKContextManager.innerDestroy(); @@ -153,26 +132,15 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest { .expect(ExpectedCount.once(), requestTo(new URI("http://localhost:18001/example/service/b/info"))) .andExpect(method(HttpMethod.GET)) .andRespond(withStatus(HttpStatus.BAD_GATEWAY).headers(headers).body("BAD_GATEWAY")); - assertThat(defaultRestTemplate.getForObject("http://localhost:18001/example/service/b/info", String.class)).isEqualTo("fallback"); + assertThatThrownBy(() -> { + defaultRestTemplate.getForObject("http://localhost:18001/example/service/b/info", String.class); + }).isInstanceOf(HttpServerErrorException.class); mockServer.verify(); mockServer.reset(); assertThatThrownBy(() -> { restTemplateFallbackFromPolaris.getForObject("/example/service/b/info", String.class); }).isInstanceOf(IllegalStateException.class); - assertThat(restTemplateFallbackFromCode.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\""); - Utils.sleepUninterrupted(2000); - assertThat(restTemplateFallbackFromCode2.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\""); - Utils.sleepUninterrupted(2000); - assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class) - .getStatusCode()).isEqualTo(HttpStatus.OK); - Utils.sleepUninterrupted(2000); - assertThat(restTemplateFallbackFromCode4.getForObject("/example/service/b/info", String.class)).isEqualTo("fallback"); - Utils.sleepUninterrupted(2000); assertThat(restTemplateFallbackFromPolaris.getForObject("/example/service/b/info", String.class)).isEqualTo("\"fallback from polaris server\""); - // just for code coverage - PolarisCircuitBreakerHttpResponse response = ((CustomPolarisCircuitBreakerFallback) applicationContext.getBean("customPolarisCircuitBreakerFallback")).fallback(); - assertThat(response.getStatusText()).isEqualTo("OK"); - assertThat(response.getFallbackInfo().getCode()).isEqualTo(200); } @Configuration @@ -182,14 +150,12 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest { public static class TestConfig { @Bean - @com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker(fallback = "fallback") public RestTemplate defaultRestTemplate() { return new RestTemplate(); } @Bean @LoadBalanced - @com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker public RestTemplate restTemplateFallbackFromPolaris() { DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://" + TEST_SERVICE_NAME); RestTemplate restTemplate = new RestTemplate(); @@ -197,61 +163,6 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest { return restTemplate; } - @Bean - @LoadBalanced - @com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker(fallbackClass = CustomPolarisCircuitBreakerFallback.class) - public RestTemplate restTemplateFallbackFromCode() { - DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://" + TEST_SERVICE_NAME); - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(uriBuilderFactory); - return restTemplate; - } - - @Bean - @LoadBalanced - @com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker(fallbackClass = CustomPolarisCircuitBreakerFallback2.class) - public RestTemplate restTemplateFallbackFromCode2() { - DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://" + TEST_SERVICE_NAME); - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(uriBuilderFactory); - return restTemplate; - } - - @Bean - @LoadBalanced - @com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker(fallbackClass = CustomPolarisCircuitBreakerFallback3.class) - public RestTemplate restTemplateFallbackFromCode3() { - DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://" + TEST_SERVICE_NAME); - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(uriBuilderFactory); - return restTemplate; - } - - @Bean - @LoadBalanced - @PolarisCircuitBreaker(fallback = "fallback") - public RestTemplate restTemplateFallbackFromCode4() { - DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://" + TEST_SERVICE_NAME); - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(uriBuilderFactory); - return restTemplate; - } - - @Bean - public CustomPolarisCircuitBreakerFallback customPolarisCircuitBreakerFallback() { - return new CustomPolarisCircuitBreakerFallback(); - } - - @Bean - public CustomPolarisCircuitBreakerFallback2 customPolarisCircuitBreakerFallback2() { - return new CustomPolarisCircuitBreakerFallback2(); - } - - @Bean - public CustomPolarisCircuitBreakerFallback3 customPolarisCircuitBreakerFallback3() { - return new CustomPolarisCircuitBreakerFallback3(); - } - @RestController @RequestMapping("/example/service/b") public class ServiceBController { @@ -269,35 +180,4 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest { } } - - public static class CustomPolarisCircuitBreakerFallback implements PolarisCircuitBreakerFallback { - @Override - public PolarisCircuitBreakerHttpResponse fallback() { - return new PolarisCircuitBreakerHttpResponse( - 200, - new HashMap() {{ - put("xxx", "xxx"); - }}, - "\"this is a fallback class\""); - } - } - - public static class CustomPolarisCircuitBreakerFallback2 implements PolarisCircuitBreakerFallback { - @Override - public PolarisCircuitBreakerHttpResponse fallback() { - return new PolarisCircuitBreakerHttpResponse( - 200, - "\"this is a fallback class\"" - ); - } - } - - public static class CustomPolarisCircuitBreakerFallback3 implements PolarisCircuitBreakerFallback { - @Override - public PolarisCircuitBreakerHttpResponse fallback() { - return new PolarisCircuitBreakerHttpResponse( - 200 - ); - } - } } diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java index 5f1fb605b..f6682b2f7 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java @@ -57,7 +57,7 @@ import org.springframework.util.CollectionUtils; */ public class PolarisContractReporter implements ApplicationListener { - private final Logger LOG = LoggerFactory.getLogger(PolarisContractReporter.class); + private static final Logger LOG = LoggerFactory.getLogger(PolarisContractReporter.class); private final org.springdoc.webmvc.api.MultipleOpenApiResource multipleOpenApiWebMvcResource; private final org.springdoc.webflux.api.MultipleOpenApiResource multipleOpenApiWebFluxResource; diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfApiMetadataGrapher.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfApiMetadataGrapher.java index 38b833d31..b92486e77 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfApiMetadataGrapher.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfApiMetadataGrapher.java @@ -38,13 +38,13 @@ import org.springframework.util.StringUtils; public class TsfApiMetadataGrapher implements SmartLifecycle { + private static final Logger logger = LoggerFactory.getLogger(TsfApiMetadataGrapher.class); private final AtomicBoolean isRunning = new AtomicBoolean(false); private final org.springdoc.webmvc.api.MultipleOpenApiResource multipleOpenApiWebMvcResource; private final org.springdoc.webflux.api.MultipleOpenApiResource multipleOpenApiWebFluxResource; private final ObjectMapperProvider springdocObjectMapperProvider; - private Logger logger = LoggerFactory.getLogger(TsfApiMetadataGrapher.class); - private ApplicationContext applicationContext; - private String groupName; + private final ApplicationContext applicationContext; + private final String groupName; public TsfApiMetadataGrapher(org.springdoc.webmvc.api.MultipleOpenApiResource multipleOpenApiWebMvcResource, org.springdoc.webflux.api.MultipleOpenApiResource multipleOpenApiWebFluxResource, diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java index a0da4c1d5..793f0dd13 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java @@ -17,8 +17,8 @@ package com.tencent.cloud.polaris.discovery.refresh; -import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; @@ -30,13 +30,19 @@ import org.springframework.context.annotation.Configuration; * @author Haotian Zhang */ @Configuration(proxyBeanMethods = false) -@ConditionalOnPolarisEnabled +@ConditionalOnPolarisDiscoveryEnabled public class PolarisRefreshConfiguration { @Bean @ConditionalOnMissingBean - public PolarisServiceStatusChangeListener polarisServiceChangeListener() { - return new PolarisServiceStatusChangeListener(); + public PolarisServiceStatusChangeListener polarisServiceChangeListener(ServiceInstanceChangeCallbackManager serviceInstanceChangeCallbackManager) { + return new PolarisServiceStatusChangeListener(serviceInstanceChangeCallbackManager); + } + + @Bean + @ConditionalOnMissingBean + public ServiceInstanceChangeCallbackManager serviceInstanceChangeCallbackManager() { + return new ServiceInstanceChangeCallbackManager(); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java index 285ca5c4c..ce54ded7d 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java @@ -17,14 +17,18 @@ package com.tencent.cloud.polaris.discovery.refresh; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; import com.google.common.collect.Sets; import com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener; +import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.RegistryCacheValue; import com.tencent.polaris.api.pojo.ServiceEventKey; +import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.client.pojo.ServiceInstancesByProto; import com.tencent.polaris.client.pojo.ServicesByProto; import org.slf4j.Logger; @@ -33,7 +37,6 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; -import org.springframework.util.CollectionUtils; /** * Change listener of Polaris service info. When service info is created or deleted, or, instance of service is from 0 to @@ -49,9 +52,13 @@ public class PolarisServiceStatusChangeListener extends AbstractResourceEventLis public static final AtomicLong INDEX = new AtomicLong(0); private static final Logger LOG = LoggerFactory.getLogger(PolarisServiceStatusChangeListener.class); - + private final ServiceInstanceChangeCallbackManager serviceInstanceChangeCallbackManager; private ApplicationEventPublisher publisher; + public PolarisServiceStatusChangeListener(ServiceInstanceChangeCallbackManager serviceInstanceChangeCallbackManager) { + this.serviceInstanceChangeCallbackManager = serviceInstanceChangeCallbackManager; + } + @Override public void onResourceUpdated(ServiceEventKey svcEventKey, RegistryCacheValue oldValue, RegistryCacheValue newValue) { @@ -87,6 +94,22 @@ public class PolarisServiceStatusChangeListener extends AbstractResourceEventLis // Trigger reload of gateway route cache. this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement())); } + + List oldInstances = new ArrayList<>(); + List newInstances = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(oldIns.getInstances())) { + oldInstances.addAll(oldIns.getInstances()); + } + if (CollectionUtils.isNotEmpty(newIns.getInstances())) { + newInstances.addAll(newIns.getInstances()); + } + + try { + this.serviceInstanceChangeCallbackManager.handle(svcEventKey.getService(), oldInstances, newInstances); + } + catch (Throwable throwable) { + LOG.error("Service[{}] instance status change callback failed.", svcEventKey.getService(), throwable); + } } } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeCallback.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeCallback.java new file mode 100644 index 000000000..5be96585b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeCallback.java @@ -0,0 +1,32 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.refresh; + +import java.util.List; + +import com.tencent.polaris.api.pojo.Instance; + +/** + * Callback for service instance change. + * + * @author Haotian Zhang + */ +public interface ServiceInstanceChangeCallback { + + void callback(List currentServiceInstances, List addServiceInstances, List deleteServiceInstances); +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeCallbackManager.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeCallbackManager.java new file mode 100644 index 000000000..0199b2a42 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeCallbackManager.java @@ -0,0 +1,145 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.refresh; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledThreadPoolExecutor; + +import com.tencent.cloud.common.util.ApplicationContextAwareUtils; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient; +import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient; +import com.tencent.polaris.api.pojo.Instance; +import com.tencent.polaris.api.utils.StringUtils; +import com.tencent.polaris.client.util.NamedThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.util.annotation.NonNull; + +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.util.CollectionUtils; + +/** + * Call back manager for service instance change. + * + * @author Haotian Zhang + */ +public class ServiceInstanceChangeCallbackManager implements ApplicationListener, BeanPostProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(ServiceInstanceChangeCallbackManager.class); + + private final ConcurrentHashMap> callbackMap = new ConcurrentHashMap<>(); + + private final ScheduledThreadPoolExecutor serviceChangeListenerExecutor; + + public ServiceInstanceChangeCallbackManager() { + this.serviceChangeListenerExecutor = new ScheduledThreadPoolExecutor(4, new NamedThreadFactory("service-change-listener")); + } + + public void handle(String serviceName, List oldInstances, List newInstances) { + + List addInstances = new ArrayList<>(); + List deleteInstances = new ArrayList<>(); + + // calculate add instances. + for (Instance instance : newInstances) { + if (!oldInstances.contains(instance)) { + addInstances.add(instance); + } + } + // calculate delete instances. + for (Instance instance : oldInstances) { + if (!newInstances.contains(instance)) { + deleteInstances.add(instance); + } + } + + if ((!CollectionUtils.isEmpty(addInstances) || !CollectionUtils.isEmpty(deleteInstances)) + && callbackMap.containsKey(serviceName)) { + List callbacks = callbackMap.get(serviceName); + + for (ServiceInstanceChangeCallback callback : callbacks) { + serviceChangeListenerExecutor.execute(() -> { + try { + callback.callback(newInstances, addInstances, deleteInstances); + } + catch (Exception e) { + LOG.error("exception in callback, service name:{}, ", serviceName, e); + } + }); + } + } + } + + @Override + public synchronized Object postProcessAfterInitialization(Object bean, String beanName) { + Class clz = bean.getClass(); + + if (!ServiceInstanceChangeCallback.class.isAssignableFrom(clz)) { + return bean; + } + + String serviceName = null; + if (clz.isAnnotationPresent(ServiceInstanceChangeListener.class)) { + ServiceInstanceChangeListener serviceInstanceChangeListener = clz.getAnnotation(ServiceInstanceChangeListener.class); + serviceName = serviceInstanceChangeListener.serviceName(); + } + + if (StringUtils.isBlank(serviceName)) { + return bean; + } + + // process callback + if (callbackMap.containsKey(serviceName)) { + List callbacks = callbackMap.get(serviceName); + callbacks.add((ServiceInstanceChangeCallback) bean); + } + else { + List callbacks = new ArrayList<>(); + callbacks.add((ServiceInstanceChangeCallback) bean); + callbackMap.put(serviceName, callbacks); + } + + return bean; + } + + @Override + public void onApplicationEvent(@NonNull ApplicationReadyEvent event) { + PolarisDiscoveryClient polarisDiscoveryClient = ApplicationContextAwareUtils.getBeanIfExists(PolarisDiscoveryClient.class); + PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient = ApplicationContextAwareUtils.getBeanIfExists(PolarisReactiveDiscoveryClient.class); + for (String serviceName : callbackMap.keySet()) { + try { + if (polarisDiscoveryClient != null) { + polarisDiscoveryClient.getInstances(serviceName); + } + else if (polarisReactiveDiscoveryClient != null) { + polarisReactiveDiscoveryClient.getInstances(serviceName).subscribe(); + } + else { + LOG.warn("[{}] no discovery client found.", serviceName); + } + } + catch (Throwable throwable) { + LOG.error("Get instances of service [{}] failed.", serviceName, throwable); + } + } + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeListener.java new file mode 100644 index 000000000..539a2e38f --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/ServiceInstanceChangeListener.java @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.refresh; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +/** + * @author Haotian Zhang + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Component +public @interface ServiceInstanceChangeListener { + + /** + * listen service name. + */ + String serviceName(); +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java new file mode 100644 index 000000000..179833b67 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/config/PolarisEagerLoadAutoConfiguration.java @@ -0,0 +1,44 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.eager.config; + +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient; +import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient; +import com.tencent.cloud.polaris.eager.instrument.feign.FeignEagerLoadSmartLifecycle; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(name = "spring.cloud.polaris.discovery.eager-load.enabled", havingValue = "true") +public class PolarisEagerLoadAutoConfiguration { + + @Bean + @ConditionalOnClass(name = "feign.Feign") + @ConditionalOnProperty(name = "spring.cloud.polaris.discovery.eager-load.feign.enabled", havingValue = "true", matchIfMissing = true) + public FeignEagerLoadSmartLifecycle feignEagerLoadSmartLifecycle( + ApplicationContext applicationContext, @Autowired(required = false) PolarisDiscoveryClient polarisDiscoveryClient, + @Autowired(required = false) PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient) { + return new FeignEagerLoadSmartLifecycle(applicationContext, polarisDiscoveryClient, polarisReactiveDiscoveryClient); + } +} + diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java new file mode 100644 index 000000000..ca4e0e0a9 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/eager/instrument/feign/FeignEagerLoadSmartLifecycle.java @@ -0,0 +1,94 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.eager.instrument.feign; + +import com.tencent.cloud.common.util.FeignUtil; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient; +import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient; +import com.tencent.polaris.api.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.SmartLifecycle; + +public class FeignEagerLoadSmartLifecycle implements SmartLifecycle { + + private static final Logger LOG = LoggerFactory.getLogger(FeignEagerLoadSmartLifecycle.class); + + private final ApplicationContext applicationContext; + + private final PolarisDiscoveryClient polarisDiscoveryClient; + + private final PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient; + + public FeignEagerLoadSmartLifecycle(ApplicationContext applicationContext, PolarisDiscoveryClient polarisDiscoveryClient, + PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient) { + this.applicationContext = applicationContext; + this.polarisDiscoveryClient = polarisDiscoveryClient; + this.polarisReactiveDiscoveryClient = polarisReactiveDiscoveryClient; + } + + @Override + public void start() { + LOG.info("feign eager-load start"); + for (String name : applicationContext.getBeanDefinitionNames()) { + try { + if (name.contains(FeignUtil.FEIGN_CLIENT_SPECIF) && !name.startsWith(FeignUtil.FEIGN_CLIENT_DEFAULT)) { + String feignName = FeignUtil.analysisFeignName(name, applicationContext); + if (StringUtils.isNotBlank(feignName)) { + LOG.info("[{}] eager-load start", feignName); + if (polarisDiscoveryClient != null) { + polarisDiscoveryClient.getInstances(feignName); + } + else if (polarisReactiveDiscoveryClient != null) { + polarisReactiveDiscoveryClient.getInstances(feignName).subscribe(); + } + else { + LOG.warn("[{}] no discovery client found.", feignName); + } + LOG.info("[{}] eager-load end", feignName); + } + else { + LOG.warn("feign name is blank."); + } + } + } + catch (Exception e) { + LOG.error("[{}] eager-load failed.", name, e); + } + } + LOG.info("feign eager-load end"); + + } + + @Override + public void stop() { + + } + + @Override + public boolean isRunning() { + return false; + } + + @Override + public int getPhase() { + return 10; + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerAutoConfiguration.java index 416a7989b..b0cc4ebf6 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerAutoConfiguration.java @@ -21,7 +21,7 @@ import java.util.ArrayList; import java.util.List; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; -import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; +import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.EnhancedRestTemplateInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfApiPolarisRegistrationCustomizer.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfApiPolarisRegistrationCustomizer.java similarity index 97% rename from spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfApiPolarisRegistrationCustomizer.java rename to spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfApiPolarisRegistrationCustomizer.java index 61bbde1d9..da235cb60 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfApiPolarisRegistrationCustomizer.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfApiPolarisRegistrationCustomizer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.tsf.registry; +package com.tencent.cloud.polaris.registry.tsf; import java.util.HashMap; import java.util.Map; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfDiscoveryRegistryAutoConfiguration.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java rename to spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfDiscoveryRegistryAutoConfiguration.java index 5538d1f5f..fb511a13f 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfDiscoveryRegistryAutoConfiguration.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.tsf.registry; +package com.tencent.cloud.polaris.registry.tsf; import com.tencent.cloud.common.tsf.ConditionalOnTsfConsulEnabled; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfPortPolarisRegistrationCustomizer.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfPortPolarisRegistrationCustomizer.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfPortPolarisRegistrationCustomizer.java rename to spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfPortPolarisRegistrationCustomizer.java index 5e4904180..51103e928 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfPortPolarisRegistrationCustomizer.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfPortPolarisRegistrationCustomizer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.tsf.registry; +package com.tencent.cloud.polaris.registry.tsf; import com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreProperties; import com.tencent.cloud.polaris.extend.consul.ConsulDiscoveryProperties; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfServletRegistrationCustomizer.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfServletRegistrationCustomizer.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfServletRegistrationCustomizer.java rename to spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfServletRegistrationCustomizer.java index 1632dc022..30252fd0c 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfServletRegistrationCustomizer.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/tsf/TsfServletRegistrationCustomizer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.tsf.registry; +package com.tencent.cloud.polaris.registry.tsf; import java.util.ArrayList; import java.util.Arrays; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/ConsulServiceChangeCallback.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/ConsulServiceChangeCallback.java new file mode 100644 index 000000000..8d58999c8 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/ConsulServiceChangeCallback.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.serviceregistry.watch; + +import java.util.List; + +import com.ecwid.consul.v1.health.model.HealthService; + +/** + * Not working anymore. Compatible with old versions TSF SDK. + *

+ * Deprecated since 2.0.0.0. + * + * @author Haotian Zhang + */ +@Deprecated +public interface ConsulServiceChangeCallback { + + /** + * 回调函数. + */ + void callback(List currentServices, List addServices, List deleteServices); +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/ConsulServiceChangeListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/ConsulServiceChangeListener.java new file mode 100644 index 000000000..ef7805088 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/ConsulServiceChangeListener.java @@ -0,0 +1,51 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.serviceregistry.watch; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + *

+ * Deprecated since 2.0.0.0. + * + * @author Haotian Zhang + */ +@Deprecated +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Component +public @interface ConsulServiceChangeListener { + + /** + * 监听的服务名. + */ + String serviceName(); + + /** + * 监听的命名空间,true 时为全局命名空间,false 为本命名空间. + */ + boolean global() default false; +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CustomFallback.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/LocalServiceChangeListener.java similarity index 55% rename from spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CustomFallback.java rename to spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/LocalServiceChangeListener.java index 6fa073388..782b79c52 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CustomFallback.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/tsf/serviceregistry/watch/LocalServiceChangeListener.java @@ -15,29 +15,32 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.quickstart.caller.circuitbreaker; +package com.tencent.tsf.serviceregistry.watch; -import java.util.HashMap; - -import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreakerFallback; -import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreakerHttpResponse; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import org.springframework.stereotype.Component; /** - * CustomFallback. + * Empty annotation. Compatible with old versions TSF SDK. + *

+ * Deprecated since 2.0.0.0. * - * @author sean yu + * @author Haotian Zhang */ +@Deprecated +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented @Component -public class CustomFallback implements PolarisCircuitBreakerFallback { - @Override - public PolarisCircuitBreakerHttpResponse fallback() { - return new PolarisCircuitBreakerHttpResponse( - 200, - new HashMap() {{ - put("Content-Type", "application/json"); - }}, - "{\"msg\": \"this is a fallback class\"}"); - } +public @interface LocalServiceChangeListener { + + /** + * 回调的 currentServices 是否包含当前实例. + */ + boolean excludeLocalInstance() default false; } 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 a8ebda064..aa0a4ec1a 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 @@ -78,6 +78,18 @@ "defaultValue": false, "description": "Zero protection test connectivity switch. Default: false." }, + { + "name": "spring.cloud.polaris.discovery.eager-load.enabled", + "type": "java.lang.Boolean", + "defaultValue": false, + "description": "Eager load switch. Default: false." + }, + { + "name": "spring.cloud.polaris.discovery.eager-load.feign.enabled", + "type": "java.lang.Boolean", + "defaultValue": true, + "description": "Feign eager load switch. Default: true." + }, { "name": "spring.cloud.nacos.discovery.enabled", "type": "java.lang.Boolean", diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 72a133c17..04de47e6b 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -3,4 +3,5 @@ com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration com.tencent.cloud.polaris.endpoint.PolarisDiscoveryEndpointAutoConfiguration com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancerAutoConfiguration -com.tencent.cloud.polaris.tsf.registry.TsfDiscoveryRegistryAutoConfiguration +com.tencent.cloud.polaris.registry.tsf.TsfDiscoveryRegistryAutoConfiguration +com.tencent.cloud.polaris.eager.config.PolarisEagerLoadAutoConfiguration diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListenerTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListenerTest.java index 4d8492260..983ba4f13 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListenerTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListenerTest.java @@ -61,7 +61,7 @@ public class PolarisServiceStatusChangeListenerTest { @Test public void testOnResourceUpdated() { - PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener(); + PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener(mock(ServiceInstanceChangeCallbackManager.class)); polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher); // Service update event diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/org/springframework/tsf/ratelimit/annotation/EnableTsfRateLimit.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/org/springframework/tsf/ratelimit/annotation/EnableTsfRateLimit.java new file mode 100644 index 000000000..1ba0c3cd4 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/org/springframework/tsf/ratelimit/annotation/EnableTsfRateLimit.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.tsf.ratelimit.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfRateLimit { +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/FeignAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/FeignAutoConfiguration.java index 29f7cee24..4fc08827a 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/FeignAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/FeignAutoConfiguration.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.router.config; -import com.tencent.cloud.polaris.router.feign.RouterLabelFeignInterceptor; +import com.tencent.cloud.polaris.router.instrument.feign.RouterLabelFeignInterceptor; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java index 8d6471af7..48857ad46 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java @@ -25,13 +25,13 @@ import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterP import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; +import com.tencent.cloud.polaris.router.instrument.resttemplate.RouterLabelRestTemplateInterceptor; +import com.tencent.cloud.polaris.router.instrument.scg.RouterLabelGlobalFilter; import com.tencent.cloud.polaris.router.interceptor.MetadataRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.NamespaceRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor; -import com.tencent.cloud.polaris.router.resttemplate.RouterLabelRestTemplateInterceptor; -import com.tencent.cloud.polaris.router.scg.RouterLabelGlobalFilter; -import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; +import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.EnhancedRestTemplateInterceptor; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/feign/RouterLabelFeignInterceptor.java similarity index 96% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java rename to spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/feign/RouterLabelFeignInterceptor.java index be2ac604b..35980d0d1 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/feign/RouterLabelFeignInterceptor.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.feign; +package com.tencent.cloud.polaris.router.instrument.feign; import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.common.metadata.MetadataContextHolder; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/resttemplate/RouterLabelRestTemplateInterceptor.java similarity index 96% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java rename to spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/resttemplate/RouterLabelRestTemplateInterceptor.java index c5b51873d..41f569872 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/resttemplate/RouterLabelRestTemplateInterceptor.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.resttemplate; +package com.tencent.cloud.polaris.router.instrument.resttemplate; import java.io.IOException; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/RouterLabelGlobalFilter.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/scg/RouterLabelGlobalFilter.java similarity index 97% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/RouterLabelGlobalFilter.java rename to spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/scg/RouterLabelGlobalFilter.java index 994a23adc..900b57978 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/RouterLabelGlobalFilter.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/instrument/scg/RouterLabelGlobalFilter.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.scg; +package com.tencent.cloud.polaris.router.instrument.scg; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.metadata.provider.ReactiveMetadataProvider; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/tsf/lane/annotation/EnableTsfLane.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/tsf/lane/annotation/EnableTsfLane.java new file mode 100644 index 000000000..e7b408541 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/tsf/lane/annotation/EnableTsfLane.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.lane.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfLane { + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/femas/adaptor/tsf/governance/zonefilter/EnableTsfZoneFilter.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/femas/adaptor/tsf/governance/zonefilter/EnableTsfZoneFilter.java new file mode 100644 index 000000000..da44b187e --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/femas/adaptor/tsf/governance/zonefilter/EnableTsfZoneFilter.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.femas.adaptor.tsf.governance.zonefilter; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfZoneFilter { +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/EnableTsfUnit.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/EnableTsfUnit.java new file mode 100644 index 000000000..335be9616 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/EnableTsfUnit.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.unit.annotation; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfUnit { +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitCall.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitCall.java new file mode 100644 index 000000000..35dda9d9f --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitCall.java @@ -0,0 +1,44 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.unit.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +public @interface TsfUnitCall { + + // 单元化下的业务系统名 + String systemName() default ""; + + // 单元化下的是否调用GDU服务 + boolean global() default false; +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitCustomerIdentifier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitCustomerIdentifier.java new file mode 100644 index 000000000..abc2672de --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitCustomerIdentifier.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.unit.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +public @interface TsfUnitCustomerIdentifier { +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitLocalCall.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitLocalCall.java new file mode 100644 index 000000000..31e164bb7 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/tsf/unit/annotation/TsfUnitLocalCall.java @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.unit.annotation; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +public @interface TsfUnitLocalCall { + String className() default ""; + + String methodName() default ""; +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/org/springframework/cloud/tsf/route/annotation/EnableTsfRoute.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/org/springframework/cloud/tsf/route/annotation/EnableTsfRoute.java new file mode 100644 index 000000000..26a5c5384 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/org/springframework/cloud/tsf/route/annotation/EnableTsfRoute.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.cloud.tsf.route.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfRoute { +} + diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java index 1114d2eb9..914b55f9c 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/FeignAutoConfigurationTest.java @@ -20,7 +20,7 @@ package com.tencent.cloud.polaris.router.config; import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; -import com.tencent.cloud.polaris.router.feign.RouterLabelFeignInterceptor; +import com.tencent.cloud.polaris.router.instrument.feign.RouterLabelFeignInterceptor; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/feign/RouterLabelFeignInterceptorTest.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java rename to spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/feign/RouterLabelFeignInterceptorTest.java index a918ec150..1f91a2978 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/feign/RouterLabelFeignInterceptorTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.feign; +package com.tencent.cloud.polaris.router.instrument.feign; import java.util.Collections; import java.util.HashMap; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/resttemplate/RouterLabelRestTemplateInterceptorTest.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java rename to spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/resttemplate/RouterLabelRestTemplateInterceptorTest.java index c2ddb2305..c06e74a83 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/resttemplate/RouterLabelRestTemplateInterceptorTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.resttemplate; +package com.tencent.cloud.polaris.router.instrument.resttemplate; import java.net.URI; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/RouterLabelGlobalFilterTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/scg/RouterLabelGlobalFilterTest.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/RouterLabelGlobalFilterTest.java rename to spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/scg/RouterLabelGlobalFilterTest.java index dec5a4cbf..52c03f416 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/RouterLabelGlobalFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/instrument/scg/RouterLabelGlobalFilterTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.scg; +package com.tencent.cloud.polaris.router.instrument.scg; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.polaris.metadata.core.MessageMetadataContainer; diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/async/PolarisAsyncConfiguration.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/async/PolarisAsyncConfiguration.java new file mode 100644 index 000000000..ace5628bb --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/async/PolarisAsyncConfiguration.java @@ -0,0 +1,83 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.common.async; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import com.tencent.cloud.plugin.threadlocal.TaskExecutorWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Role; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import static com.tencent.cloud.common.metadata.CrossThreadMetadataContext.CROSS_THREAD_METADATA_CONTEXT_CONSUMER; +import static com.tencent.cloud.common.metadata.CrossThreadMetadataContext.CROSS_THREAD_METADATA_CONTEXT_SUPPLIER; + +/** + * polaris async executor for @Async . + * + * @author Haotian Zhang + */ +@Configuration +@Role(BeanDefinition.ROLE_INFRASTRUCTURE) +@ConditionalOnProperty(name = "spring.cloud.tencent.async.enabled") +public class PolarisAsyncConfiguration implements AsyncConfigurer { + + private static final Logger logger = LoggerFactory.getLogger(PolarisAsyncConfiguration.class); + + @Primary + @Bean("polarisAsyncExecutor") + public TaskExecutor polarisAsyncExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + int corePoolSize = 10; + executor.setCorePoolSize(corePoolSize); + int maxPoolSize = 50; + executor.setMaxPoolSize(maxPoolSize); + int queueCapacity = 10; + executor.setQueueCapacity(queueCapacity); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + String threadNamePrefix = "polaris-async-executor-"; + executor.setThreadNamePrefix(threadNamePrefix); + executor.setWaitForTasksToCompleteOnShutdown(true); + executor.setAwaitTerminationSeconds(5); + executor.initialize(); + TaskExecutor executorWrapper = new TaskExecutorWrapper<>(executor, CROSS_THREAD_METADATA_CONTEXT_SUPPLIER, CROSS_THREAD_METADATA_CONTEXT_CONSUMER); + logger.info("Created async executor with corePoolSize:{}, maxPoolSize:{}, queueCapacity:{}", corePoolSize, maxPoolSize, queueCapacity); + return executorWrapper; + } + + @Override + public Executor getAsyncExecutor() { + return polarisAsyncExecutor(); + } + + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return (ex, method, params) -> logger.error("Execute asynchronous tasks '{}' failed.", method, ex); + } +} diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/CrossThreadMetadataContext.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/CrossThreadMetadataContext.java new file mode 100644 index 000000000..f8ae7c239 --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/CrossThreadMetadataContext.java @@ -0,0 +1,68 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.common.metadata; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +import com.tencent.cloud.common.util.JacksonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Metadata Context for cross thread. + * + * @author Haotian Zhang + */ +public class CrossThreadMetadataContext { + + private static final Logger LOG = LoggerFactory.getLogger(CrossThreadMetadataContext.class); + + /** + * Get metadata context from previous thread. + */ + public static final Supplier CROSS_THREAD_METADATA_CONTEXT_SUPPLIER = () -> { + CrossThreadMetadataContext crossThreadMetadataContext = new CrossThreadMetadataContext(); + crossThreadMetadataContext.setMetadataContext(MetadataContextHolder.get()); + + if (LOG.isDebugEnabled()) { + LOG.debug("Context map is got: {}", JacksonUtils.serialize2Json(crossThreadMetadataContext)); + } + return crossThreadMetadataContext; + }; + + /** + * Set metadata context to current thread. + */ + public static final Consumer CROSS_THREAD_METADATA_CONTEXT_CONSUMER = crossThreadMetadataContext -> { + MetadataContextHolder.set(crossThreadMetadataContext.getMetadataContext()); + if (LOG.isDebugEnabled()) { + LOG.debug("Context map is set: {}", JacksonUtils.serialize2Json(crossThreadMetadataContext)); + } + }; + + private MetadataContext metadataContext; + + public MetadataContext getMetadataContext() { + return metadataContext; + } + + public void setMetadataContext(MetadataContext metadataContext) { + this.metadataContext = metadataContext; + } +} 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 fb41763bb..e01e41afb 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 @@ -142,8 +142,8 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M return values; } - public void putMetadataAsMap(MetadataType metadataType, TransitiveType transitiveType, boolean downstream, Map values) { - MetadataContainer metadataContainer = getMetadataContainer(metadataType, downstream); + public void putMetadataAsMap(MetadataType metadataType, TransitiveType transitiveType, boolean caller, Map values) { + MetadataContainer metadataContainer = getMetadataContainer(metadataType, caller); for (Map.Entry entry : values.entrySet()) { metadataContainer.putMetadataStringValue(entry.getKey(), entry.getValue(), transitiveType); } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java index 66cdedd1f..474a9512c 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java @@ -84,4 +84,18 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware { } return property; } + + public static T getBean(Class requiredType) { + return applicationContext.getBean(requiredType); + } + + public static T getBeanIfExists(Class requiredType) { + try { + return applicationContext.getBean(requiredType); + } + catch (Throwable e) { + LOGGER.warn("get bean failed, bean type: {}", requiredType.getName()); + return null; + } + } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/FeignUtil.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/FeignUtil.java new file mode 100644 index 000000000..4306e087e --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/FeignUtil.java @@ -0,0 +1,83 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.common.util; + +import org.springframework.context.ApplicationContext; +import org.springframework.util.StringUtils; + +/** + * @author heihuliliu + */ +public final class FeignUtil { + + /** + * Feign client spec. + */ + public static final String FEIGN_CLIENT_SPECIF = ".FeignClientSpecification"; + + /** + * Default Feign client spec. + */ + public static final String FEIGN_CLIENT_DEFAULT = "default."; + + /** + * regular expression that parses ${xxx} . + */ + public static final String REGEX = "^[$][{](.*)[}]$"; + + /** + * replacement of ${xxx}. + */ + public static final String REPLACEMENT = "$1"; + + private FeignUtil() { + + } + + /** + * TODO If @FeignClient specifies contextId, the service name will not be obtained correctly, but the contextId will be obtained. + * + * @param name feign name. + * @param context application context. + * @return service name. + */ + public static String analysisFeignName(String name, ApplicationContext context) { + String feignName = ""; + String feignPath = name.substring(0, name.indexOf(FEIGN_CLIENT_SPECIF)); + // Handle the case where the service name is a variable + if (feignPath.matches(REGEX)) { + feignPath = context.getEnvironment().getProperty(feignPath.replaceAll(REGEX, REPLACEMENT)); + } + if (StringUtils.hasText(feignPath)) { + // The case of multi-level paths + String[] feignNames = feignPath.split("/"); + if (feignNames.length > 1) { + for (int i = 0; i < feignNames.length; i++) { + if (StringUtils.hasText(feignNames[i])) { + feignName = feignNames[i]; + break; + } + } + } + else { + feignName = feignNames[0]; + } + } + return feignName; + } +} diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisCompletableFutureUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisCompletableFutureUtils.java new file mode 100644 index 000000000..ce30ceedf --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisCompletableFutureUtils.java @@ -0,0 +1,45 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.common.util; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; + +import com.tencent.polaris.threadlocal.cross.CompletableFutureUtils; + +import static com.tencent.cloud.common.metadata.CrossThreadMetadataContext.CROSS_THREAD_METADATA_CONTEXT_CONSUMER; +import static com.tencent.cloud.common.metadata.CrossThreadMetadataContext.CROSS_THREAD_METADATA_CONTEXT_SUPPLIER; + +/** + * Polaris CompletableFuture Utils. + * + * @author Haotian Zhang + */ +public final class PolarisCompletableFutureUtils { + + private PolarisCompletableFutureUtils() { + } + + public static CompletableFuture supplyAsync(Supplier supplier) { + return CompletableFutureUtils.supplyAsync(supplier, CROSS_THREAD_METADATA_CONTEXT_SUPPLIER, CROSS_THREAD_METADATA_CONTEXT_CONSUMER); + } + + public static CompletableFuture runAsync(Runnable runnable) { + return CompletableFutureUtils.runAsync(runnable, CROSS_THREAD_METADATA_CONTEXT_SUPPLIER, CROSS_THREAD_METADATA_CONTEXT_CONSUMER); + } +} diff --git a/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/context/TsfContext.java b/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/TsfContext.java similarity index 80% rename from spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/context/TsfContext.java rename to spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/TsfContext.java index 2e6093458..577b82549 100644 --- a/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/context/TsfContext.java +++ b/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/TsfContext.java @@ -15,10 +15,11 @@ * specific language governing permissions and limitations under the License. */ -package org.springframework.tsf.core.context; +package org.springframework.tsf.core; import java.nio.charset.StandardCharsets; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import com.tencent.cloud.common.metadata.MetadataContext; @@ -32,6 +33,7 @@ public final class TsfContext { static final int MAX_KEY_LENGTH = 32; static final int MAX_VALUE_LENGTH = 128; + private static final String CUSTOM_METADATA = "tsf-custom-metadata"; private TsfContext() { @@ -74,4 +76,19 @@ public final class TsfContext { MAX_VALUE_LENGTH)); } } + + public static void putCustomMetadata(Object customMetadata) { + if (customMetadata == null) { + return; + } + MetadataContext tsfCoreContext = MetadataContextHolder.get(); + Map tagMap = new HashMap<>(); + try { + tagMap.put(CUSTOM_METADATA, customMetadata.toString()); + } + catch (Throwable throwable) { + throw new RuntimeException("Failed to parse custom metadata", throwable); + } + tsfCoreContext.putMetadataAsMap(MetadataType.CUSTOM, TransitiveType.NONE, false, tagMap); + } } diff --git a/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/util/TsfSpringContextAware.java b/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/util/TsfSpringContextAware.java new file mode 100644 index 000000000..557a131d8 --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/org/springframework/tsf/core/util/TsfSpringContextAware.java @@ -0,0 +1,69 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.tsf.core.util; + +import com.tencent.cloud.common.util.ApplicationContextAwareUtils; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; + +/** + * Spring context utils. + *

+ * Deprecated since 2.0.0.0. + * + * @author hongweizhu + */ +@Deprecated +public class TsfSpringContextAware { + + /** + * Get application context. + * @return application context + */ + public static ApplicationContext getApplicationContext() { + return ApplicationContextAwareUtils.getApplicationContext(); + } + + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + // do nothing. + } + + /** + * Get application property. + * @param key property name + * @return property value + */ + public static String getProperties(String key) { + return ApplicationContextAwareUtils.getProperties(key); + } + + /** + * Get application property. If null, return default. + * @param key property name + * @param defaultValue default value + * @return property value + */ + public static String getProperties(String key, String defaultValue) { + return ApplicationContextAwareUtils.getProperties(key, defaultValue); + } + + public static T getBean(Class requiredType) { + return ApplicationContextAwareUtils.getBean(requiredType); + } +} diff --git a/spring-cloud-tencent-commons/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-commons/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 291e26dbe..9df3e2d80 100644 --- a/spring-cloud-tencent-commons/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-commons/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -19,6 +19,12 @@ "name": "spring.cloud.tencent.metadata.headers", "type": "java.util.List", "description": "Custom transitive http header key list." + }, + { + "name": "spring.cloud.tencent.async.enabled", + "type": "java.lang.Boolean", + "defaultValue": false, + "description": "Async support switch. Default: false." } ] } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 81bc748e9..f8af3f4fd 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -211,6 +211,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-starter-tencent-fault-tolerance + ${revision} + + com.google.guava diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java index f4f805a32..56ed7aca1 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -22,6 +22,7 @@ import java.net.URLDecoder; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.quickstart.callee.config.DataSourceProperties; +import com.tencent.cloud.quickstart.callee.service.FaultToleranceService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,6 +60,9 @@ public class QuickstartCalleeController { @Autowired private DataSourceProperties dataSourceProperties; + @Autowired + private FaultToleranceService faultToleranceService; + /** * Get sum of two value. * @param value1 value 1 @@ -126,7 +130,22 @@ public class QuickstartCalleeController { @GetMapping("/test/{num}/echo") public String test(@PathVariable int num) { - LOG.info("Quickstart Callee Service [%s] is detected right.", num); + LOG.info("Quickstart Callee Service [{}] is detected right.", num); return String.format("Quickstart Callee Service [%s] is detected right.", num); } + + @GetMapping("/faultTolerance/failFast") + public String faultToleranceFailFast() { + return faultToleranceService.failFast(); + } + + @GetMapping("/faultTolerance/failOver") + public String faultToleranceFailOver() { + return faultToleranceService.failOver(); + } + + @GetMapping("/faultTolerance/forking") + public String faultToleranceForking() { + return faultToleranceService.forking(); + } } diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/CalleeServiceChangeCallback.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/CalleeServiceChangeCallback.java new file mode 100644 index 000000000..c4ac75b17 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/CalleeServiceChangeCallback.java @@ -0,0 +1,60 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.quickstart.callee.service; + +import java.util.List; + +import com.tencent.cloud.polaris.discovery.refresh.ServiceInstanceChangeCallback; +import com.tencent.cloud.polaris.discovery.refresh.ServiceInstanceChangeListener; +import com.tencent.polaris.api.pojo.Instance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.stereotype.Component; + +/** + * Call back for QuickstartCalleeService. + * + * @author Haotian Zhang + */ +@Component +@ServiceInstanceChangeListener(serviceName = "QuickstartCalleeService") +public class CalleeServiceChangeCallback implements ServiceInstanceChangeCallback { + + private static final Logger LOG = LoggerFactory.getLogger(CalleeServiceChangeCallback.class); + + @Override + public void callback(List currentServiceInstances, List addServiceInstances, List deleteServiceInstances) { + String current = generateNodeList(currentServiceInstances); + String add = generateNodeList(addServiceInstances); + String delete = generateNodeList(deleteServiceInstances); + LOG.info("current: {}, add: {}, delete: {}", current, add, delete); + } + + private String generateNodeList(List deleteServiceInstances) { + StringBuilder nodeListStr = new StringBuilder("["); + for (Instance instance : deleteServiceInstances) { + if (nodeListStr.length() > 1) { + nodeListStr.append(", "); + } + nodeListStr.append(instance.getHost()).append(":").append(instance.getPort()); + } + nodeListStr.append("]"); + return nodeListStr.toString(); + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java new file mode 100644 index 000000000..cc913de7f --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java @@ -0,0 +1,62 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.quickstart.callee.service; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.tencent.cloud.plugin.faulttolerance.annotation.FaultTolerance; +import com.tencent.cloud.plugin.faulttolerance.model.FaultToleranceStrategy; + +import org.springframework.stereotype.Service; + +/** + * Service for fault tolerance. + * + * @author Haotian Zhang + */ +@Service +public class FaultToleranceService { + + private final AtomicInteger failOverCount = new AtomicInteger(0); + private final AtomicInteger forkingCount = new AtomicInteger(0); + + @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_FAST, fallbackMethod = "fallback") + public String failFast() { + throw new RuntimeException("NO"); + } + + public String fallback() { + return "fallback"; + } + + @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_OVER, maxAttempts = 3) + public String failOver() { + if (failOverCount.incrementAndGet() % 4 == 0) { + return "OK"; + } + throw new RuntimeException("NO"); + } + + @FaultTolerance(strategy = FaultToleranceStrategy.FORKING, parallelism = 4) + public String forking() { + if (forkingCount.incrementAndGet() % 4 == 0) { + return "OK"; + } + throw new RuntimeException("NO"); + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java index 0f6d71ad5..ae4bac993 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java @@ -17,9 +17,6 @@ package com.tencent.cloud.quickstart.caller; -import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker; -import com.tencent.cloud.quickstart.caller.circuitbreaker.CustomFallback; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; @@ -50,27 +47,7 @@ public class QuickstartCallerApplication { @Bean @LoadBalanced - public RestTemplate defaultRestTemplate() { - DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://QuickstartCalleeService"); - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(uriBuilderFactory); - return restTemplate; - } - - @Bean - @LoadBalanced - @PolarisCircuitBreaker - public RestTemplate restTemplateFallbackFromPolaris() { - DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://QuickstartCalleeService"); - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(uriBuilderFactory); - return restTemplate; - } - - @Bean - @LoadBalanced - @PolarisCircuitBreaker(fallbackClass = CustomFallback.class) - public RestTemplate restTemplateFallbackFromCode() { + public RestTemplate calleeRestTemplate() { DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://QuickstartCalleeService"); RestTemplate restTemplate = new RestTemplate(); restTemplate.setUriTemplateHandler(uriBuilderFactory); diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CircuitBreakerController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CircuitBreakerController.java index 907980025..bbf8ea619 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CircuitBreakerController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/circuitbreaker/CircuitBreakerController.java @@ -50,16 +50,8 @@ public class CircuitBreakerController { private CircuitBreakerQuickstartCalleeServiceWithFallback circuitBreakerQuickstartCalleeServiceWithFallback; @Autowired - @Qualifier("defaultRestTemplate") - private RestTemplate defaultRestTemplate; - - @Autowired - @Qualifier("restTemplateFallbackFromPolaris") - private RestTemplate restTemplateFallbackFromPolaris; - - @Autowired - @Qualifier("restTemplateFallbackFromCode") - private RestTemplate restTemplateFallbackFromCode; + @Qualifier("calleeRestTemplate") + private RestTemplate calleeRestTemplate; @Autowired private CircuitBreakerFactory circuitBreakerFactory; @@ -114,7 +106,7 @@ public class CircuitBreakerController { public String circuitBreakRestTemplate() { return circuitBreakerFactory .create(MetadataContext.LOCAL_NAMESPACE + "#QuickstartCalleeService#/quickstart/callee/circuitBreak#http#GET") - .run(() -> defaultRestTemplate.getForObject("/quickstart/callee/circuitBreak", String.class), + .run(() -> calleeRestTemplate.getForObject("/quickstart/callee/circuitBreak", String.class), throwable -> "trigger the refuse for service callee." ); } @@ -126,17 +118,7 @@ public class CircuitBreakerController { @GetMapping("/rest/fallbackFromPolaris/wildcard/{uid}") public ResponseEntity circuitBreakRestTemplateFallbackFromPolarisWildcard(@PathVariable String uid) { String path = String.format("/quickstart/callee/circuitBreak/wildcard/%s", uid); - return restTemplateFallbackFromPolaris.getForEntity(path, String.class); - } - - /** - * RestTemplate wildcard circuit breaker with fallback from code. - * @return circuit breaker information of callee - */ - @GetMapping("/rest/fallbackFromCode/wildcard/{uid}") - public ResponseEntity circuitBreakRestTemplateFallbackFromCodeWildcard(@PathVariable String uid) { - String path = String.format("/quickstart/callee/circuitBreak/wildcard/%s", uid); - return restTemplateFallbackFromCode.getForEntity(path, String.class); + return calleeRestTemplate.getForEntity(path, String.class); } /** @@ -146,21 +128,7 @@ public class CircuitBreakerController { @GetMapping("/rest/fallbackFromPolaris") public ResponseEntity circuitBreakRestTemplateFallbackFromPolaris() { try { - return restTemplateFallbackFromPolaris.getForEntity("/quickstart/callee/circuitBreak", String.class); - } - catch (HttpClientErrorException | HttpServerErrorException httpClientErrorException) { - return new ResponseEntity<>(httpClientErrorException.getResponseBodyAsString(), httpClientErrorException.getStatusCode()); - } - } - - /** - * RestTemplate circuit breaker with fallback from code. - * @return circuit breaker information of callee - */ - @GetMapping("/rest/fallbackFromCode") - public ResponseEntity circuitBreakRestTemplateFallbackFromCode() { - try { - return restTemplateFallbackFromCode.getForEntity("/quickstart/callee/circuitBreak", String.class); + return calleeRestTemplate.getForEntity("/quickstart/callee/circuitBreak", String.class); } catch (HttpClientErrorException | HttpServerErrorException httpClientErrorException) { return new ResponseEntity<>(httpClientErrorException.getResponseBodyAsString(), httpClientErrorException.getStatusCode()); diff --git a/spring-cloud-tencent-examples/tsf-example/consumer-demo/pom.xml b/spring-cloud-tencent-examples/tsf-example/consumer-demo/pom.xml index 44abeeb31..ca0dc4358 100644 --- a/spring-cloud-tencent-examples/tsf-example/consumer-demo/pom.xml +++ b/spring-cloud-tencent-examples/tsf-example/consumer-demo/pom.xml @@ -14,27 +14,7 @@ com.tencent.cloud - spring-cloud-starter-tencent-polaris-discovery - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-config - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-contract - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-router - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-circuitbreaker + spring-cloud-starter-tencent-all @@ -46,16 +26,6 @@ org.springframework.cloud spring-cloud-starter-openfeign - - - com.tencent.cloud - spring-cloud-starter-tencent-metadata-transfer - - - - com.tencent.cloud - spring-cloud-starter-tencent-trace-plugin - diff --git a/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/ConsumerApplication.java b/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/ConsumerApplication.java index d5479c2fd..7b7f3f91d 100644 --- a/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/ConsumerApplication.java +++ b/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/ConsumerApplication.java @@ -17,8 +17,6 @@ package com.tencent.cloud.tsf.demo.consumer; -import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; @@ -37,7 +35,6 @@ public class ConsumerApplication { @LoadBalanced @Bean - @PolarisCircuitBreaker public RestTemplate restTemplate() { return new RestTemplate(); } diff --git a/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/ConsumerController.java b/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/ConsumerController.java index 3173935f2..605147ece 100644 --- a/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/ConsumerController.java +++ b/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/ConsumerController.java @@ -19,14 +19,17 @@ package com.tencent.cloud.tsf.demo.consumer.controller; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import com.tencent.cloud.common.util.PolarisCompletableFutureUtils; import com.tencent.cloud.tsf.demo.consumer.proxy.ProviderDemoService; import com.tencent.cloud.tsf.demo.consumer.proxy.ProviderService; import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.tsf.core.context.TsfContext; +import org.springframework.tsf.core.TsfContext; import org.springframework.tsf.core.entity.Tag; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -62,7 +65,30 @@ public class ConsumerController { catch (CallAbortedException callAbortedException) { return callAbortedException.getMessage(); } + } + + @RequestMapping(value = "/echo-rest-async/{str}", method = RequestMethod.GET) + public String restAsync(@PathVariable String str, + @RequestParam(required = false) String tagName, + @RequestParam(required = false) String tagValue) throws ExecutionException, InterruptedException { + if (StringUtils.isNotBlank(tagName)) { + TsfContext.putTag(tagName, tagValue); + } + TsfContext.putTag("operation", "rest"); + Map mTags = new HashMap<>(); + mTags.put("rest-trace-key1", "value1"); + mTags.put("rest-trace-key2", "value2"); + TsfContext.putTags(mTags, Tag.ControlFlag.TRANSITIVE); + CompletableFuture echoFuture = PolarisCompletableFutureUtils.supplyAsync(() -> { + try { + return restTemplate.getForObject("http://provider-demo/echo/" + str, String.class); + } + catch (CallAbortedException callAbortedException) { + return callAbortedException.getMessage(); + } + }); + return echoFuture.get(); } @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) diff --git a/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/SdkBaseTest.java b/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/SdkBaseTest.java new file mode 100644 index 000000000..c90065c18 --- /dev/null +++ b/spring-cloud-tencent-examples/tsf-example/consumer-demo/src/main/java/com/tencent/cloud/tsf/demo/consumer/controller/SdkBaseTest.java @@ -0,0 +1,54 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.demo.consumer.controller; + +import com.tencent.polaris.api.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.tsf.core.TsfContext; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class SdkBaseTest { + + private static final Logger LOG = LoggerFactory.getLogger(SdkBaseTest.class); + + @Autowired + private RestTemplate restTemplate; + + // 调用一次provider接口 + @RequestMapping(value = "/echo-once/{str}", method = RequestMethod.GET) + public String restOnceProvider(@PathVariable String str, + @RequestParam(required = false) String tagName, + @RequestParam(required = false) String tagValue) { + if (!StringUtils.isEmpty(tagName)) { + TsfContext.putTag(tagName, tagValue); + } + LOG.info("start call echo-once"); + String result = restTemplate.getForObject("http://provider-demo/echo/" + str, String.class); + LOG.info("end call echo-once, the result is : " + result); + return result; + } +} diff --git a/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml b/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml index a8e3dedee..2af3215fa 100644 --- a/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml +++ b/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml @@ -14,27 +14,7 @@ com.tencent.cloud - spring-cloud-starter-tencent-polaris-discovery - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-config - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-contract - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-ratelimit - - - - com.tencent.cloud - spring-cloud-starter-tencent-polaris-auth + spring-cloud-starter-tencent-all @@ -49,14 +29,6 @@ org.springframework.cloud spring-cloud-starter-bootstrap - - com.tencent.cloud - spring-cloud-starter-tencent-metadata-transfer - - - com.tencent.cloud - spring-cloud-starter-tencent-trace-plugin - diff --git a/spring-cloud-tencent-plugin-starters/pom.xml b/spring-cloud-tencent-plugin-starters/pom.xml index 702dfeca4..b80d06b7f 100644 --- a/spring-cloud-tencent-plugin-starters/pom.xml +++ b/spring-cloud-tencent-plugin-starters/pom.xml @@ -21,6 +21,7 @@ spring-cloud-tencent-lossless-plugin spring-cloud-starter-tencent-threadlocal-plugin spring-cloud-starter-tencent-trace-plugin + spring-cloud-starter-tencent-fault-tolerance diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/pom.xml b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/pom.xml new file mode 100644 index 000000000..f22d224e7 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/pom.xml @@ -0,0 +1,34 @@ + + + + spring-cloud-tencent-plugin-starters + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-starter-tencent-fault-tolerance + Spring Cloud Starter Tencent Fault Tolerance Plugin + + + + org.springframework.cloud + spring-cloud-context + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.boot + spring-boot-starter-test + test + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/annotation/FaultTolerance.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/annotation/FaultTolerance.java new file mode 100644 index 000000000..2e2d8757a --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/annotation/FaultTolerance.java @@ -0,0 +1,91 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import com.tencent.cloud.plugin.faulttolerance.model.FaultToleranceStrategy; + +/** + * Annotation for Fault Tolerance. + * + * @author Haotian Zhang + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface FaultTolerance { + + /** + * Specifies a method to process fallback logic. + * A fallback method should be defined in the same class where is @FaultTolerance. + * Also a fallback method should have same signature to a method. + * for example: + *

+ *

+	 * @FaultTolerance(fallbackMethod = "getByIdFallback")
+	 * public String getById(String id) {
+	 *     // original method implementation
+	 * }
+	 *
+	 * public String getByIdFallback(String id) {
+	 *     // fallback method implementation
+	 * }
+	 * 
+ *

+ * @return method name + */ + String fallbackMethod() default ""; + + /** + * Defines exceptions which should be ignored. + * + * @return exceptions to ignore + */ + Class[] ignoreExceptions() default {}; + + /** + * Defines exceptions which should be retried. + * Default is all exceptions. + * + * @return exceptions to wrap + */ + Class[] raisedExceptions() default {}; + + /** + * Defines the fault tolerance strategy, the default is fast fail strategy. + * @see FaultToleranceStrategy + */ + FaultToleranceStrategy strategy() default FaultToleranceStrategy.FAIL_FAST; + + /** + * Number of retries, only used under {@link FaultToleranceStrategy}.FAIL_OVER strategy. Default is 0. + */ + int maxAttempts() default 0; + + /** + * The parallelism of forking is only used under the {@link FaultToleranceStrategy}.FORKING strategy. + */ + int parallelism() default 1; +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/common/FallbackMethod.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/common/FallbackMethod.java new file mode 100644 index 000000000..84671502b --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/common/FallbackMethod.java @@ -0,0 +1,43 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.common; + +import java.lang.reflect.Method; + +/** + * Fallback method for fault-tolerance. + * + * @author zhixinzxliu + */ +public class FallbackMethod { + + /** + * Absent fallback method. + */ + public static final FallbackMethod ABSENT = new FallbackMethod(null); + + private final Method method; + + public FallbackMethod(Method method) { + this.method = method; + } + + public Method getMethod() { + return method; + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/config/FaultToleranceAutoConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/config/FaultToleranceAutoConfiguration.java new file mode 100644 index 000000000..41246bcee --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/config/FaultToleranceAutoConfiguration.java @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.config; + +import com.tencent.cloud.plugin.faulttolerance.instrument.FaultToleranceAspect; + +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Role; + +/** + * FaultTolerance Auto-Configuration. + * + * @author Haotian Zhang + */ +@Configuration(proxyBeanMethods = false) +@Role(RootBeanDefinition.ROLE_INFRASTRUCTURE) +public class FaultToleranceAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public FaultToleranceAspect faultToleranceAspect() { + return new FaultToleranceAspect(); + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/instrument/FaultToleranceAspect.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/instrument/FaultToleranceAspect.java new file mode 100644 index 000000000..91803eba3 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/instrument/FaultToleranceAspect.java @@ -0,0 +1,248 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.instrument; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import com.tencent.cloud.plugin.faulttolerance.annotation.FaultTolerance; +import com.tencent.cloud.plugin.faulttolerance.common.FallbackMethod; +import com.tencent.cloud.plugin.faulttolerance.model.FaultToleranceStrategy; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.cloud.tsf.faulttolerance.annotation.TsfFaultTolerance; +import org.springframework.cloud.tsf.faulttolerance.model.TsfFaultToleranceStragety; + +/** + * Fault tolerance. + * + * @author zhixinzxliu, Haotian Zhang + */ +@Aspect +public class FaultToleranceAspect { + + private static final Logger logger = LoggerFactory.getLogger(FaultToleranceAspect.class); + + private final Map fallbackMethodMap = new ConcurrentHashMap<>(); + private final ExecutorService executorService = Executors.newCachedThreadPool(); + + @Pointcut("@annotation(org.springframework.cloud.tsf.faulttolerance.annotation.TsfFaultTolerance)" + + " || @annotation(com.tencent.cloud.plugin.faulttolerance.annotation.FaultTolerance)") + public void faultToleranceAnnotationPointcut() { + } + + @Around("faultToleranceAnnotationPointcut()") + public Object methodsAnnotatedWithFaultTolerance(final ProceedingJoinPoint joinPoint) throws Throwable { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + FaultTolerance faultTolerance = signature.getMethod().getAnnotation(FaultTolerance.class); + TsfFaultTolerance tsfFaultTolerance = signature.getMethod().getAnnotation(TsfFaultTolerance.class); + + Object result; + try { + // Invoke job in parallel. Whoever returns the result first will use it. + // The latter result will be ignored directly. + // If all exceptions occur, an exception will be thrown. + if (faultTolerance != null + && faultTolerance.strategy() == FaultToleranceStrategy.FORKING + && faultTolerance.parallelism() > 1) { + List> jobs = generateJobs(faultTolerance.parallelism(), joinPoint); + result = executorService.invokeAny(jobs); + } + else if (tsfFaultTolerance != null && + tsfFaultTolerance.strategy() == TsfFaultToleranceStragety.FORKING && + tsfFaultTolerance.parallelism() > 1) { + List> jobs = generateJobs(tsfFaultTolerance.parallelism(), joinPoint); + result = executorService.invokeAny(jobs); + } + else { + result = joinPoint.proceed(); + } + } + catch (Throwable throwable) { + return execFaultToleranceLogic(joinPoint, faultTolerance, tsfFaultTolerance, throwable); + } + + return result; + } + + private Object execFaultToleranceLogic(ProceedingJoinPoint joinPoint, FaultTolerance faultTolerance, TsfFaultTolerance tsfFaultTolerance, Throwable throwable) throws Throwable { + if (!needExecuteFaultTolerance(faultTolerance, tsfFaultTolerance, throwable)) { + throw throwable; + } + + // 重试逻辑 + if ((faultTolerance != null && faultTolerance.strategy() == FaultToleranceStrategy.FAIL_OVER) + || (tsfFaultTolerance != null && tsfFaultTolerance.strategy() == TsfFaultToleranceStragety.FAIL_OVER)) { + int maxAttempts = faultTolerance != null ? faultTolerance.maxAttempts() : tsfFaultTolerance.maxAttempts(); + while (maxAttempts > 0) { + try { + return joinPoint.proceed(); + } + catch (Throwable throwable1) { + if (!needExecuteFaultTolerance(faultTolerance, tsfFaultTolerance, throwable1)) { + throw throwable1; + } + + logger.error("The {} time retry error, will continue retry {} times.", maxAttempts, maxAttempts - 1, throwable1); + } + finally { + maxAttempts--; + } + } + } + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + if (method == null) { + throw throwable; + } + + FallbackMethod fallbackMethod = fallbackMethodMap.get(method); + if (fallbackMethod == null) { + fallbackMethod = resolveFallbackMethod(joinPoint); + fallbackMethodMap.putIfAbsent(method, fallbackMethod); + } + if (fallbackMethod.getMethod() != null) { + return fallbackMethod.getMethod().invoke(joinPoint.getTarget(), joinPoint.getArgs()); + } + + throw throwable; + } + + private List> generateJobs(int parallelism, final ProceedingJoinPoint joinPoint) { + List> jobs = new ArrayList<>(); + + for (int i = 0; i < parallelism; i++) { + jobs.add(() -> { + try { + return joinPoint.proceed(); + } + catch (Throwable t) { + throw new RuntimeException(t); + } + }); + } + + return jobs; + } + + /** + * 1. If the user sets ignoreExceptions and the current exception is a subclass of one of them, + * the fault-tolerant logic will not be executed.
+ * 2. If the user does not set ignoreExceptions or the current exception is not a subclass of ignoreExceptions + * and meets the following conditions, fault-tolerant logic will be executed:
+ * 2.1. If the user does not set raisedExceptions, fault tolerance logic will be executed.
+ * 2.2. The user has set raisedExceptions, and the current exception is a subclass of one of the raisedExceptions set by the user. + */ + private boolean needExecuteFaultTolerance(FaultTolerance faultTolerance, TsfFaultTolerance tsfFaultTolerance, Throwable throwable) { + if (faultTolerance == null && tsfFaultTolerance == null) { + return false; + } + Class[] ignoreExceptions = faultTolerance != null ? faultTolerance.ignoreExceptions() : null; + if (ignoreExceptions == null || ignoreExceptions.length == 0) { + ignoreExceptions = tsfFaultTolerance != null ? tsfFaultTolerance.ignoreExceptions() : null; + } + Class[] raisedExceptions = faultTolerance != null ? faultTolerance.raisedExceptions() : null; + if (raisedExceptions == null || raisedExceptions.length == 0) { + raisedExceptions = tsfFaultTolerance != null ? tsfFaultTolerance.raisedExceptions() : null; + } + + try { + + if (ignoreExceptions != null) { + for (Class ignoreException : ignoreExceptions) { + if (ignoreException.isAssignableFrom(throwable.getClass())) { + logger.debug("The exception {} is ignored.", throwable.getClass().getName()); + return false; + } + } + } + + if (raisedExceptions == null || raisedExceptions.length == 0) { + return true; + } + else { + for (Class raisedException : raisedExceptions) { + if (raisedException.isAssignableFrom(throwable.getClass())) { + return true; + } + } + } + } + catch (Throwable throwable1) { + logger.warn("Check exception {} failed.", throwable.getClass().getName(), throwable1); + return false; + } + + return false; + } + + protected FallbackMethod resolveFallbackMethod(ProceedingJoinPoint joinPoint) { + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + FaultTolerance faultTolerance = signature.getMethod().getAnnotation(FaultTolerance.class); + TsfFaultTolerance tsfFaultTolerance = signature.getMethod().getAnnotation(TsfFaultTolerance.class); + + String fallbackMethodName = faultTolerance != null ? faultTolerance.fallbackMethod() : tsfFaultTolerance.fallbackMethod(); + Class targetClass = joinPoint.getTarget().getClass(); + Class[] parameterTypes = signature.getMethod().getParameterTypes(); + + Method method = getDeclaredMethodFor(targetClass, fallbackMethodName, false, parameterTypes); + + if (method == null) { + return FallbackMethod.ABSENT; + } + else { + if (signature.getMethod().getReturnType().isAssignableFrom(method.getReturnType())) { + return new FallbackMethod(method); + } + else { + return FallbackMethod.ABSENT; + } + } + } + + private Method getDeclaredMethodFor(Class clazz, String name, boolean inRecursive, Class... parameterTypes) { + try { + return clazz.getDeclaredMethod(name, parameterTypes); + } + catch (NoSuchMethodException e) { + if (!inRecursive) { + logger.warn("Fallback method not found! Classname = {}, methodName = {}, parameterTypes = {}", + clazz.getName(), name, Arrays.toString(parameterTypes)); + } + Class superClass = clazz.getSuperclass(); + if (superClass != null) { + return getDeclaredMethodFor(superClass, name, true, parameterTypes); + } + } + return null; + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/model/FaultToleranceStrategy.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/model/FaultToleranceStrategy.java new file mode 100644 index 000000000..b2563b507 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/com/tencent/cloud/plugin/faulttolerance/model/FaultToleranceStrategy.java @@ -0,0 +1,37 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.model; + +public enum FaultToleranceStrategy { + /** + * Fails directly. For downstream services without idempotence, fail fast is recommended. + */ + FAIL_FAST, + + /** + * If the request is wrong, it will be retried. + */ + FAIL_OVER, + + /** + * Sending multiple requests at the same time requires the user to configure the degree of parallelism. + * For example, if two requests are sent at the same time, whichever one returns first will return the result. + * If the first request is an exception, it will wait for another request, and if all are exceptions, an exception will be returned. + */ + FORKING +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/EnableTsfFaultTolerance.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/EnableTsfFaultTolerance.java new file mode 100644 index 000000000..8156f6dac --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/EnableTsfFaultTolerance.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.cloud.tsf.faulttolerance.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfFaultTolerance { +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/TsfFaultTolerance.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/TsfFaultTolerance.java new file mode 100644 index 000000000..53b96464d --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/TsfFaultTolerance.java @@ -0,0 +1,90 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.cloud.tsf.faulttolerance.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.cloud.tsf.faulttolerance.model.TsfFaultToleranceStragety; + +/** + * @author zhixinzxiliu + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@Deprecated(since = "2.0.0.0") +public @interface TsfFaultTolerance { + /** + * Specifies a method to process fallback logic. + * A fallback method should be defined in the same class where is TsfFaultTolerance. + * Also a fallback method should have same signature to a method. + * for example: + * + * @TsfFaultTolerance(fallbackMethod = "getByIdFallback") + * public String getById(String id) {...} + * + * public String getByIdFallback(String id) {...} + * + * + * @return method name + */ + String fallbackMethod() default ""; + + /** + * Specifies command properties. + * + * @return command properties + */ + TsfFaultToleranceProperty[] properties() default {}; + + /** + * Defines exceptions which should be ignored. + * + * @return exceptions to ignore + */ + Class[] ignoreExceptions() default {}; + + /** + * Defines exceptions which should be retry. + * Default is all exceptions. + * + * @return exceptions to wrap + */ + Class[] raisedExceptions() default {}; + + /** + * 设置容错策略,默认为快速失败策略. + */ + TsfFaultToleranceStragety strategy() default TsfFaultToleranceStragety.FAIL_FAST; + + /** + * 重试次数,只在Failover策略下用到. + */ + int maxAttempts() default 0; + + /** + * forking 的并行度,只在Forking策略下用到. + */ + int parallelism() default 1; +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/TsfFaultToleranceProperty.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/TsfFaultToleranceProperty.java new file mode 100644 index 000000000..a5c051f1b --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/annotation/TsfFaultToleranceProperty.java @@ -0,0 +1,52 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.cloud.tsf.faulttolerance.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation allows specify TsfFaultTolerance properties in the following format: + * property name = property value. + * + * @author zhixinzxliu + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Deprecated(since = "2.0.0.0") +public @interface TsfFaultToleranceProperty { + + /** + * Property name. + * + * @return name + */ + String name(); + + /** + * Property value. + * + * @return value + */ + String value(); + +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/model/TsfFaultToleranceStragety.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/model/TsfFaultToleranceStragety.java new file mode 100644 index 000000000..86953fae8 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/java/org/springframework/cloud/tsf/faulttolerance/model/TsfFaultToleranceStragety.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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 org.springframework.cloud.tsf.faulttolerance.model; + +@Deprecated(since = "2.0.0.0") +public enum TsfFaultToleranceStragety { + /** + * Fails directly. For downstream services without idempotence, fail fast is recommended. + */ + FAIL_FAST, + + /** + * If the request is wrong, it will be retried. + */ + FAIL_OVER, + + /** + * Sending multiple requests at the same time requires the user to configure the degree of parallelism. + * For example, if two requests are sent at the same time, whichever one returns first will return the result. + * If the first request is an exception, it will wait for another request, and if all are exceptions, an exception will be returned. + */ + FORKING +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 000000000..095822ab9 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.tencent.cloud.plugin.faulttolerance.config.FaultToleranceAutoConfiguration diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/test/java/com/tencent/cloud/plugin/faulttolerance/integration/FaultToleranceService.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/test/java/com/tencent/cloud/plugin/faulttolerance/integration/FaultToleranceService.java new file mode 100644 index 000000000..7bba88ac8 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/test/java/com/tencent/cloud/plugin/faulttolerance/integration/FaultToleranceService.java @@ -0,0 +1,73 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.integration; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.tencent.cloud.plugin.faulttolerance.annotation.FaultTolerance; +import com.tencent.cloud.plugin.faulttolerance.model.FaultToleranceStrategy; + +/** + * Fault tolerance test service. + * + * @author Haotian Zhang + */ +public class FaultToleranceService { + + private final AtomicInteger failFastCount = new AtomicInteger(0); + private final AtomicInteger failOverCount = new AtomicInteger(0); + private final AtomicInteger forkingCount = new AtomicInteger(0); + + @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_FAST, fallbackMethod = "fallback") + public String failFast() { + failFastCount.getAndIncrement(); + throw new RuntimeException("NO"); + } + + public String fallback() { + return "fallback"; + } + + @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_OVER, maxAttempts = 3) + public String failOver() { + if (failOverCount.incrementAndGet() % 4 == 0) { + return "OK"; + } + throw new RuntimeException("NO"); + } + + @FaultTolerance(strategy = FaultToleranceStrategy.FORKING, parallelism = 4) + public String forking() { + if (forkingCount.incrementAndGet() % 4 == 0) { + return "OK"; + } + throw new RuntimeException("NO"); + } + + public int getFailFastCount() { + return failFastCount.get(); + } + + public int getFailOverCount() { + return failOverCount.get(); + } + + public int getForkingCount() { + return forkingCount.get(); + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/test/java/com/tencent/cloud/plugin/faulttolerance/integration/FaultToleranceTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/test/java/com/tencent/cloud/plugin/faulttolerance/integration/FaultToleranceTest.java new file mode 100644 index 000000000..dd931d13d --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-fault-tolerance/src/test/java/com/tencent/cloud/plugin/faulttolerance/integration/FaultToleranceTest.java @@ -0,0 +1,74 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.plugin.faulttolerance.integration; + +import com.tencent.cloud.plugin.faulttolerance.config.FaultToleranceAutoConfiguration; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration test for Fault Tolerance. + * + * @author Haotian Zhang + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = FaultToleranceTest.TestApplication.class) +public class FaultToleranceTest { + + @Autowired + private FaultToleranceService testService; + + @Test + public void testFailFast() { + assertThat(testService.failFast()).isEqualTo("fallback"); + assertThat(testService.getFailFastCount()).isEqualTo(1); + } + + @Test + public void testFailOver() { + assertThat(testService.failOver()).isEqualTo("OK"); + assertThat(testService.getFailOverCount()).isEqualTo(4); + } + + @Test + public void testForking() { + assertThat(testService.forking()).isEqualTo("OK"); + assertThat(testService.getForkingCount()).isEqualTo(4); + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration(FaultToleranceAutoConfiguration.class) + public static class TestApplication { + + @Bean + public FaultToleranceService faultToleranceService() { + return new FaultToleranceService(); + } + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/tsf/monitor/annotation/EnableTsfMonitor.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/tsf/monitor/annotation/EnableTsfMonitor.java new file mode 100644 index 000000000..387cfac21 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/tsf/monitor/annotation/EnableTsfMonitor.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.monitor.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfMonitor { + +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/tsf/sleuth/annotation/EnableTsfSleuth.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/tsf/sleuth/annotation/EnableTsfSleuth.java new file mode 100644 index 000000000..289134d59 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-trace-plugin/src/main/java/com/tencent/tsf/sleuth/annotation/EnableTsfSleuth.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 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.tsf.sleuth.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Empty annotation. Compatible with old versions TSF SDK. + * + * @author Haotian Zhang + */ +@Deprecated(since = "2.0.0.0") +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface EnableTsfSleuth { + +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfContextConfigModifier.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfContextConfigModifier.java index 13f060de8..5c589c4e6 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfContextConfigModifier.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/extend/tsf/TsfContextConfigModifier.java @@ -21,6 +21,7 @@ import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.config.extend.consul.ConsulProperties; import com.tencent.polaris.api.config.plugin.DefaultPlugins; +import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.plugins.event.tsf.TsfEventReporterConfig; @@ -45,6 +46,13 @@ public class TsfContextConfigModifier implements PolarisConfigModifier { configuration.getGlobal().getEventReporter().getReporters().add(DefaultPlugins.TSF_EVENT_REPORTER_TYPE); TsfEventReporterConfig tsfEventReporterConfig = new TsfEventReporterConfig(); + if (StringUtils.isNotBlank(tsfCoreProperties.getEventMasterIp())) { + tsfEventReporterConfig.setEnable(true); + } + else { + tsfEventReporterConfig.setEnable(false); + return; + } tsfEventReporterConfig.setEventMasterIp(tsfCoreProperties.getEventMasterIp()); tsfEventReporterConfig.setEventMasterPort(tsfCoreProperties.getEventMasterPort()); tsfEventReporterConfig.setAppId(tsfCoreProperties.getAppId()); diff --git a/spring-cloud-tencent-polaris-context/src/main/java/org/springframework/tsf/annotation/EnableTsf.java b/spring-cloud-tencent-polaris-context/src/main/java/org/springframework/tsf/annotation/EnableTsf.java index 890ff9b72..a4fd51cc2 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/org/springframework/tsf/annotation/EnableTsf.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/org/springframework/tsf/annotation/EnableTsf.java @@ -24,22 +24,15 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.client.discovery.EnableDiscoveryClient; - /** - * Compatible with old versions TSF SDK. + * Empty annotation. Compatible with old versions TSF SDK. * * @author Haotian Zhang */ - +@Deprecated(since = "2.0.0.0") @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited -@EnableAutoConfiguration -@EnableDiscoveryClient // 服务注册发现 -@EnableConfigurationProperties // 分布式配置 public @interface EnableTsf { } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java index fef771c94..1a211fae6 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java @@ -23,24 +23,24 @@ import java.util.List; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignBeanPostProcessor; -import com.tencent.cloud.rpc.enhancement.feign.PolarisLoadBalancerFeignRequestTransformer; -import com.tencent.cloud.rpc.enhancement.filter.EnhancedReactiveFilter; -import com.tencent.cloud.rpc.enhancement.filter.EnhancedServletFilter; +import com.tencent.cloud.rpc.enhancement.instrument.feign.EnhancedFeignBeanPostProcessor; +import com.tencent.cloud.rpc.enhancement.instrument.feign.PolarisLoadBalancerFeignRequestTransformer; +import com.tencent.cloud.rpc.enhancement.instrument.filter.EnhancedReactiveFilter; +import com.tencent.cloud.rpc.enhancement.instrument.filter.EnhancedServletFilter; +import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.EnhancedRestTemplateInterceptor; +import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer; +import com.tencent.cloud.rpc.enhancement.instrument.scg.EnhancedGatewayGlobalFilter; +import com.tencent.cloud.rpc.enhancement.instrument.webclient.EnhancedWebClientExchangeFilterFunction; +import com.tencent.cloud.rpc.enhancement.instrument.webclient.PolarisLoadBalancerClientRequestTransformer; import com.tencent.cloud.rpc.enhancement.plugin.DefaultEnhancedPluginRunner; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner; import com.tencent.cloud.rpc.enhancement.plugin.reporter.ExceptionPolarisReporter; import com.tencent.cloud.rpc.enhancement.plugin.reporter.SuccessPolarisReporter; -import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; -import com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer; -import com.tencent.cloud.rpc.enhancement.scg.EnhancedGatewayGlobalFilter; import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer; import com.tencent.cloud.rpc.enhancement.transformer.PolarisInstanceTransformer; import com.tencent.cloud.rpc.enhancement.transformer.PolarisRegistrationTransformer; import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; -import com.tencent.cloud.rpc.enhancement.webclient.EnhancedWebClientExchangeFilterFunction; -import com.tencent.cloud.rpc.enhancement.webclient.PolarisLoadBalancerClientRequestTransformer; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignBeanPostProcessor.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignBeanPostProcessor.java index 132f5f26e..f4aed2a3c 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignBeanPostProcessor.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.feign; +package com.tencent.cloud.rpc.enhancement.instrument.feign; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner; import feign.Client; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignClient.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignClient.java index 8650c36f8..e4c2d1a5b 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignClient.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.feign; +package com.tencent.cloud.rpc.enhancement.instrument.feign; import java.io.IOException; import java.net.URI; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/PolarisLoadBalancerFeignRequestTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/PolarisLoadBalancerFeignRequestTransformer.java similarity index 88% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/PolarisLoadBalancerFeignRequestTransformer.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/PolarisLoadBalancerFeignRequestTransformer.java index 8ab2cc5dd..eeb17ef1e 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/PolarisLoadBalancerFeignRequestTransformer.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/feign/PolarisLoadBalancerFeignRequestTransformer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.feign; +package com.tencent.cloud.rpc.enhancement.instrument.feign; import com.tencent.cloud.common.metadata.MetadataContextHolder; import feign.Request; @@ -23,7 +23,7 @@ import feign.Request; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.openfeign.loadbalancer.LoadBalancerFeignRequestTransformer; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; /** * PolarisLoadBalancerFeignRequestTransformer. diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/filter/EnhancedReactiveFilter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/filter/EnhancedReactiveFilter.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/filter/EnhancedReactiveFilter.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/filter/EnhancedReactiveFilter.java index d2c935cb5..a830ea79e 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/filter/EnhancedReactiveFilter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/filter/EnhancedReactiveFilter.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.filter; +package com.tencent.cloud.rpc.enhancement.instrument.filter; import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/filter/EnhancedServletFilter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/filter/EnhancedServletFilter.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/filter/EnhancedServletFilter.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/filter/EnhancedServletFilter.java index 970993903..1c1142da9 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/filter/EnhancedServletFilter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/filter/EnhancedServletFilter.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.filter; +package com.tencent.cloud.rpc.enhancement.instrument.filter; import java.io.IOException; import java.net.URI; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/EnhancedRestTemplateInterceptor.java similarity index 94% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/EnhancedRestTemplateInterceptor.java index fdadc59de..5f739118c 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/EnhancedRestTemplateInterceptor.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.resttemplate; +package com.tencent.cloud.rpc.enhancement.instrument.resttemplate; import java.io.IOException; @@ -32,7 +32,7 @@ import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; /** * EnhancedRestTemplateInterceptor. diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/PolarisLoadBalancerRequestTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/PolarisLoadBalancerRequestTransformer.java similarity index 96% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/PolarisLoadBalancerRequestTransformer.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/PolarisLoadBalancerRequestTransformer.java index dd7a5de34..f72ea4c74 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/PolarisLoadBalancerRequestTransformer.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/PolarisLoadBalancerRequestTransformer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.resttemplate; +package com.tencent.cloud.rpc.enhancement.instrument.resttemplate; import com.tencent.cloud.common.metadata.MetadataContextHolder; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/scg/EnhancedGatewayGlobalFilter.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/scg/EnhancedGatewayGlobalFilter.java index e6f2bed2e..1cd2748b5 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/scg/EnhancedGatewayGlobalFilter.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.scg; +package com.tencent.cloud.rpc.enhancement.instrument.scg; import java.net.URI; diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/EnhancedWebClientExchangeFilterFunction.java similarity index 94% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/EnhancedWebClientExchangeFilterFunction.java index c2e599485..e8d8f1231 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/EnhancedWebClientExchangeFilterFunction.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.webclient; +package com.tencent.cloud.rpc.enhancement.instrument.webclient; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; @@ -31,7 +31,7 @@ import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.ExchangeFunction; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; /** * EnhancedWebClientExchangeFilterFunction. diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/PolarisLoadBalancerClientRequestTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/PolarisLoadBalancerClientRequestTransformer.java similarity index 88% rename from spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/PolarisLoadBalancerClientRequestTransformer.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/PolarisLoadBalancerClientRequestTransformer.java index 8737d4beb..cf7bb9df0 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/PolarisLoadBalancerClientRequestTransformer.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/PolarisLoadBalancerClientRequestTransformer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.webclient; +package com.tencent.cloud.rpc.enhancement.instrument.webclient; import com.tencent.cloud.common.metadata.MetadataContextHolder; @@ -23,7 +23,7 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerClientRequestTransformer; import org.springframework.web.reactive.function.client.ClientRequest; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; /** * PolarisLoadBalancerClientRequestTransformer. diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java index b84fa6343..dd3911967 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java @@ -18,11 +18,11 @@ package com.tencent.cloud.rpc.enhancement.config; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignBeanPostProcessor; +import com.tencent.cloud.rpc.enhancement.instrument.feign.EnhancedFeignBeanPostProcessor; +import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.EnhancedRestTemplateInterceptor; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner; import com.tencent.cloud.rpc.enhancement.plugin.reporter.ExceptionPolarisReporter; import com.tencent.cloud.rpc.enhancement.plugin.reporter.SuccessPolarisReporter; -import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessorTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignBeanPostProcessorTest.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessorTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignBeanPostProcessorTest.java index 17868daa7..6dbca5fec 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessorTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignBeanPostProcessorTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.feign; +package com.tencent.cloud.rpc.enhancement.instrument.feign; import feign.Client; import org.junit.jupiter.api.BeforeEach; diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClientTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignClientTest.java similarity index 99% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClientTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignClientTest.java index debbd1d94..8cf85b46a 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClientTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/EnhancedFeignClientTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.feign; +package com.tencent.cloud.rpc.enhancement.instrument.feign; import java.io.IOException; import java.util.ArrayList; diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/PolarisLoadBalancerFeignRequestTransformerTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/PolarisLoadBalancerFeignRequestTransformerTest.java similarity index 91% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/PolarisLoadBalancerFeignRequestTransformerTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/PolarisLoadBalancerFeignRequestTransformerTest.java index 4f259d289..8e78c95b5 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/PolarisLoadBalancerFeignRequestTransformerTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/feign/PolarisLoadBalancerFeignRequestTransformerTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.feign; +package com.tencent.cloud.rpc.enhancement.instrument.feign; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; @@ -36,7 +36,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.cloud.client.ServiceInstance; import org.springframework.context.ApplicationContext; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; @@ -72,7 +72,8 @@ public class PolarisLoadBalancerFeignRequestTransformerTest { StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class); doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class); doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class); - mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext); + mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext) + .thenReturn(applicationContext); } @AfterAll @@ -89,6 +90,7 @@ public class PolarisLoadBalancerFeignRequestTransformerTest { @Test public void test() throws Throwable { transformer.transformRequest(clientRequest, serviceInstance); - assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance); + assertThat(MetadataContextHolder.get().getLoadbalancerMetadata() + .get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptorTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/EnhancedRestTemplateInterceptorTest.java similarity index 97% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptorTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/EnhancedRestTemplateInterceptorTest.java index 659d1cd15..7da97785b 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptorTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/EnhancedRestTemplateInterceptorTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.resttemplate; +package com.tencent.cloud.rpc.enhancement.instrument.resttemplate; import java.io.IOException; import java.net.SocketTimeoutException; @@ -62,12 +62,12 @@ public class EnhancedRestTemplateInterceptorTest { private static MockedStatic mockedApplicationContextAwareUtils; @Mock + Registration registration; + @Mock private RpcEnhancementReporterProperties reporterProperties; @Mock private SDKContext sdkContext; @Mock - Registration registration; - @Mock private ClientHttpRequestExecution mockClientHttpRequestExecution; @Mock private ClientHttpResponse mockClientHttpResponse; @@ -86,7 +86,8 @@ public class EnhancedRestTemplateInterceptorTest { StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class); doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class); doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class); - mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext); + mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext) + .thenReturn(applicationContext); } @AfterAll diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/PolarisLoadBalancerRequestTransformerTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/PolarisLoadBalancerRequestTransformerTest.java similarity index 90% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/PolarisLoadBalancerRequestTransformerTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/PolarisLoadBalancerRequestTransformerTest.java index 71bbd822f..3da230d61 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/PolarisLoadBalancerRequestTransformerTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/resttemplate/PolarisLoadBalancerRequestTransformerTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.resttemplate; +package com.tencent.cloud.rpc.enhancement.instrument.resttemplate; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; @@ -36,7 +36,7 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpRequest; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; @@ -66,7 +66,8 @@ public class PolarisLoadBalancerRequestTransformerTest { StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class); doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class); doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class); - mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext); + mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext) + .thenReturn(applicationContext); } @AfterAll @@ -83,7 +84,8 @@ public class PolarisLoadBalancerRequestTransformerTest { @Test public void test() throws Throwable { transformer.transformRequest(clientRequest, serviceInstance); - assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance); + assertThat(MetadataContextHolder.get().getLoadbalancerMetadata() + .get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/scg/EnhancedGatewayGlobalFilterTest.java similarity index 98% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/scg/EnhancedGatewayGlobalFilterTest.java index 0c6583a55..21174b5a6 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/scg/EnhancedGatewayGlobalFilterTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.scg; +package com.tencent.cloud.rpc.enhancement.instrument.scg; import java.net.URI; import java.net.URISyntaxException; diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunctionTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/EnhancedWebClientExchangeFilterFunctionTest.java similarity index 96% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunctionTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/EnhancedWebClientExchangeFilterFunctionTest.java index a29d65cbd..67502167d 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunctionTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/EnhancedWebClientExchangeFilterFunctionTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.webclient; +package com.tencent.cloud.rpc.enhancement.instrument.webclient; import java.net.URI; import java.net.URISyntaxException; @@ -84,7 +84,8 @@ public class EnhancedWebClientExchangeFilterFunctionTest { StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class); doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class); doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class); - mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext); + mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext) + .thenReturn(applicationContext); } @AfterAll @@ -97,6 +98,7 @@ public class EnhancedWebClientExchangeFilterFunctionTest { MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST; MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER; } + @Test public void testRun() throws URISyntaxException { @@ -117,8 +119,8 @@ public class EnhancedWebClientExchangeFilterFunctionTest { doReturn(Mono.error(new RuntimeException())).when(exchangeFunction).exchange(any()); - assertThatThrownBy(() -> reporter.filter(clientRequest, exchangeFunction).block()).isInstanceOf(RuntimeException.class); - + assertThatThrownBy(() -> reporter.filter(clientRequest, exchangeFunction) + .block()).isInstanceOf(RuntimeException.class); } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/webclient/PolarisLoadBalancerClientRequestTransformerTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/PolarisLoadBalancerClientRequestTransformerTest.java similarity index 91% rename from spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/webclient/PolarisLoadBalancerClientRequestTransformerTest.java rename to spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/PolarisLoadBalancerClientRequestTransformerTest.java index eec316bcc..0519402f1 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/webclient/PolarisLoadBalancerClientRequestTransformerTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/instrument/webclient/PolarisLoadBalancerClientRequestTransformerTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.rpc.enhancement.webclient; +package com.tencent.cloud.rpc.enhancement.instrument.webclient; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; @@ -36,7 +36,7 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.context.ApplicationContext; import org.springframework.web.reactive.function.client.ClientRequest; -import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; +import static com.tencent.cloud.rpc.enhancement.instrument.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE; import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; @@ -67,7 +67,8 @@ public class PolarisLoadBalancerClientRequestTransformerTest { StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class); doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class); doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class); - mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext); + mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext) + .thenReturn(applicationContext); } @AfterAll @@ -84,6 +85,7 @@ public class PolarisLoadBalancerClientRequestTransformerTest { @Test public void test() throws Throwable { transformer.transformRequest(clientRequest, serviceInstance); - assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance); + assertThat(MetadataContextHolder.get().getLoadbalancerMetadata() + .get(LOAD_BALANCER_SERVICE_INSTANCE)).isEqualTo(serviceInstance); } }