fix:fix retry loadbalancer not working bug. (#1154)

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

@ -12,3 +12,4 @@
- [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1139) - [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1139)
- [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1146) - [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1146)
- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1148) - [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1148)
- [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1154)

@ -25,12 +25,12 @@ import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterc
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.condition.ConditionalOnMissingBean;
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.ConditionalOnDiscoveryEnabled; import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor; import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer; import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration; import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -52,19 +52,23 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
public class PolarisLoadBalancerAutoConfiguration { public class PolarisLoadBalancerAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean public RestTemplateCustomizer restTemplateCustomizer(
public RestTemplateCustomizer restTemplateCustomizer(@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) { @Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor,
@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> { return restTemplate -> {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors()); List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors());
// LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor // LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor
if (loadBalancerInterceptor != null) { if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
int addIndex = list.size(); int addIndex = list.size();
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) { if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
addIndex = i; addIndex = i;
} }
} }
list.add(addIndex, loadBalancerInterceptor); list.add(addIndex,
retryLoadBalancerInterceptor != null
? retryLoadBalancerInterceptor
: loadBalancerInterceptor);
} }
restTemplate.setInterceptors(list); restTemplate.setInterceptors(list);
}; };

@ -33,6 +33,11 @@
<artifactId>spring-cloud-starter-tencent-polaris-contract</artifactId> <artifactId>spring-cloud-starter-tencent-polaris-contract</artifactId>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.retry</groupId>-->
<!-- <artifactId>spring-retry</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>com.tencent.polaris</groupId>--> <!-- <groupId>com.tencent.polaris</groupId>-->
<!-- <artifactId>connector-consul</artifactId>--> <!-- <artifactId>connector-consul</artifactId>-->

Loading…
Cancel
Save