Skip to content

Commit ca37ff3

Browse files
herodotus-ecosystemSkyeBeFreeman
authored andcommitted
fix:fix restTemplateCustomizer bean conflict causing service to fail to start properly.
1 parent 8e1090a commit ca37ff3

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
@@ -23,3 +23,4 @@
2323
- [fix:fix sct-all wrong spring boot version obtain.](https://github.com/Tencent/spring-cloud-tencent/pull/1203)
2424
- [fix:fix reporter wrong initialization when using config data.](https://github.com/Tencent/spring-cloud-tencent/pull/1221)
2525
- [fix:fix swagger not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1224)
26+
- fix:fix restTemplateCustomizer bean conflict causing service to fail to start properly.

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
<properties>
9191
<!-- Project revision -->
92-
<revision>1.13.0-2021.0.9</revision>
92+
<revision>1.13.1-2021.0.9</revision>
9393

9494
<!-- Spring Framework -->
9595
<spring.framework.version>5.3.31</spring.framework.version>

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

+23-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.context.annotation.Bean;
3737
import org.springframework.context.annotation.Configuration;
3838
import org.springframework.http.client.ClientHttpRequestInterceptor;
39+
import org.springframework.util.CollectionUtils;
3940

4041
/**
4142
* Auto-configuration of loadbalancer for Polaris.
@@ -52,23 +53,38 @@
5253
public class PolarisLoadBalancerAutoConfiguration {
5354

5455
@Bean
55-
public RestTemplateCustomizer restTemplateCustomizer(
56+
public RestTemplateCustomizer polarisRestTemplateCustomizer(
5657
@Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor,
5758
@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) {
5859
return restTemplate -> {
5960
List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors());
6061
// LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor
61-
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
62-
int addIndex = list.size();
62+
int addIndex = list.size();
63+
if (CollectionUtils.containsInstance(list, retryLoadBalancerInterceptor) || CollectionUtils.containsInstance(list, loadBalancerInterceptor)) {
64+
ClientHttpRequestInterceptor enhancedRestTemplateInterceptor = null;
6365
for (int i = 0; i < list.size(); i++) {
6466
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
67+
enhancedRestTemplateInterceptor = list.get(i);
6568
addIndex = i;
6669
}
6770
}
68-
list.add(addIndex,
69-
retryLoadBalancerInterceptor != null
70-
? retryLoadBalancerInterceptor
71-
: loadBalancerInterceptor);
71+
if (enhancedRestTemplateInterceptor != null) {
72+
list.remove(addIndex);
73+
list.add(enhancedRestTemplateInterceptor);
74+
}
75+
}
76+
else {
77+
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
78+
for (int i = 0; i < list.size(); i++) {
79+
if (list.get(i) instanceof EnhancedRestTemplateInterceptor) {
80+
addIndex = i;
81+
}
82+
}
83+
list.add(addIndex,
84+
retryLoadBalancerInterceptor != null
85+
? retryLoadBalancerInterceptor
86+
: loadBalancerInterceptor);
87+
}
7288
}
7389
restTemplate.setInterceptors(list);
7490
};

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-2021.0.9</revision>
73+
<revision>1.13.1-2021.0.9</revision>
7474

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

0 commit comments

Comments
 (0)