使用带有 Load Balancer 功能的 RestTemplate 调用服务
1. 定义 RestTemplate Bean
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}2. 使用上述 Bean
private RestTemplate restTemplate;
private Organization getOrganization(String organizationId) {
Organization organization = restTemplate.getForObject(
"http://{applicationId}/v1/organization/{organizationId}",
Organization.class,
"organization-service",
organizationId);
return organization;
}
@Autowired
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}Last updated