spring cloud中服务注册成功,但不能通过服务名来调用IP地址

lzw95 lzw95 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近在学习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();
}
}
...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
lzw95
Java EE 67,535 成员 225,843 内容