SpringCloud集成组件Ribbon 报错:java.lang.IllegalStateException: No instances available for localhost

1. 报错信息

java.lang.IllegalStateException: No instances available for localhost

2. 问题重现


服务提供者向外暴露接口,我们可以访问。但是ribbon调用服务提供者的接口时,报错!!

@RestController
@RequestMapping("ribbon")
public class RibbonController {
   

    @Autowired
    private RestTemplate restTemplate;

    //查询所有
    @GetMapping("findAll")
    public Collection<Student> findAll(){
   
        //url指向服务提供者,这里的restTemplate类似于一个服务消费者
        return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
}

3. 问题原因

这边是因为我们ribbon去调用服务提供者的服务时,接口用的不再是简单的服务提供者的接口,而是用的服务提供者的应用服务名称!!

4. 解决方案

ribbon的web层调用服务提供者localhost:8010/student/findAll而是provider/student/findAll需要,这个名称的配置在各自服务的配置文件中!

#服务提供者的配置
server:
  port: 8010
spring:
  application:
      name: provider
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
#ribbon的配置
server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

改后的代码:

    @GetMapping("findAll")
    public Collection<Student> findAll(){
   
        //url指向服务提供者,这里的restTemplate类似于一个服务消费者
        return restTemplate.getForEntity("http://provider/student/findAll",Collection.class).getBody();
    }

重新访问即可!

感谢老哥博客:https://blog.csdn.net/Echouuu/article/details/104292845/

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-20 20:30
工作没了,落户没了,什么都没了
梦想是成为七海千秋:是因为什么原因呀,如果是因为导师恶意卡你就和他爆了
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
秋盈丶:后续:我在宿舍群里和大学同学分享了这事儿,我好兄弟气不过把他挂到某脉上了,10w+阅读量几百条评论,直接干成精品贴子,爽
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务