Spring 弹簧丝带@LoadBalanced';t为restTemplate使用@scope(“原型”)

Spring 弹簧丝带@LoadBalanced';t为restTemplate使用@scope(“原型”),spring,resttemplate,netflix-ribbon,Spring,Resttemplate,Netflix Ribbon,我在跟踪,我注意到一件事 如果以这种方式创建rest模板: @LoadBalanced @Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) ---- I added this line RestTemplate restTemplate(){ logger.warn("create a restTemplate bean..."); --- added this for debug return new RestTempl

我在跟踪,我注意到一件事

如果以这种方式创建
rest模板

@LoadBalanced
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)  ---- I added this line
RestTemplate restTemplate(){
    logger.warn("create a restTemplate bean..."); --- added this for debug
    return new RestTemplate();
}
然后,功能区客户端将无法找到服务器。(无法解析具有服务器IP地址的客户端名称)

我调试了bean创建过程,注意到日志中出现了两次消息“CreatearestTemplateBean…”

自动连线restTemplate实例没有
LoadBalancerInterceptor
,这可能就是它失败的原因

因此,我想知道是否有任何建议来解决这个问题

备注:我需要将
restemplate
作为原型,这样我就可以设置不同的错误处理程序。

如下面提到的:

事实证明,
@LoadBalanced
注释不会从应用程序上下文中检索
restemplate
bean,在“prototype”范围内,它只会检索一个新实例,任何自动连接字段都不会使用该实例


它确实失败了,因为缺少LoadBalancerInterceptor,但是可以简单地注入该类。 注入它,并移除@LoadBalanced注释:

java
@豆子
@范围(“原型”)
RestTemplate loadBalancedTemplate(LoadBalancerInterceptor LoadBalancerInterceptor){
RestTemplate=新的RestTemplate();
template.getInterceptors().add(loadBalancerInterceptor);
返回模板;
}