Spring Cloud中使用Consul作为服务注册中心时如何获得local service id?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/russle/article/details/81806472

微服务是目前非常流行和实用的软件架构设计。Spring Cloud是java开发领域最受欢迎也是常用的微服务框架。Spring Cloud Finchley版本已经发布,与此同时Eureka 2.0的开源开发工作也停止了。因此很多项目开始转向使用Consul作为服务注册中心(关于如何使用consul不在本文讨论范围)。 那么之前我们使用EurekaInstanceConfig获取了服务自身信息的方式无法继续。而DiscoveryClient虽然在低版本的Spring Cloud中提供了ServiceInstance getLocalServiceInstance();方法,但是被标记为 @Deprecated. 当我们升级到Spring Cloud Finchley版本时, getLocalServiceInstance方法被从DiscoveryClient移除了,那如何获得服务自身的ServiceInstance信息?

  • 1, 问题

使用Consul作为微服务注册中心,无法使用EurekaInstanceConfig。 而Spring Cloud中提供的DiscoveryClient类,虽然有
ServiceInstance getLocalServiceInstance();方法,但是被标记为@Deprecated. 更为严重的是, Spring Cloud的Finchley版本中,getLocalServiceInstance方法从DiscoveryClient移除了。

这里写图片描述

  • 2, 解决办法

通过查看Spring Cloud最新版本的文档我们发现。在ServiceInstance getLocalServiceInstance();方法这,有一段文字。
* @deprecated use the {@link org.springframework.cloud.client.serviceregistry.Registration} bean instead
*

/*
 * Copyright 2013-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.client.discovery;

import java.util.List;

import org.springframework.cloud.client.ServiceInstance;

/**
 * DiscoveryClient represents read operations commonly available to Discovery service such as
 * Netflix Eureka or consul.io
 * @author Spencer Gibb
 */
public interface DiscoveryClient {

    /**
     * A human readable description of the implementation, used in HealthIndicator
     * @return the description
     */
    String description();

    /**
     * @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();

    /**
     * Get all ServiceInstances associated with a particular serviceId
     * @param serviceId the serviceId to query
     * @return a List of ServiceInstance
     */
    List<ServiceInstance> getInstances(String serviceId);

    /**
     * @return all known service ids
     */
    List<String> getServices();

}
  • 3, 实践

我们的项目很简单,启动一个consul,注册一个user-service,user-service提供了一个rest查询自己的serviceId。
关于consul的按照和启动,本文不再赘述(我在Windows10上下载consul,然后直接启动)。 user-service很简单。**代码在这里。

    @ApiOperation(value = "查询自身的服务id")
    @GetMapping(value = "/info/local", produces = "application/json;charset=UTF-8")
    public String getInfoLocal() {
        JSONObject jsonTemp = new JSONObject();

        jsonTemp.put("ServiceId", registration.getServiceId());
        jsonTemp.put("ServiceUri", registration.getUri());
        jsonTemp.put("ServiceHost", registration.getHost());
        jsonTemp.put("ServiceSchema", registration.getScheme());
        jsonTemp.put("ServicePort", registration.getPort());
        jsonTemp.put("ServiceMetadata", registration.getMetadata());
        return jsonTemp.toJSONString();
    }

启动consul
这里写图片描述

  • 4, 效果

这里写图片描述

重点,也可以直接使用ConsulRegistration, 使用可以获得instanceId String currentInstId = registration.getInstanceId();

猜你喜欢

转载自blog.csdn.net/russle/article/details/81806472