SpringCloud 服务消费者(rest+ribbon)报错  java.net.UnknownHostException

守給你的承諾、 提交于 2020-01-25 03:01:03

版本信息:springboot 2.0.3 springcloud   Finchley.RELEASE

@Service
public class RibbonService {
    @Autowired
    @Qualifier(value = "restTemplate")
    private RestTemplate restTemplate;
    public String callEurekaClient(String name){
        http://SERVICE-HI/hi?name=
        return restTemplate.getForObject("http://SERVICE-HI/eurekaClient/hi?name=" + name,String.class);
    }
}

本地测试报错,报错信息为: java.net.UnknownHostException service-hi

解决方法: 

  1. 检查springboot和springcloud版本是否匹配。
  2. RestTemplate 需要加 @LoadBalanced
    @Configuration
    public class RibbonConfig {
        @Bean("restTemplate")
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    

     

  3.  host 文件加入以下配置:

    127.0.0.1 localhost
    127.0.0.1 service-hi

     

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!