Set error handler named EnhancedRestTemplateReporter for RestTemplate (#543)

pull/547/head
DerekYRC 2 years ago committed by GitHub
parent cbbbf1f4a7
commit b0ff775e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,3 +18,4 @@
- [feat:optimize pushgateway plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/530) - [feat:optimize pushgateway plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/530)
- [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/535) - [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/535)
- [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/541) - [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/541)
- [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/543)

@ -17,6 +17,7 @@
package com.tencent.cloud.rpc.enhancement.config; package com.tencent.cloud.rpc.enhancement.config;
import java.util.Collections;
import java.util.List; import java.util.List;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
@ -29,13 +30,14 @@ import com.tencent.cloud.rpc.enhancement.feign.plugin.reporter.SuccessPolarisRep
import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateReporter; import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateReporter;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -99,6 +101,10 @@ 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)
private List<RestTemplate> restTemplates = Collections.emptyList();
@Bean @Bean
public EnhancedRestTemplateReporter polarisRestTemplateResponseErrorHandler( public EnhancedRestTemplateReporter polarisRestTemplateResponseErrorHandler(
RpcEnhancementReporterProperties properties, RpcEnhancementReporterProperties properties,
@ -107,8 +113,12 @@ public class RpcEnhancementAutoConfiguration {
} }
@Bean @Bean
public RestTemplateCustomizer setErrorHandlerCustomizer(EnhancedRestTemplateReporter reporter) { public SmartInitializingSingleton setErrorHandlerForRestTemplate(EnhancedRestTemplateReporter reporter) {
return restTemplate -> restTemplate.setErrorHandler(reporter); return () -> {
for (RestTemplate restTemplate : restTemplates) {
restTemplate.setErrorHandler(reporter);
}
};
} }
} }
} }

@ -17,9 +17,6 @@
package com.tencent.cloud.rpc.enhancement.config; 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.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignBeanPostProcessor; import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignBeanPostProcessor;
import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignPluginRunner; import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignPluginRunner;
@ -32,11 +29,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; 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.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.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -54,8 +47,7 @@ public class RpcEnhancementAutoConfigurationTest {
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class, PolarisContextAutoConfiguration.class,
RpcEnhancementAutoConfiguration.class, RpcEnhancementAutoConfiguration.class,
PolarisRestTemplateAutoConfigurationTester.class, PolarisRestTemplateAutoConfigurationTester.class))
LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true"); .withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");
@Test @Test
@ -82,30 +74,5 @@ public class RpcEnhancementAutoConfigurationTest {
RestTemplate restTemplate() { RestTemplate restTemplate() {
return new RestTemplate(); return new RestTemplate();
} }
@Bean
LoadBalancerClient loadBalancerClient() {
return new LoadBalancerClient() {
@Override
public ServiceInstance choose(String serviceId) {
return null;
}
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
return null;
}
@Override
public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException {
return null;
}
@Override
public URI reconstructURI(ServiceInstance instance, URI original) {
return null;
}
};
}
} }
} }

Loading…
Cancel
Save