From 44426a816e033ea7f7d235bcd2963fcea6ce79c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=81=E5=8C=A0=E5=90=9B?= Date: Wed, 21 Feb 2024 13:08:20 +0800 Subject: [PATCH] fix:fix restTemplateCustomizer bean conflict causing service to fail to start properly. --- .../PolarisLoadBalancerAutoConfiguration.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) 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 df296cd5b..c9fa66fa9 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 @@ -36,6 +36,7 @@ import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfigurati import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.util.CollectionUtils; /** * Auto-configuration of loadbalancer for Polaris. @@ -52,23 +53,38 @@ import org.springframework.http.client.ClientHttpRequestInterceptor; public class PolarisLoadBalancerAutoConfiguration { @Bean - public RestTemplateCustomizer restTemplateCustomizer( + public RestTemplateCustomizer polarisRestTemplateCustomizer( @Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor, @Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) { return restTemplate -> { List list = new ArrayList<>(restTemplate.getInterceptors()); // LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor - if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) { - int addIndex = list.size(); + int addIndex = list.size(); + if (CollectionUtils.containsInstance(list, retryLoadBalancerInterceptor) || CollectionUtils.containsInstance(list, loadBalancerInterceptor)) { + ClientHttpRequestInterceptor enhancedRestTemplateInterceptor = null; for (int i = 0; i < list.size(); i++) { if (list.get(i) instanceof EnhancedRestTemplateInterceptor) { + enhancedRestTemplateInterceptor = list.get(i); addIndex = i; } } - list.add(addIndex, - retryLoadBalancerInterceptor != null - ? retryLoadBalancerInterceptor - : loadBalancerInterceptor); + if (enhancedRestTemplateInterceptor != null) { + list.remove(addIndex); + list.add(enhancedRestTemplateInterceptor); + } + } + else { + if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i) instanceof EnhancedRestTemplateInterceptor) { + addIndex = i; + } + } + list.add(addIndex, + retryLoadBalancerInterceptor != null + ? retryLoadBalancerInterceptor + : loadBalancerInterceptor); + } } restTemplate.setInterceptors(list); };