微服务是目前非常流行和实用的软件架构设计。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*/@DeprecatedServiceInstance 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();

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

  1. Spring Cloud H (二)服务注册中心 Zookeeper

    目录 前言 一.Zookeeper数据结构 二.统一配置管理 三.统一命名服务 四.Zookeeper安装启动 五.zoo.conf参数解读 六.集群搭建 七.Zookeeper Session 为什 ...

  2. Spring Cloud【Finchley】实战-01注册中心及商品微服务

    文章目录 Spring Cloud[Finchley]专栏 概述 版本说明 搭建Eureka Server注册中心 工程结构 Step1. pom添加依赖 Step2.application.yml ...

  3. SpringCloud - Spring Cloud Alibaba 之 Nacos Discovery服务注册发现(三)

    阅读本文可先参考博文 https://blog.csdn.net/MinggeQingchun/article/details/125613600 https://blog.csdn.net/Ming ...

  4. 微服务实战——Spring Cloud 第四篇 将服务注册到Eureka Server上

    为什么80%的码农都做不了架构师?>>>    将服务注册到Eureka上是一件非常简单的事情,只要以下两步,就可以将一个微服务注册到Eureka Server上. 1. 首先添加E ...

  5. consul作为服务注册中心

    consul consul 下载地址 provider order consul 由go语言编写的一款优秀的服务注册中心应用. https://www.consul.io/intro/index.ht ...

  6. Spring Cloud 微服务实战系列-Eureka注册中心(一)

    导语   在这一个系列的分享中,笔者主要是来分享在实战实际操作中的一些使用场景,了解了实战的操作中的使用场景才能更好的从实战中的问题出发找到自己需要的原理性的东西,找到原理性的东西之后才可以更好的根据 ...

  7. Spring Cloud 微服务实战系列-Eureka注册中心(二)

    导语   之前的分享中,简单的介绍了SpringBoot的入门知识以及如何使用Eureka搭建服务注册中心,这一次的分享主要是来讲解一些在Eureka中的常用的配置,方便大家在使用Eureka的时候可 ...

  8. 『注册中心』Consul微服务注册中心的使用及相关集群搭建

    Consul目录 一.概念篇--注册中心 1. 什么是注册中心 2. 为什么要使用注册中心 3. 注册中心类型 4. 注册中心的优点 二.概念篇--Consul 1. 什么是Consul 2. Con ...

  9. Spring Cloud第十四篇: 服务注册(consul)

    这篇文章主要介绍 spring cloud consul 组件,它是一个提供服务发现和配置的工具.consul具有分布式.高可用.高扩展性. 一.consul 简介 consul 具有以下性质: 服务 ...

最新文章

  1. [转]关于MyEclipse下的项目无法使用BASE64Encoder问题的解决办法
  2. linux项目课程设计,LINUX课程设计项目需求解析.doc
  3. sklearn.feature_extraction.text.CountVectorizer 学习
  4. 1033 To Fill or Not to Fill (25 分)【难度: 难 / 知识点: 模拟 贪心】
  5. Android中集成Jpush实现推送消息通知与根据别名指定推送附示例代码下载
  6. centos 6.7 ssh免密登录配置
  7. mysql的FIQ怎么安装_MySQL数据库设计总结
  8. 看博客学学Android(五)
  9. Python_Socket实现简单的ssh/ftp
  10. 程序代码移植和烧录需要注意什么_牙齿矫正需要注意什么?
  11. vc red.msi matlab,vc red.msi x64+x32位版下载
  12. Java代码审查工具 FindBugs下载、安装和使用(无需集成环境一键安装使用)
  13. 谷歌广告联盟怎么收款?google AdSense 广告款收取流程!(转载)
  14. Windows 下使用 TFTPD32+HTTP PXE引导安装linux
  15. Java代码实现使用while循环接受键盘的输入,如果输入的exit就退出,否则提示用户继续输入
  16. 包装类------拆箱与装箱
  17. [创业-39]:中小公司的组织架构与公司管理
  18. STM32学习(二)
  19. MQ2烟雾传感器模块——stm32f103
  20. rnnlm源码分析(六)

热门文章

  1. 微信 沙箱验证签名失败 请确认沙箱签名key是否正确 解决办法
  2. 人体骨骼关键点检测AP值AP@s
  3. go依赖注入--google开源库wire
  4. CSS样式笔记_背景文本字体链接
  5. 揭秘时空大数据:详细介绍、真实应用场景和数据示例解析
  6. 原命题组组长王式安谈数学命题规律
  7. 学人工智能?你真的想好了吗?
  8. 控制台五子棋java_JAVA控制台五子棋的问题
  9. AMASLAB-EPIC-KBS工控机Ubuntu基础环境配置
  10. DeepWalk模型的简介与优缺点