etcd,一个分布式的可靠key/value存储系统,用于存储分布式系统最关键、最重要数据,它具有以下特性:
- 简单: 定义良好,面向用户API(gRPC)
- 安全: 使用可选客户端证书认证进行自动TLS
- 快速: 基准线每秒1W写入
- 可靠: 使用Raft协议
本地独立集群
注:独立集群,是指单台服务器的集群
下载二进制免安装压缩包,下载地址

启动命令:
cd D:\Program Files\etcd-v3.4.27
#单机启动etcd
etcd.exe
启动界面如下:

本地多成员集群
注:多成员集群,是指多台服务器的集群
#启动etcd集群
.\goreman.exe -f .\Procfile start
#查看集群成员
etcdctl --write-out=table --endpoints=localhost:2379 member list
Procfile 在github的etcd项目的根目录下:
etcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380 --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380 --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380 --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
启动日志:

查看集群成员:

直接使用github上的Procfile去启动etcd集群,发现启动不了,定位了很久,发现是--initial-cluster 参数值放在单引号里面引起的,去掉就能启动成功
--initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380'

存储键值对及获取键对应的值:

# 杀掉 etcd2
$ goreman run stop etcd2
# 注:windows实测这个命令可以停止etcd
$ etcdctl --endpoints=localhost:12379 put key hello
OK
$ etcdctl --endpoints=localhost:12379 get key
hello
# 试图从被杀掉的成员获取key
$ etcdctl --endpoints=localhost:22379 get key
{"level":"warn","ts":"2023-07-11T23:57:28.66665+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-b5ec35a9-0b28-43bb-82b3-6e90bb8831a5/localhost:22379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = latest balancer error: all SubConns are in TransientFailure, latest connection error: connection error: desc = \"transport: Error while dialing dial tcp [::1]:22379: connectex: No connection could be made because the target machine actively refused it.\""}
Error: context deadline exceeded
# 重启被杀掉的成员
# 注:实测这个restart命令可用
$ goreman run restart etcd2
# 从重启的成员获取key
$ etcdctl --endpoints=localhost:22379 get key
hello
网友评论