diff --git a/CHANGELOG.md b/CHANGELOG.md index 54e634c85..ab8d197f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,3 +25,4 @@ - [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/534) - [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/540) - [add feature-env plugin & add spring cloud gateway staining plugin](https://github.com/Tencent/spring-cloud-tencent/pull/532) +- [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/544) 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 b2092ff3b..d015a15e7 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 @@ -17,6 +17,7 @@ package com.tencent.cloud.rpc.enhancement.config; +import java.util.Collections; import java.util.List; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; @@ -30,13 +31,14 @@ import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateReport import com.tencent.cloud.rpc.enhancement.resttemplate.PolarisResponseErrorHandler; import com.tencent.polaris.api.core.ConsumerAPI; +import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @@ -100,6 +102,10 @@ public class RpcEnhancementAutoConfiguration { @ConditionalOnClass(name = "org.springframework.web.client.RestTemplate") protected static class PolarisRestTemplateAutoConfiguration { + @LoadBalanced + @Autowired(required = false) + private List restTemplates = Collections.emptyList(); + @Bean public EnhancedRestTemplateReporter polarisRestTemplateResponseErrorHandler( RpcEnhancementReporterProperties properties, ConsumerAPI consumerAPI, @@ -108,8 +114,12 @@ public class RpcEnhancementAutoConfiguration { } @Bean - public RestTemplateCustomizer setErrorHandlerCustomizer(EnhancedRestTemplateReporter reporter) { - return restTemplate -> restTemplate.setErrorHandler(reporter); + public SmartInitializingSingleton setErrorHandlerForRestTemplate(EnhancedRestTemplateReporter reporter) { + return () -> { + for (RestTemplate restTemplate : restTemplates) { + restTemplate.setErrorHandler(reporter); + } + }; } } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java index 35e65dc44..78508dd16 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfigurationTest.java @@ -17,9 +17,6 @@ package com.tencent.cloud.rpc.enhancement.config; -import java.io.IOException; -import java.net.URI; - import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignBeanPostProcessor; import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignPluginRunner; @@ -32,12 +29,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalanced; -import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration; -import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; -import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest; -import org.springframework.cloud.client.loadbalancer.Request; import org.springframework.cloud.openfeign.loadbalancer.FeignLoadBalancerAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -57,7 +49,6 @@ public class RpcEnhancementAutoConfigurationTest { PolarisContextAutoConfiguration.class, RpcEnhancementAutoConfiguration.class, PolarisRestTemplateAutoConfigurationTester.class, - LoadBalancerAutoConfiguration.class, FeignLoadBalancerAutoConfiguration.class)) .withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true"); @@ -85,36 +76,5 @@ public class RpcEnhancementAutoConfigurationTest { RestTemplate restTemplate() { return new RestTemplate(); } - - @Bean - LoadBalancerClient loadBalancerClient() { - return new LoadBalancerClient() { - - @Override - public ServiceInstance choose(String serviceId) { - return null; - } - - @Override - public ServiceInstance choose(String serviceId, Request request) { - return null; - } - - @Override - public T execute(String serviceId, LoadBalancerRequest request) throws IOException { - return null; - } - - @Override - public T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest request) throws IOException { - return null; - } - - @Override - public URI reconstructURI(ServiceInstance instance, URI original) { - return null; - } - }; - } } }