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

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

@ -10,3 +10,4 @@
- [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1144) - [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1144)
- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1147) - [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1147)
- [fix: dynamic routing using cookies.](https://github.com/Tencent/spring-cloud-tencent/pull/1152) - [fix: dynamic routing using cookies.](https://github.com/Tencent/spring-cloud-tencent/pull/1152)
- [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1157)

@ -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>-->

@ -25,11 +25,11 @@ 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.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.netflix.ribbon.RibbonAutoConfiguration; import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -55,19 +55,23 @@ 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);
}; };

Loading…
Cancel
Save