微服务注册中心:Consul——服务注册
系列文章:
微服务架构:网关概念与 zuul
微服务网关:Spring Cloud Gateway —— Zuul
微服务网关:Spring Cloud Config- 配置中心
微服务网关方案:Kong & Nacos
Nacos 实践
微服务网关:Nacos 源码实践(二)
微服务注册中心:Consul——概念与基础操作
一 简介
微服务注册中心:Consul——概念与基础操作介绍了consul的安装和基本操作,本篇开始在consul上进行服务注册与发现,语言使用Java,框架使用Spring Boot整合Consul。
二 Spring Boot整合Consul
通常demo比较好找,导入后观察需要引入哪些依赖,然后启动。最多微调版本和配置问题即可。
2.1 网上demo
打脸来的如此迅速,百度上搜了一堆,最后发现都是官方文档的翻译,版本不清,代码不全,尝试几个未果。再次感叹现在很多文章的质量。
2.2 官方文档
不得已还是先尝试查看官方文档,Spring Cloud Consul是基于3.0.2版本,给出的集成demo。不过很遗憾,官方给出的sample地址,访问一直是404状态。
上面Consul Sample的查看结果:
2.3 spring-cloud的github
考虑下一种渠道,寻找官方github。官方的pom.xml配置建议如下:
<project>
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>{spring-boot-version}</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
事实上,springboot的版本选择过程中也遇到不少问题,几个典型的错误如下(为了节省空间,只截取了关键错误信息):
2.3.1 启动报错信息1
Description:An attempt was made to call a method that does not exist. The attempt was made from the following location:org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator.<init>(DiscoveryCompositeHealthIndicator.java:41)The following method did not exist:org.springframework.boot.actuate.health.CompositeHealthIndicator.<init>(Lorg/springframework/boot/actuate/health/HealthAggregator;)VThe method's class, org.springframework.boot.actuate.health.CompositeHealthIndicator, is available from the following locations:jar:file:/Users/lijingyong/.m2/repository/org/springframework/boot/spring-boot-actuator/2.2.6.RELEASE/spring-boot-actuator-2.2.6.RELEASE.jar!/org/springframework/boot/actuate/health/CompositeHealthIndicator.classIt was loaded from the following location:file:/Users/lijingyong/.m2/repository/org/springframework/boot/spring-boot-actuator/2.2.6.RELEASE/spring-boot-actuator-2.2.6.RELEASE.jarAction:Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.actuate.health.CompositeHealthIndicatorProcess finished with exit code 1
2.3.2 报错2
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: nullat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]... 96 common frames omitted
Caused by: java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: nullat org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.normalizeForDns(ConsulAutoRegistration.java:178) ~[spring-cloud-consul-discovery-2.2.1.RELEASE.jar:2.2.1.RELEASE]
2.4 一个可用demo
根据官方说明,并参考另一个demo:https://github.com/RobbieXie/springboot-consul 后,整理如下,不过是使用的较旧版本springboot,可以根据需要更新到适合的版本信息。
关键配置和代码:
2.4.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>springboot-consul</artifactId><description>Spring Boot整合consul示例</description><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.orbitz.consul</groupId><artifactId>consul-client</artifactId><version>1.0.0</version></dependency><dependency><groupId>com.ecwid.consul</groupId><artifactId>consul-api</artifactId><version>1.3.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><snapshots><enabled>false</enabled></snapshots><id>central</id><name>bintray</name><url>http://jcenter.bintray.com</url></repository></repositories>
</project>
2.4.2 应用启动类 SpringBootConsulApplication
package com.flamingskys.learn.springboot.consul;import com.flamingskys.learn.springboot.consul.service.ConsulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;import javax.annotation.PreDestroy;@SpringBootApplication
@EnableAutoConfiguration
public class SpringBootConsulApplication {@Value("${consul.instanceId}")private String instanceId;@Beanpublic String getInstanceId(){return instanceId;}@Autowiredprivate ConsulService consulService;public static void main(String[] args) {SpringApplication.run(SpringBootConsulApplication.class, args);}@PreDestroypublic void beforeExit(){System.out.println("-----------------------------BEFORE EXIT--------------------------------");consulService.deregisterService(instanceId);}
}
2.4.3 资源配置application.properties
spring.application.name=first-consul-clientconsul.host=127.0.0.1
consul.port=8500
consul.instanceId=${spring.application.name}:${spring.application.instanceid:${random.value}}spring.cloud.consul.discovery.heartbeat.enabled=true
应用启动后,查看console上注册的服务列表,first-consul-client就是我们的服务:
完整代码已上传到了gitee。可关注公众号:程序员架构进阶 随时获取更新。
微服务注册中心:Consul——服务注册相关推荐
- 分布式服务发现与注册中心 Consul 中文入门指南
公众号关注 「奇妙的 Linux 世界」 设为「星标」,每天带你玩转 Linux ! 基础概念 什么是注册中心 随着微服务理论发展的成熟,越来越多互联网公司采用微服务架构来支持业务发展.各个微服务之间 ...
- 服务注册中心-Consul
Consul官方文档 官方文档:https://www.consul.io/docs/intro 中文文档:https://www.springcloud.cc/spring-cloud-co ...
- 微服务架构-服务注册中心和服务网关(6.8) (转载)
原文链接:微服务架构-服务注册中心和服务网关(6.8) 这篇文章还是基于SpringCloud开源框架体系来谈下对Eureka服务注册中心和Zuul服务网关在使用上的一些理解和说明.在使用微服务架构进 ...
- java 服务注册中心_服务治理的含义和java最流行的微服务框架服务治理注册中心的搭建...
原标题:服务治理的含义和java最流行的微服务框架服务治理注册中心的搭建 Spring Cloud Eureka基于Netflix Eureka做了二次封装,是Spring Cloud Netflix ...
- Nacos注册中心和服务消费方式
哈喽朋友们本次小無分享Nacos注册中心和服务消费方式 前言:本期文章操作性不多,多在于详细的理论说明 还各位看官耐心看完 一,服务治理介绍 目录 一,服务治理介绍 二,nacos简介 nacos实战 ...
- [享学Eureka] 一、源生Eureka介绍 --- 基于注册中心的服务发现
凡事皆有代价,一切皆是取舍. 本专栏所有文章均计划逐步重写搬迁至本人公号:Java方向盘,且免费开放!故不再建议下单购买,可关注我公号前往免费学习.交流 –> 返回Netflix OSS套件专栏 ...
- Dubbo学习笔记001---分布式服务调用_Dubbo简介_依赖zookeeper做为注册中心进行服务注册与发现
JAVA技术交流QQ群:170933152 Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员 ...
- Dubbo使用multicast广播注册中心暴露服务地址时启动报错empty notify
2019独角兽企业重金招聘Python工程师标准>>> 开发机上Dubbo使用multicast广播注册中心暴露服务地址 <dubbo:registry protocol=&q ...
- linux 查看zookeeper目录,查看zookeeper注册中心是否有注册服务
查看zookeeper注册中心是否有注册服务可以在服务器上看,也可以在dubboadmin看哦~ 1.在服务器上看: 1)查找zookeeper的目录: find / -name zookeeper ...
- 注册中心Consul
注册中心Consul 1 Consul介绍 2 安装 3 Consul使用 3.1 Consul集群注册 4 消费 1 Consul介绍 在 Spring Cloud 中,大部分组件都有备选方案,例如 ...
最新文章
- CF1B Spreadsheets
- const在指针中的用法
- 专家:电脑看多了掉头发怎么办?
- 056、macvlan网络结构分析(2019-03-25 周一)
- 没有上司的舞会(洛谷-P1352)
- 进入Activity后让EditText自动弹出小键盘
- 计算机课程教改论文,高职计算机教改的课程设计研究论文
- 2018-2019-2 20165118 《网络对抗技术》Exp4 恶意代码分析
- Win7下PDF文件无法显示缩略图的解决方法
- 【教程】安卓7.0-11.0高版本 fiddler抓包失败的解决方案
- 接口测试用例设计 - 精简版
- 思科模拟器配置默认路由(下一跳使用端口)
- android扁平化按钮图标,35个扁平化设计(Flat UI)图标UI Kit素材下载
- 高并发场景设计与解决方案
- 《计算机网络原理》IP部分
- 【硬核】肝了一个月,Cisco网络工程师知识点总结
- win10下载安装office2016
- “求同”不是最终目标,英特尔致力打造有“差异”的精彩
- 使用css制作跳动的心
- ROS控制桌面机械手Dobot魔术师
热门文章
- java代码优化的方法和准则_编写高质量代码:改善Java程序的151个建议(第1章:JAVA开发中通用的方法和准则___建议16~20)...
- 因子分析 二元logistic回归
- (改进GM(1,1)模型)灰色残差马尔科夫预测模型的matlab实现
- MySQL8.0 OCP最新版1Z0-908认证考试题库整理-006
- 8s数据导入导出的load和unload解析
- 清东陵的三次大规模盗掘
- ▲ Android自定义方框EditText注册验证码
- delete、truncate 、Drop删除表的区别
- Docker 入门教程(一) - Docker Tutorial
- Unity3D 建筑类 虚拟漫游 PC 端 优化