微服务是目前非常流行的和实用的软件架构设计。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移除了。
EurekaInstanceConfig.png
2, 解决办法
通过查看Spring Cloud最新版本的文档我们发现。在ServiceInstance getLocalServiceInstance();方法这,有一段文字。
org.springframework.cloud.client.serviceregistry.Registration
/*
* 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 getInstances(String serviceId);
/**
* @return all known service ids
*/
List 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
start-consul.png
4, 效果
consul_queryLocalServiceID.png