From 249578f7415df0e073e17ee1bb3366f3f1a87911 Mon Sep 17 00:00:00 2001 From: Haotian Zhang Date: Wed, 8 Nov 2023 16:45:18 +0800 Subject: [PATCH] fix:fix rule-based router when using RestTemplate. (#1200) --- CHANGELOG.md | 1 + .../polaris/router/config/RouterAutoConfiguration.java | 9 ++++++++- .../config/RpcEnhancementAutoConfiguration.java | 2 -- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27e17ad94..40b157fe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,3 +19,4 @@ - [fix:remove bcprov-jdk15on dependency.](https://github.com/Tencent/spring-cloud-tencent/pull/1177) - [feat:support configuration encryption.](https://github.com/Tencent/spring-cloud-tencent/pull/1181) - [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/1185) +- [fix:fix rule-based router when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/1200) 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 4421f4211..592dfb9da 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 @@ -35,6 +35,7 @@ import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestIntercept import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor; import com.tencent.cloud.polaris.router.resttemplate.RouterLabelRestTemplateInterceptor; import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver; +import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.annotation.Autowired; @@ -117,7 +118,13 @@ public class RouterAutoConfiguration { public SmartInitializingSingleton addRouterLabelInterceptorForRestTemplate(RouterLabelRestTemplateInterceptor interceptor) { return () -> restTemplates.forEach(restTemplate -> { List list = new ArrayList<>(restTemplate.getInterceptors()); - list.add(interceptor); + int addIndex = list.size(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i) instanceof EnhancedRestTemplateInterceptor) { + addIndex = i; + } + } + list.add(addIndex, interceptor); restTemplate.setInterceptors(list); }); } 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 3b52a6ade..3f458676c 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 @@ -52,7 +52,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -176,7 +175,6 @@ public class RpcEnhancementAutoConfiguration { @ConditionalOnClass(name = "org.springframework.web.client.RestTemplate") protected static class PolarisRestTemplateAutoConfiguration { - @LoadBalanced @Autowired(required = false) private List restTemplates = Collections.emptyList();