Spring Cloud DiscoveryClient。不推荐使用getLocalServiceInstance(),如何使用注册?

时间:2017-07-13 15:57:59

标签: java spring microservices spring-cloud auto-registration

我需要获取正在运行的微服务的当前实例ID,问题是我要求如果进程未注册"随机生成"必须提供实例ID。

我试图从DiscoveryClient获取服务实例ID,但是代码指出getLocalServiceInstance已被弃用,我无法使用它。

我尝试使用javadoc中所述的Registration,但是coudn没有找到初始化它的方法。

是否有传统/特定的方式来获得服务自己的注册?

顺便说一句,我不能使用直接实现,因为它是一个启动程序,不知道DiscoveryService实现在运行时将可用。

/**
 * @deprecated use the {@link org.springframework.cloud.client.serviceregistry.Registration} bean instead
 *
 * @return ServiceInstance with information used to register the local service
 */
@Deprecated
ServiceInstance getLocalServiceInstance();

1 个答案:

答案 0 :(得分:0)

代码

discoveryClient.getServices().forEach(id -> {
            discoveryClient.getInstances(id).forEach(instance -> {
                logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
            });
        });