Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestTemplate中如果使用到了spring-retry则使用loadbalancer无效bug #1138

Closed
Ive4 opened this issue Sep 24, 2023 · 0 comments · Fixed by #1154, #1155, #1157 or #1156
Closed

RestTemplate中如果使用到了spring-retry则使用loadbalancer无效bug #1138

Ive4 opened this issue Sep 24, 2023 · 0 comments · Fixed by #1154, #1155, #1157 or #1156
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Ive4
Copy link

Ive4 commented Sep 24, 2023

Describe the bug
如果依赖中有spring-retry则会无法正常初始化@LoadBalanced的RestTemplate,解决办法如下:
@bean
public RestTemplateCustomizer restTemplateCustomizer(
@Autowired(required = false) RetryLoadBalancerInterceptor retryLoadBalancerInterceptor,
@Autowired(required = false) LoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List list = new ArrayList<>(restTemplate.getInterceptors());
// LoadBalancerInterceptor must invoke before EnhancedRestTemplateInterceptor
if (retryLoadBalancerInterceptor != null || loadBalancerInterceptor != null) {
int addIndex = list.size();
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);
};
}

To Reproduce
Steps to reproduce the behavior.

Expected behavior
A clear and concise description of what you expected to happen.

Environment

  • Version: [1.12.0-2022.0.4]
  • OS: [e.g. CentOS7]

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment