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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM