Spring Cloud中使用Consul作爲服務註冊中心時如何獲得local service id?

微服務是目前非常流行的和實用的軟件架構設計。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, 效果

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章