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, 效果
重點,也可以直接使用ConsulRegistration, 使用可以獲得instanceId String currentInstId = registration.getInstanceId();