Spring Cloud Consul 项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具备分布式、高可用、高扩展性。html
Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其余分布式服务注册与发现的方案,Consul的方案更“一站式”
,内置了服务注册与发现框 架、具备如下性质:java
再也不须要依赖其余工具(好比ZooKeeper等)。node
使用起来也较 为简单。Consul使用Go语言编写,所以具备自然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合 。
基于 Mozilla Public License 2.0 的协议进行开源. Consul 支持健康检查,并容许 HTTP 和 DNS 协议调用 API 存储键值对.
一致性协议采用 Raft 算法,用来保证服务的高可用. 使用 GOSSIP 协议管理成员和广播消息, 而且支持 ACL 访问控制.linux
使用 Raft 算法来保证一致性, 比复杂的 Paxos 算法更直接. 相比较而言, zookeeper 采用的是 Paxos, 而 etcd 使用的则是 Raft.
支持多数据中心,内外网的服务采用不一样的端口进行监听。 多数据中心集群能够避免单数据中心的单点故障,而其部署则须要考虑网络延迟, 分片等状况等. zookeeper 和 etcd 均不提供多数据中心功能的支持.
支持健康检查. etcd 不提供此功能.
支持 http 和 dns 协议接口. zookeeper 的集成较为复杂, etcd 只支持 http 协议.
官方提供web管理界面, etcd 无此功能.nginx
client: 客户端, 无状态, 将 HTTP 和 DNS 接口请求转发给局域网内的服务端集群.server: 服务端, 保存配置信息, 高可用集群, 在局域网内与本地客户端通信, 经过广域网与其余数据中心通信. 每一个数据中心的 server 数量推荐为 3 个或是 5 个.git
因为Spring Cloud Consul项目的实现,咱们能够轻松的将基于Spring Boot的微服务应用注册到Consul上,并经过此实现微服务架构中的服务治理。github
参考web
要想利用Consul提供的服务实现服务的注册与发现,咱们须要搭建Consul Cluster 环境。算法
在Consul方案中,每一个提供服务的节点上都要部署和运行Consul的agent,全部运行Consul agent节点的集合构成Consul Cluster。spring
Consul agent有两种运行模式:Server和Client。这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上 的应用服务无关。
以Server模式运行的Consul agent节点用于维护Consul集群的状态,官方建议每一个Consul Cluster至少有3个或以上的运行在Server mode的Agent,Client节点不限。
环境配置以下:
Centos 7.3
主机名称 | IP | 做用 | 是否容许远程访问 |
---|---|---|---|
node1 | 192.168.252.121 | consul server | 是 |
node2 | 192.168.252.122 | consul client | 是 |
node3 | 192.168.252.123 | consul client | 是 |
关闭防火墙
systemctl stop firewalld.service
Consul 最新版的下载地址:
https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip
下载,而后unzip 解压,获得惟一,一个可执行文件
cd /opt/ wget https://releases.hashicorp.com/consul/1.0.1/consul_1.0.1_linux_amd64.zip unzip consul_1.0.1_linux_amd64.zip cp consul /usr/local/bin/
查看是否安装成功
[root@node1 opt]# consul
出现以下结果,表示安装成功
Usage: consul [--version] [--help] <command> [<args>] Available commands are: agent Runs a Consul agent catalog Interact with the catalog event Fire a new event exec Executes a command on Consul nodes force-leave Forces a member of the cluster to enter the "left" state info Provides debugging information for operators. join Tell Consul agent to join cluster keygen Generates a new encryption key keyring Manages gossip layer encryption keys kv Interact with the key-value store leave Gracefully leaves the Consul cluster and shuts down lock Execute a command holding a lock maint Controls node or service maintenance mode members Lists the members of a Consul cluster monitor Stream logs from a Consul agent operator Provides cluster-level tools for Consul operators reload Triggers the agent to reload configuration files rtt Estimates network round trip time between nodes snapshot Saves, restores and inspects snapshots of Consul server state validate Validate config files/directories version Prints the Consul version watch Watch for changes in Consul
检查版本
[root@node1 opt]# consul version
Consul v1.0.1 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
命令 | 解释 | 示例 |
---|---|---|
agent | 运行一个consul agent | consul agent -dev |
join | 将agent加入到consul集群 | consul join IP |
members | 列出consul cluster集群中的members | consul members |
leave | 将节点移除所在集群 | consul leave |
-data-dir
-config-dir
-config-file
-dev
-bootstrap-expect
-node
-bind
Failed to get advertise address: Multiple private IPs found. Please configure one.
的异常-server
-client
-join
-datacenter(老版本叫-dc,-dc已经失效)
咱们尝试一下:
-dev表示开发模式运行,使用-client 参数可指定容许客户端使用什么ip去访问,例如-client 192.168.252.121 表示可使用
http://192.168.252.121:8500/ui/ 去访问。
consul agent -dev -client 192.168.252.121
Consul Cluster集群架构图以下:
这边准备了三台Centos 7.3的虚拟机,主机规划以下,供参考:
主机名称 | IP | 做用 | 是否容许远程访问 |
---|---|---|---|
node1 | 192.168.252.121 | consul server | 是 |
node2 | 192.168.252.122 | consul client | 是 |
node3 | 192.168.252.123 | consul client | 是 |
命令参数,参看上面详细介绍
在 node1 机器上启动 Consul
cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.121 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.121 -server -bootstrap-expect 1 > /dev/null 2>&1 &
在 node2 机器上启动 Consul,而且将node2节点加入到node1节点上
cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.122 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.122 -join=192.168.252.121 > /dev/null 2>&1 &
在 node3 机器上启动 Consul,而且将node3节点加入到node1节点上
cd /opt/ mkdir data consul agent -data-dir /opt/data -node=192.168.252.123 -bind=0.0.0.0 -datacenter=dc1 -ui -client=192.168.252.123 -join=192.168.252.121 > /dev/null 2>&1 &
在node1上查看当前集群节点:
consul members -rpc-addr=192.168.252.123:8400 consul leave -rpc-addr=192.168.252.123:8400
http://192.168.252.121:8500/ui/ 去访问。
新建项目:spring-cloud-consul-client
在项目 spring-cloud-consul-client
pom.xml
中引入须要的依赖内容:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
客户端注册Consul时,它提供有关自身的元数据,如主机和端口,ID,名称和标签。默认状况下,将建立一个HTTP 检查,每隔10秒Consul命中/health端点。若是健康检查失败,则服务实例被标记为关键。
package io.ymq.example.consul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulApplication { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(ConsulApplication.class, args); } }
在application.yml
配置文件中增长以下信息:若是Consul客户端位于localhost:8500之外,则须要配置来定位客户端
spring: application: name: consul-client cloud: consul: host: 192.168.252.121 port: 8500 discovery: healthCheckPath: / healthCheckInterval: 5s
若是Consul客户端位于localhost:8500之外的位置,则须要配置来定位客户端。例:
host: 192.168.252.121 port: 8500
HTTP健康检查路径 INSTALL
“10s”和“1m”分别表示10秒和1分
discovery: healthCheckPath: ${management.context-path}/health healthCheckInterval: 15s
到spring-cloud-consul-client
项目根目录下,执行mvn clean package
,把target
目录下 生成的 jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar
上传服务器,发布项目
打包命令
mvn clean package
发布命令
nohup java -jar spring-cloud-consul-client-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &
http://192.168.252.121:8500/ui/#/dc1/nodes/192.168.252.121
经过上图HTTP健康检查,能够看到服务检测正常
GitHub:https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul
码云:https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-consul