springcloud-服务发现Discovery

  服务发现是指:服务或其他程序可以去获取注册中心上的注册信息

  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 {}

 

posted @ 2021-01-03 16:25  爱编程DE文兄  阅读(368)  评论(0编辑  收藏  举报