Consul是强一致性的数据存储,使用gossip形成动态集群。它提供分级键/值存储方式,不仅可以存储数据,而且可以用于注册器件事各种任务,从发送数据改变通知到运行健康检查和自定义命令,具体如何取决于它们的输出。下面两张图是Consul的原理图
Consul是分布式的、高可用、横向扩展的。consul提供的一些关键特性:
Feature | Consul | zookeeper | etcd | euerka |
---|---|---|---|---|
服务健康检查 | 服务状态,内存,硬盘等 | (弱)长连接,keepalive | 连接心跳 | 可配支持 |
多数据中心 | 支持 | — | — | — |
kv存储服务 | 支持 | 支持 | 支持 | — |
一致性 | raft | paxos | raft | — |
cap | ca | cp | cp | ap |
使用接口(多语言能力) | 支持http和dns | 客户端 | http/grpc | http(sidecar) |
watch支持 | 全量/支持long polling | 支持 | 支持 long polling | 支持 long polling/大部分增量 |
自身监控 | metrics | — | metrics | metrics |
安全 | acl /https | acl | https支持(弱) | — |
spring cloud集成 | 已支持 | 已支持 | 已支持 | 已支持 |
stackshare官方提供的使用情况对比统计
https://stackshare.io/stackups/consul-vs-zookeeper-vs-etcd
其他操作系统的安装包下载地址
https://www.consul.io/downloads.html
D:
cd D:\opt\consul
consul agent -dev
可以看到启动成功。
打开网址:http://localhost:8500 ,可以看到界面,相关服务发现的界面。
$ mkdir -p $GOPATH/src/github.com/hashicorp && cd $!
$ git clone https://github.com/hashicorp/consul.git
$ cd consul
$ make bootstrap
$ make bootstrap
setenforce 0
sed --follow-symlinks -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
yum install docker
systemctl enable docker && systemctl start docker
firewall-cmd --set-default-zone=trusted
firewall-cmd --complete-reload
-bind:该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0。
-client:consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1。
-node consul节点ID,集群内唯一
-join 192.168.1.60 节点加入集群(leader 192.168.1.60)
-server agent启用server模式运行。不加入此参数则client模式运行。
-ui 启用web管理页面
-data-dir 指定agent储存状态的数据目录
这里使用的是DIEA
pom
4.0.0
bamboo
consul-server
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
healthCheckPath: /health
healthCheckInterval: 5s
instance-id: consul-server
application:
name: consul-server
server:
port: 8501
这里说明一下:healthCheckPath中的REST借口需要在代码中实现并可以get方式正常访问,否则健康检查的结果将会一直是critical状态,即不可用严重状态
package bamboo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 使用consul作为服务注册发现中心
*
* 这里是一个服务启动类
*
* Created by xialeme on 2017/11/22.
*/
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulServerApplication {
@RequestMapping("/hi")
public String home() {
return "hello ConsulServer ";
}
//健康检查的实现REST部分
@RequestMapping("/health")
public String health() {
return "hello health ";
}
public static void main(String[] args) {
new SpringApplicationBuilder(ConsulServerApplication.class).web(true).run(args);
}
}
运行ConsulServerApplication
可以在监控台看到如下信息,说明已经成功了
consul还提供了键/值存储的功能。
可以在http://localhost:8500中点击KEY/VALUE按钮进行操作
使用API的方法如下:
如 查询 所有K/V
curl -v http://localhost:8500/v1/kv/?recurse
保存键为web/key2, flags 为42, 值为true的记录。
curl -X PUT -d ‘test’ http://localhost:8500/v1/kv/web/key2?flags=42
true
删除记录:
curl -X DELETE http://localhost:8500/v1/kv/web/sub?recurse
更新值:
curl -X PUT -d ‘newval’ http://localhost:8500/v1/kv/web/key1?cas=97
true
更新index:
curl “http://localhost:8500/v1/kv/web/key2?index=101&wait=5s”
结果:[{“CreateIndex”:98,“ModifyIndex”:101,“Key”:“web/key2”,“Flags”:42,“Value”:“dGVzdA==”}]
更详细的consul命令详解:
http://m.oschina.net/blog/353392
当有一台服务器不可用时,处理的方法有:
对服务器进恢复,然后重新上线
用新服务器,替代旧的consul服务器
这两种方式都需要将服务器ip与原来的ip相同。
添加新的服务器,而ip无需与原来的相同。步骤: 停掉所有的consul服务器,将损坏的服务器ip从raft/peer.json中移除,重启其他服务器,并将新的服务器加入集群。
https://github.com/BambooZhang/spring-cloud/tree/master/chapter9
其他教程的源码请在下面的地址中查找
https://github.com/BambooZhang/spring-cloud/
https://www.cnblogs.com/newP/p/6349316.html
sprngcloud中国提供的翻译
https://springcloud.cc/spring-cloud-consul.html