最近在学习spring cloud,按照网上的教程建立了hello-service服务,但由服务消费者调用时显示No instances available for HELLO-SERVICE但是服务注册已经成功了这是为什么?我直接调用服务IP地址就可以得到返回值服务提供者的controller类和入口类如下:
@RestController
public class HelloController {
    private final Logger logger = Logger.getLogger(getClass());
    @Autowired
    private DiscoveryClient client;    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() {
        List<ServiceInstance> instances = client.getInstances("hello-service");
        for (int i = 0; i < instances.size(); i++) {
            logger.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
        }
        return "Hello World";
    }
}
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}服务消费者的controller类和入口类:
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String helloController() {
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
    }
}
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

解决方案 »

  1.   

    消费端
    eureka的配置对吗?
      

  2.   

    因为你的  @RequestMapping 里的 value 写的 hello
      

  3.   

    之前我也遇到过,是不是和我的一样呢?https://blog.csdn.net/a729913162/article/details/81123078#comments
      

  4.   

    eureka.instance.hostname配置有没有,有的话删除掉再试下
      

  5.   

    我也遇到了,刚解决  在消费端访问服务时写spring.application.name的对外暴露名称就好了
      

  6.   


    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
          #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: jih-manage
        prefer-ip-address: true
      server:
          enable-self-preservation: false
    这是发布者的eureka配置。源代码在https://github.com/qpdyg/jih
      

  7.   

    RestTemplate注入有问题
    新版的需要这样注入:
        @Bean
        @LoadBalanced
        RestOperations restTemplate(RestTemplateBuilder builder) {
            return builder.build();
        }    @Autowired
        RestOperations restTemplate;
      

  8.   

    请问你解决了吗,我和你出现了一样的问题。13楼说12楼是正解,你试了吗,我不知道public RestOperations restTemplate(RestTemplateBuilder builder) {
            return builder.build();
        }
    RestTemplateBuilder 这个参数是什么
      

  9.   

    为什么不用feign老哥
      

  10.   

    https://blog.csdn.net/o_darling/article/details/82147350
      

  11.   

    大佬, 你的版本是什么?我用了你这种方式, 仍然有问题。 我在想会不会是版本的问题?
    SpringBoot版本    2.0.4.RELEASE
    SpringCloud版本    Finchley.SR1
    上面注入方法还可以简写一下:    @Bean
        @LoadBalanced
        public RestTemplate restTemplate(RestTemplateBuilder builder) {
            return builder.build();
        }
      

  12.   

    大佬, 你的版本是什么?我用了你这种方式, 仍然有问题。 我在想会不会是版本的问题?
    SpringBoot版本    2.0.4.RELEASE
    SpringCloud版本    Finchley.SR1
    上面注入方法还可以简写一下:    @Bean
        @LoadBalanced
        public RestTemplate restTemplate(RestTemplateBuilder builder) {
            return builder.build();
        }
    我的问题解决了, 我是缺了eureka的依赖尴尬。 谢谢大佬
      

  13.   

    prefer-ip-address: true
      

  14.   

    大佬,请问你是怎么解决的,缺了什么依赖。我现在eureka使用主机名注册后,无法调用我的服务接口,求解