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

pull/1390/merge 1.13.1-Hoxton.SR12
码匠君 9 months ago committed by Haotian Zhang
parent 0c8dda6285
commit 3ddfd85d4f

@ -20,3 +20,4 @@
- [feat:optimize examples.](https://github.com/Tencent/spring-cloud-tencent/pull/1186)
- [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1191)
- [fix:fix sct-all wrong spring boot version obtain.](https://github.com/Tencent/spring-cloud-tencent/pull/1204)
- fix:fix restTemplateCustomizer bean conflict causing service to fail to start properly.

@ -90,7 +90,7 @@
<properties>
<!-- Project revision -->
<revision>1.13.0-Hoxton.SR12</revision>
<revision>1.13.1-Hoxton.SR12</revision>
<!-- Spring Framework -->
<spring.framework.version>5.2.25.RELEASE</spring.framework.version>

@ -70,7 +70,7 @@
</developers>
<properties>
<revision>1.13.0-Hoxton.SR12</revision>
<revision>1.13.1-Hoxton.SR12</revision>
<!-- Dependencies -->
<polaris.version>1.15.0</polaris.version>

@ -35,6 +35,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClients;
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 Ribbon for Polaris.
@ -55,23 +56,38 @@ 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();
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;
}
}
list.add(addIndex,
retryLoadBalancerInterceptor != null
? retryLoadBalancerInterceptor
: loadBalancerInterceptor);
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;
}
}
list.add(addIndex,
retryLoadBalancerInterceptor != null
? retryLoadBalancerInterceptor
: loadBalancerInterceptor);
}
}
restTemplate.setInterceptors(list);
};

Loading…
Cancel
Save