No instances available for localhost
No instances available for localhost
关键代码如下:
@GetMapping("/article/callHello")
public String callHello() {
return restTemplate.getForObject(
"http://localhost:8083/user/hello", String.class);
}
@Configuration
public class BeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
在配置RestTemplate的时候,如果加了注解LoadBalance的话,就不能直接用localhost了,因为这样没办法知道这个请求会转发到哪个项目,所以会报错,因而只能用项目名称来代替:
@GetMapping("/article/callHello2")
public String callHello2() {
return restTemplate.getForObject(
"http://eureka-client-user-service/user/hello", String.class);
//或者"http://eureka-client-user-service:8083/user/hello"
}
如果想用localhost的话,需要将注解@LoadBalanced注释掉