一:什么是consul
1.consul是一套开源的分布式服务注册与发现和配置管理系统,由HashiCorp公司用Go语言开发
2.它提供了为服务系统中的服务治理,配置中心,控制总线等功能,这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网络,总之consul提升了一种完整的服务网络解决方案。
3.它的优点包括基于raft协议,比较简洁,支持健康检查,同时支持http和DNS协议 支持跨数据中心的WAN集群 提供图形化界面 跨平台,支持linux,mac,windows
二:安装并运行consul
1.下载consul(百度 consul官网即可,点击download 然后下载对应操作系统的安装包,这里以windows 64位为例)
2.下载完后解压,只有一个consul.exe文件
3.在此consul.exe所在的文件夹打开cmd窗口,输入 consul --version 可查看consul的具体版本
consul --version
4.cmd中 输入 consul agent -dev进行启动
consul agent -dev
5.启动完后可以用localhost:8500进行访问安装的consul首页
6.linus安装步骤:
1.百度consul下载(https://releases.hashicorp.com/consul/1.10.3/),下载consul的zip包,或者在linux使用 wget 命令直接下载到指定的文件夹中
2.使用 unzip + 包名 解压压缩包
3.使用./consul命令检查安装是否有误
4.使用 nohup consul agent -server -data-dir=/opt/consul/data/ -node=node1 -bind=0.0.0.0 -bootstrap-expect=1 -client=0.0.0.0 -ui > /opt/consul/consul.log & 进行启动,指定data路径和日志文件
5.使用服务器ip + 8500(默认端口)访问客户端页面
三:.springboot整合consul
1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
2.配置文件
spring:
application:
name: cloud-consumer-order82
cloud:
#consul相关配置信息
consul:
host: localhost #主机ip
port: 8500 #端口
discovery:
service-name: ${spring.application.name} #注册时被发现的名字(这里取服务名称)
hostname: 127.0.0.1 #主机名
heartbeat:
enable: true #开启心跳检查
3.启动类加上@EnableDiscoveryClient注解
4.启动服务,启动成功后 用 localhost:8005可以看到已经注册上去的微服务节点