From 8bf17080d32cce742f5dc300555bcd908d7bb1da Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 31 Aug 2023 15:56:27 +0800 Subject: [PATCH 1/2] fix:fix feign url bug when using sleuth. --- .../feign/EnhancedFeignBeanPostProcessor.java | 48 +++++++++++-------- .../EnhancedLoadBalancerFeignClient.java | 37 -------------- .../EnhancedFeignBeanPostProcessorTest.java | 7 +-- ...edFeignBlockingLoadBalancerClientTest.java | 35 -------------- .../EnhancedLoadBalancerFeignClientTest.java | 39 --------------- 5 files changed, 33 insertions(+), 133 deletions(-) delete mode 100644 spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClient.java delete mode 100644 spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBlockingLoadBalancerClientTest.java delete mode 100644 spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClientTest.java 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/feign/EnhancedFeignBeanPostProcessor.java index 3e0fedd0f..c1eb56824 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/feign/EnhancedFeignBeanPostProcessor.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory; import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; @@ -40,7 +41,6 @@ public class EnhancedFeignBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware { private final EnhancedPluginRunner pluginRunner; - private BeanFactory factory; public EnhancedFeignBeanPostProcessor(EnhancedPluginRunner pluginRunner) { @@ -54,24 +54,28 @@ public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull Str private Object wrapper(Object bean) { if (isNeedWrap(bean)) { + Client delegate; if (bean instanceof LoadBalancerFeignClient) { - LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean); - return new EnhancedLoadBalancerFeignClient( - createPolarisFeignClient(client.getDelegate()), - factory(), - clientFactory()); - } - if (bean instanceof RetryableFeignBlockingLoadBalancerClient - || bean instanceof FeignBlockingLoadBalancerClient) { - Client delegate; - if (bean instanceof RetryableFeignBlockingLoadBalancerClient) { - delegate = ((RetryableFeignBlockingLoadBalancerClient) bean).getDelegate(); + delegate = ((LoadBalancerFeignClient) bean).getDelegate(); + if (delegate != null && !(delegate instanceof EnhancedFeignClient)) { + return new LoadBalancerFeignClient(createPolarisFeignClient(delegate), + factory(), + clientFactory()); } - else { - delegate = ((FeignBlockingLoadBalancerClient) bean).getDelegate(); + } + if (bean instanceof RetryableFeignBlockingLoadBalancerClient) { + delegate = ((RetryableFeignBlockingLoadBalancerClient) bean).getDelegate(); + if (delegate != null && !(delegate instanceof EnhancedFeignClient)) { + EnhancedFeignClient client = createPolarisFeignClient(delegate); + return new RetryableFeignBlockingLoadBalancerClient(client, + blockingLoadBalancerClient(), + loadBalancedRetryFactory()); } - if (delegate != null) { - return new EnhancedFeignBlockingLoadBalancerClient(createPolarisFeignClient(delegate), + } + if (bean instanceof FeignBlockingLoadBalancerClient) { + delegate = ((FeignBlockingLoadBalancerClient) bean).getDelegate(); + if (delegate != null && !(delegate instanceof EnhancedFeignClient)) { + return new FeignBlockingLoadBalancerClient(createPolarisFeignClient(delegate), factory.getBean(BlockingLoadBalancerClient.class)); } } @@ -81,9 +85,7 @@ private Object wrapper(Object bean) { } private boolean isNeedWrap(Object bean) { - return bean instanceof Client && !(bean instanceof EnhancedFeignClient) - && !(bean instanceof EnhancedFeignBlockingLoadBalancerClient) - && !(bean instanceof EnhancedLoadBalancerFeignClient); + return bean instanceof Client && !(bean instanceof EnhancedFeignClient); } private EnhancedFeignClient createPolarisFeignClient(Client delegate) { @@ -102,4 +104,12 @@ CachingSpringLoadBalancerFactory factory() { SpringClientFactory clientFactory() { return this.factory.getBean(SpringClientFactory.class); } + + LoadBalancedRetryFactory loadBalancedRetryFactory() { + return this.factory.getBean(LoadBalancedRetryFactory.class); + } + + BlockingLoadBalancerClient blockingLoadBalancerClient() { + return this.factory.getBean(BlockingLoadBalancerClient.class); + } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClient.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClient.java deleted file mode 100644 index 4dc30234b..000000000 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClient.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.rpc.enhancement.feign; - -import feign.Client; - -import org.springframework.cloud.netflix.ribbon.SpringClientFactory; -import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; -import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; - -/** - * Wrap for {@link LoadBalancerFeignClient}. - * - * @author Haotian Zhang - */ -public class EnhancedLoadBalancerFeignClient extends LoadBalancerFeignClient { - - public EnhancedLoadBalancerFeignClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory, - SpringClientFactory clientFactory) { - super(delegate, lbClientFactory, clientFactory); - } -} 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/feign/EnhancedFeignBeanPostProcessorTest.java index d654276ee..821a9a49a 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/feign/EnhancedFeignBeanPostProcessorTest.java @@ -71,7 +71,6 @@ public void testPostProcessBeforeInitialization() { Object bean = enhancedFeignBeanPostProcessor.postProcessBeforeInitialization(bean1, "bean1"); assertThat(bean).isNotInstanceOfAny( EnhancedFeignClient.class, - EnhancedLoadBalancerFeignClient.class, EnhancedFeignBlockingLoadBalancerClient.class); // bean instanceOf Client.class @@ -83,12 +82,14 @@ public void testPostProcessBeforeInitialization() { LoadBalancerFeignClient bean3 = mock(LoadBalancerFeignClient.class); doReturn(mock(Client.class)).when(bean3).getDelegate(); bean = enhancedFeignBeanPostProcessor.postProcessBeforeInitialization(bean3, "bean3"); - assertThat(bean).isInstanceOf(EnhancedLoadBalancerFeignClient.class); + assertThat(bean).isInstanceOf(LoadBalancerFeignClient.class); + assertThat(((LoadBalancerFeignClient) bean).getDelegate()).isInstanceOf(EnhancedFeignClient.class); // bean instanceOf FeignBlockingLoadBalancerClient.class FeignBlockingLoadBalancerClient bean4 = mock(FeignBlockingLoadBalancerClient.class); doReturn(mock(Client.class)).when(bean4).getDelegate(); bean = enhancedFeignBeanPostProcessor.postProcessBeforeInitialization(bean4, "bean4"); - assertThat(bean).isInstanceOf(EnhancedFeignBlockingLoadBalancerClient.class); + assertThat(bean).isInstanceOf(FeignBlockingLoadBalancerClient.class); + assertThat(((FeignBlockingLoadBalancerClient) bean).getDelegate()).isInstanceOf(EnhancedFeignClient.class); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBlockingLoadBalancerClientTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBlockingLoadBalancerClientTest.java deleted file mode 100644 index 56dd2ef69..000000000 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBlockingLoadBalancerClientTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.rpc.enhancement.feign; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThatCode; - -/** - * Test for {@link EnhancedFeignBlockingLoadBalancerClient}. - * - * @author Haotian Zhang - */ -public class EnhancedFeignBlockingLoadBalancerClientTest { - - @Test - public void testConstructor() { - assertThatCode(() -> new EnhancedFeignBlockingLoadBalancerClient(null, null)).doesNotThrowAnyException(); - } -} diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClientTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClientTest.java deleted file mode 100644 index 2207b47dd..000000000 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedLoadBalancerFeignClientTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.rpc.enhancement.feign; - -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Test for {@link EnhancedLoadBalancerFeignClient}. - * - * @author Haotian Zhang - */ -public class EnhancedLoadBalancerFeignClientTest { - - @Test - public void testConstructor() { - try { - new EnhancedLoadBalancerFeignClient(null, null, null); - } - catch (Exception e) { - Assertions.fail("Exception encountered.", e); - } - } -} From ed1f73fb0e3611b35a66b995b3998fdb952f280c Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Sat, 16 Sep 2023 17:22:57 +0800 Subject: [PATCH 2/2] fix:fix feign url bug when using sleuth. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56316d59b..9fc10669b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,4 @@ - [fix:the polaris config relation non-daemon thread should stop when application fails to start.](https://github.com/Tencent/spring-cloud-tencent/pull/1110) - [Refactoring:remove invalid @AutoConfigureAfter and @AutoConfigureBefore from discovery client automatic configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1118) +- [fix:fix feign url bug when using sleuth.](https://github.com/Tencent/spring-cloud-tencent/pull/1119)