Edn*_*dna 3 cloud spring resttemplate netflix-ribbon
我正在使用 spring cloud: Spring Boot Application with Eureka + Ribbon 默认配置。
我正在使用 2 种RestTemplate配置,它们都是@LoadBalanced当前的,并且它们都具有相同的UriTemplateHandler.
我同时声明@SpringBootApplication了@RibbonClient(name="${service.name}")注释和注释。
我的问题是:
当我试图访问的第一配置RestTemplate中,RestTemplateresolvs(由尤里卡和负载由带状平衡)到服务器,而不是我要求作为配置UriTemplateHandler。
例如:在UriTemplateHandler我配置的“A-Service”并实时restTemplate发送httpRequest到“B-Service”这种行为经常发生,不仅仅是针对特定请求,但它看起来只在我访问首先配置RestTemplate。
使用具有相同 uri 的 2 RestTemplate 是否有问题?
我不知道为什么会这样,请指教。
小智 8
将这些 rest 模板创建为 bean 时,将它们命名为唯一,例如
@LoadBalanced
@Bean("integrationRestTemplate")
public RestTemplate restTemplate() throws Exception {
// build and return your rest template
return ....
}
Run Code Online (Sandbox Code Playgroud)
然后,另一个可能没有任何特定名称,例如
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Run Code Online (Sandbox Code Playgroud)
现在,如果你有这两个不同的休息模板,你可以像这样注入前一个:
@Service
public class MyService {
private final RestTemplate restTemplate;
public ApplicantService(@Qualifier("integrationRestTemplate") RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
// service methods
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,关键是您可以通过指定@Qualifier 来选择您想要的任何休息模板。
| 归档时间: |
|
| 查看次数: |
5807 次 |
| 最近记录: |