ETCD 了解一下

etcd,一个分布式的可靠key/value存储系统,用于存储分布式系统最关键、最重要数据,它具有以下特性:

  • 简单: 定义良好,面向用户API(gRPC)
  • 安全: 使用可选客户端证书认证进行自动TLS
  • 快速: 基准线每秒1W写入
  • 可靠: 使用Raft协议

本地独立集群

:独立集群,是指单台服务器的集群
下载二进制免安装压缩包,下载地址

windows解压后目录

启动命令

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容