fix:fix rule-based router when using RestTemplate. (#1201)

pull/1205/head
Haotian Zhang 1 year ago committed by GitHub
parent f186d2ce4f
commit 5c49e44c78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,3 +19,4 @@
- [feat:support configuration encryption.](https://github.com/Tencent/spring-cloud-tencent/pull/1183) - [feat:support configuration encryption.](https://github.com/Tencent/spring-cloud-tencent/pull/1183)
- [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/1187) - [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/1187)
- [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1192) - [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1192)
- [fix:fix rule-based router when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/1201)

@ -36,6 +36,7 @@ import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestIntercept
import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.resttemplate.RouterLabelRestTemplateInterceptor; import com.tencent.cloud.polaris.router.resttemplate.RouterLabelRestTemplateInterceptor;
import com.tencent.cloud.polaris.router.spi.SpringWebRouterLabelResolver; 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.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -125,7 +126,13 @@ public class RouterAutoConfiguration {
public SmartInitializingSingleton addRouterLabelInterceptorForRestTemplate(RouterLabelRestTemplateInterceptor interceptor) { public SmartInitializingSingleton addRouterLabelInterceptorForRestTemplate(RouterLabelRestTemplateInterceptor interceptor) {
return () -> restTemplates.forEach(restTemplate -> { return () -> restTemplates.forEach(restTemplate -> {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors()); List<ClientHttpRequestInterceptor> 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); restTemplate.setInterceptors(list);
}); });
} }

@ -52,7 +52,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -176,7 +175,6 @@ public class RpcEnhancementAutoConfiguration {
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate") @ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
protected static class PolarisRestTemplateAutoConfiguration { protected static class PolarisRestTemplateAutoConfiguration {
@LoadBalanced
@Autowired(required = false) @Autowired(required = false)
private List<RestTemplate> restTemplates = Collections.emptyList(); private List<RestTemplate> restTemplates = Collections.emptyList();

Loading…
Cancel
Save