springcloud——通过程序来发现注册中心有哪些服务

1、在控制器添加的程序代码

 //服务发现程序
    @GetMapping("/payment/discovery")
    public Object discovery() {

        //根据名称来获取注册中心有哪几种不同名称的服务
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("*******service : " + service);
        }

        //根据特定名称获取注册中心有哪几个集群的服务
        List<ServiceInstance> instances = discoveryClient.getInstances("PAYMENT-PROVIDER");
        for (ServiceInstance instance : instances) {
            log.info(instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort()
            + "\t" + instance.getUri());
        }

        return this.discoveryClient;
    }

2、在启动类额外添加的注解

@EnableDiscoveryClient      //开启服务返现客户端

猜你喜欢

转载自blog.csdn.net/weixin_43925059/article/details/107562186