服务发现是指:服务或其他程序可以去获取注册中心上的注册信息
eureka提供了实现服务发现的API,具体操作如下:
@Autowired private DiscoveryClient discoveryClient;
@GetMapping("/payment/getServices") public void getServicesInfo(){ //获取多个服务名 List<String> services = discoveryClient.getServices(); for (String service: services) { log.info("服务名:"+service); } //指定一个服务,得到多个该服务的实例 List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service"); for (ServiceInstance instance: instances) { log.info("主机名:"+instance.getHost()+ "\t"+"实例ID"+instance.getInstanceId()+"\t"+"服务ID"+instance.getServiceId()+"\t"+ "实例端口:"+instance.getPort()+"\t"+"实例URI"+instance.getUri()); } }
想要获取注册中心的注册信息,可以这个DidscoveryClient这个API去操作
等下,还没完,这个API是需要加入到容器中的,所以我们得加一个注解,目的之一就是让他加入到IOC中
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //启动发现客户端 public class PaymentApplication {}
原文地址:https://www.cnblogs.com/ibcdwx/p/14225899.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。