java.lang.IllegalStateException: No instances available for localhost

spring cloud 注册中心eureka学习中碰到两个问题记录一下。

第一个问题是 配置文件 application.yml

第二个问题就是文章标题错误。

是因为使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如果要调用真实的域名或者ip的url,会有错误,如下:

String result = rest.getForObject("http://localhost:9090/getMyMember", String.class);

会出现的错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for www.baidu.com

查看错误的跟踪链发现,自动注入的restTemplate中加入了cloud.netflix*包下面的interceptor,所以默认会通过RibbonLoadBalancerClient去查找注册中心的instances,如上面的代码,www.baidu.com肯定不存在,所以就报错了。

找了半天,解决方案是,自己自定义或者产生一个resttemplate即可,不使用自动配置的。

@Bean(name="remoteRestTemplate")
public RestTemplate restTemplate() {
    return new RestTemplate();
}
@Autowired
@Qualifier(value = "remoteRestTemplate")
private RestTemplate rest;

搞定。记录一下,非常感谢LoWang,参考: https://www.jianshu.com/p/0db3570e4674

扫描二维码关注公众号,回复: 6146769 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_39930369/article/details/87616077