From abad0c32ced3dcd68f86af0d434dae72deb38da4 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 27 Sep 2023 11:45:56 +0800 Subject: [PATCH] fix:fix retry loadbalancer not working bug. (#1154) --- CHANGELOG.md | 1 + .../PolarisLoadBalancerAutoConfiguration.java | 14 +++++++++----- .../discovery-caller-service/pom.xml | 5 +++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afd75ffba..857e2af72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1139) - [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1146) - [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1148) +- [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1154) 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 8ee742d6a..df296cd5b 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 @@ -25,12 +25,12 @@ import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterc import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor; import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer; +import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration; import org.springframework.context.annotation.Bean; @@ -52,19 +52,23 @@ import org.springframework.http.client.ClientHttpRequestInterceptor; public class PolarisLoadBalancerAutoConfiguration { @Bean - @ConditionalOnMissingBean - public RestTemplateCustomizer restTemplateCustomizer(@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) { + public RestTemplateCustomizer restTemplateCustomizer( + @Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor, + @Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) { return restTemplate -> { List list = new ArrayList<>(restTemplate.getInterceptors()); // LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor - if (loadBalancerInterceptor != null) { + if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) { int addIndex = list.size(); for (int i = 0; i < list.size(); i++) { if (list.get(i) instanceof EnhancedRestTemplateInterceptor) { addIndex = i; } } - list.add(addIndex, loadBalancerInterceptor); + list.add(addIndex, + retryLoadBalancerInterceptor != null + ? retryLoadBalancerInterceptor + : loadBalancerInterceptor); } restTemplate.setInterceptors(list); }; diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml index 69e5c1e64..f6a28f36e 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml @@ -33,6 +33,11 @@ spring-cloud-starter-tencent-polaris-contract + + + + +