安装
1.下载地址
https://github.com/etcd-io/etcd/releases/
2.部署
cd /tools/
tar -zxvf etcd-v3.5.2-linux-amd64.tar.gz
3.验证版本
/tools/etcd-v3.5.2-linux-amd64/etcd —version
/tools/etcd-v3.5.2-linux-amd64/etcdctl version
/tools/etcd-v3.5.2-linux-amd64/etcdutl version
4.启动
/tools/etcd-v3.5.2-linux-amd64/etcd //缺陷为不可通过IP访问
/tools/etcd-v3.5.2-linux-amd64/etcd —name=etcd1 —data-dir=/data/etcd —listen-peer-urls=”http://0.0.0.0:2380“ —listen-client-urls=”http://0.0.0.0:2379“ —advertise-client-urls=”http://0.0.0.0:2379“
5.验证数据写入、读取、更新、读取、删除
/tools/etcd-v3.5.2-linux-amd64/etcdctl —endpoints=http://127.0.0.1:2379 put foo bar
/tools/etcd-v3.5.2-linux-amd64/etcdctl —endpoints=http://127.0.0.1:2379 get foo
/tools/etcd-v3.5.2-linux-amd64/etcdctl —endpoints=http://127.0.0.1:2379 put foo bar2
/tools/etcd-v3.5.2-linux-amd64/etcdctl —endpoints=http://127.0.0.1:2379 get foo
/tools/etcd-v3.5.2-linux-amd64/etcdctl —endpoints=http://127.0.0.1:2379 del foo
集群搭建
本地集群安装
参考:https://github.com/etcd-io/etcd/blob/v3.4.9/Documentation/dev-guide/local_cluster.md
1.go环境搭建
2.安装goreman
go install github.com/mattn/goreman@latest
3.配置环境变量
vim /etc/profile
export PATH=$PATH:/root/go/bin
4.准备Procfile文件
/opt/etcd/etcd请替换成正确的etcd路径
# Use goreman to run `go get github.com/mattn/goreman`etcd1: /opt/etcd/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=stderretcd2: /opt/etcd/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=stderretcd3: /opt/etcd/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#proxy: bin/etcd grpc-proxy start --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 --listen-addr=127.0.0.1:23790 --advertise-client-url=127.0.0.1:23790 --enable-pprof
多机集群搭建
参考:https://github.com/etcd-io/etcd/blob/v3.4.9/Documentation/op-guide/clustering.md
GoLang操作ETCD
安装第三方库
使用旧版本etcd包需要修改grpc版本
go get go.etcd.io/etcd/clientv3
go新版本使用如下etcd包
go get go.etcd.io/etcd/client/v3
注意事项
etcd的client3与grpc冲突,grpc的版本要修改为1.26
http://wearygods.online/articles/2020/09/17/1600327573429.html
操作示例
新增、修改、删除、查看操作
package mainimport ("context""fmt""go.etcd.io/etcd/clientv3""time")var (etcdClient *clientv3.Clienterr error)func main() {config := clientv3.Config{Endpoints: []string{"192.168.72.10:2379"}, //etcd地址DialTimeout: 5 * time.Second, //连接超时时间}etcdClient, err = clientv3.New(config)defer etcdClient.Close()if err != nil {fmt.Println("connect etcd err", err)return}//新增,修改ctx, cancel := context.WithTimeout(context.Background(), time.Second)_, err = etcdClient.Put(ctx, "test", "张志玉")cancel()if err != nil {fmt.Println("put etcd key err", err)return}//查看ctx, cancel = context.WithTimeout(context.Background(), time.Second)resp, err := etcdClient.Get(ctx, "test")cancel()if err != nil {fmt.Println("get etcd key err", err)return}for _, ev := range resp.Kvs {fmt.Println(string(ev.Key), string(ev.Value))}//删除ctx, cancel = context.WithTimeout(context.Background(), time.Second)_, err = etcdClient.Delete(ctx, "test")cancel()if err != nil {fmt.Println("delete etcd key err", err)return}}
watch操作
package mainimport ("context""fmt""go.etcd.io/etcd/clientv3""time")var (etcdClient *clientv3.Clienterr error)func main() {config := clientv3.Config{Endpoints: []string{"192.168.72.10:2379"}, //etcd地址DialTimeout: 5 * time.Second, //连接超时时间}etcdClient, err = clientv3.New(config)defer etcdClient.Close()if err != nil {fmt.Println("connect etcd err", err)return}//观察//创建一个哨兵,监控某个key的新增(PUT)、修改(PUT)、删除(DELETE)操作,watchCh := etcdClient.Watch(context.Background(), "test")//从通道取值for watch := range watchCh {for _, evt := range watch.Events {//输出该key的类型、key,valuefmt.Println(evt.Type, evt.Kv.Key, evt.Kv.Value)}}}
异常汇总
1.客户端连接etcd异常
执行命令:
/tools/etcd-v3.5.2-linux-amd64/etcdctl —endpoints=localhost:2379 put foo bar
响应结果:
{“level”:”warn”,”ts”:”2022-02-28T20:59:44.398-0500”,”logger”:”etcd-client”,”caller”:”v3/retry_interceptor.go:62”,”msg”:”retrying of unary invoker failed”,”target”:”etcd-endpoints://0xc00043e380/localhost:2379”,”attempt”:0,”error”:”rpc error: code = DeadlineExceeded desc = latest balancer error: last connection error: connection closed”}
Error: context deadline exceeded
原因:
客户端连接如要证书认证
