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注釋掉