spring cloud中服务注册成功,但不能通过服务名来调用IP地址
最近在学习spring cloud,按照网上的教程建立了hello-service服务,但由服务消费者调用时显示No instances available for HELLO-SERVICE

但是服务注册已经成功了

这是为什么?我直接调用服务IP地址就可以得到返回值

服务提供者的controller类和入口类如下:
服务消费者的controller类和入口类:

但是服务注册已经成功了

这是为什么?我直接调用服务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();
}
}
...全文
相关推荐
spring cloud consul使用ip注册服务的方法示例
主要介绍了spring cloud consul使用ip注册服务的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring cloud eureka微服务之间的调用详解
主要介绍了spring cloud eureka微服务之间的调用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring cloud consul注册的服务报错critical的解决
主要介绍了spring cloud consul注册的服务报错critical的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring cloud将spring boot服务注册到Eureka Server上的方法
本篇文章主要介绍了spring cloud将spring boot服务注册到Eureka Server上的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
spring cloud nacos 注册中心
spring cloud nacos的可视化界面,微服务可以注册进nacos,支持CP,AP两种方式。


