Skip to content

Commit 3ddfd85

Browse files
herodotus-ecosystemSkyeBeFreeman
authored andcommitted
fix:fix restTemplateCustomizer bean conflict causing service to fail to start properly.
1 parent 0c8dda6 commit 3ddfd85

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

CHANGELOG.md

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

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
<properties>
9292
<!-- Project revision -->
93-
<revision>1.13.0-Hoxton.SR12</revision>
93+
<revision>1.13.1-Hoxton.SR12</revision>
9494

9595
<!-- Spring Framework -->
9696
<spring.framework.version>5.2.25.RELEASE</spring.framework.version>

spring-cloud-tencent-dependencies/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
</developers>
7171

7272
<properties>
73-
<revision>1.13.0-Hoxton.SR12</revision>
73+
<revision>1.13.1-Hoxton.SR12</revision>
7474

7575
<!-- Dependencies -->
7676
<polaris.version>1.15.0</polaris.version>

spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.context.annotation.Bean;
3636
import org.springframework.context.annotation.Configuration;
3737
import org.springframework.http.client.ClientHttpRequestInterceptor;
38+
import org.springframework.util.CollectionUtils;
3839

3940
/**
4041
* Auto-configuration Ribbon for Polaris.
@@ -55,23 +56,38 @@ public PolarisLoadBalancerProperties polarisLoadBalancerProperties() {
5556
}
5657

5758
@Bean
58-
public RestTemplateCustomizer restTemplateCustomizer(
59+
public RestTemplateCustomizer polarisRestTemplateCustomizer(
5960
@Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor,
6061
@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) {
6162
return restTemplate -> {
6263
List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors());
6364
// LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor
64-
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
65-
int addIndex = list.size();
65+
int addIndex = list.size();
66+
if (CollectionUtils.containsInstance(list, retryLoadBalancerInterceptor) || CollectionUtils.containsInstance(list, loadBalancerInterceptor)) {
67+
ClientHttpRequestInterceptor enhancedRestTemplateInterceptor = null;
6668
for (int i = 0; i < list.size(); i++) {
6769
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
70+
enhancedRestTemplateInterceptor = list.get(i);
6871
addIndex = i;
6972
}
7073
}
71-
list.add(addIndex,
72-
retryLoadBalancerInterceptor != null
73-
? retryLoadBalancerInterceptor
74-
: loadBalancerInterceptor);
74+
if (enhancedRestTemplateInterceptor != null) {
75+
list.remove(addIndex);
76+
list.add(enhancedRestTemplateInterceptor);
77+
}
78+
}
79+
else {
80+
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
81+
for (int i = 0; i < list.size(); i++) {
82+
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
83+
addIndex = i;
84+
}
85+
}
86+
list.add(addIndex,
87+
retryLoadBalancerInterceptor != null
88+
? retryLoadBalancerInterceptor
89+
: loadBalancerInterceptor);
90+
}
7591
}
7692
restTemplate.setInterceptors(list);
7793
};

0 commit comments

Comments
 (0)