fix:fix restTemplateCustomizer bean conflict causing service to fail to start properly.

pull/1239/head
码匠君 10 months ago committed by Haotian Zhang
parent cb57c3b809
commit 44426a816e

@ -36,6 +36,7 @@ import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfigurati
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.CollectionUtils;
/**
* Auto-configuration of loadbalancer for Polaris.
@ -52,14 +53,28 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
public class PolarisLoadBalancerAutoConfiguration {
@Bean
public RestTemplateCustomizer restTemplateCustomizer(
public RestTemplateCustomizer polarisRestTemplateCustomizer(
@Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor,
@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors());
// LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
int addIndex = list.size();
if (CollectionUtils.containsInstance(list, retryLoadBalancerInterceptor) || CollectionUtils.containsInstance(list, loadBalancerInterceptor)) {
ClientHttpRequestInterceptor enhancedRestTemplateInterceptor = null;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
enhancedRestTemplateInterceptor = list.get(i);
addIndex = i;
}
}
if (enhancedRestTemplateInterceptor != null) {
list.remove(addIndex);
list.add(enhancedRestTemplateInterceptor);
}
}
else {
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
addIndex = i;
@ -70,6 +85,7 @@ public class PolarisLoadBalancerAutoConfiguration {
? retryLoadBalancerInterceptor
: loadBalancerInterceptor);
}
}
restTemplate.setInterceptors(list);
};
}

Loading…
Cancel
Save