@Autowired
RestTemplate restTemplate;
/**
* @LoadBalanced需要注释掉,因为 restTemplate.getForObject(url,String.class);
* 会把ip转化成hostName报错
*/
//@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Autowired
LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String getTest(){
ServiceInstance serviceInstance = loadBalancerClient.choose("spring-cloud-provider-serverice");
String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()+"/hello");
return restTemplate.getForObject(url,String.class);
}