安装

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路径

  1. # Use goreman to run `go get github.com/mattn/goreman`
  2. 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=stderr
  3. etcd2: /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=stderr
  4. etcd3: /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
  5. #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

5.启动
goreman -f Procfile start

多机集群搭建

参考: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

操作示例

新增、修改、删除、查看操作

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "go.etcd.io/etcd/clientv3"
  6. "time"
  7. )
  8. var (
  9. etcdClient *clientv3.Client
  10. err error
  11. )
  12. func main() {
  13. config := clientv3.Config{
  14. Endpoints: []string{"192.168.72.10:2379"}, //etcd地址
  15. DialTimeout: 5 * time.Second, //连接超时时间
  16. }
  17. etcdClient, err = clientv3.New(config)
  18. defer etcdClient.Close()
  19. if err != nil {
  20. fmt.Println("connect etcd err", err)
  21. return
  22. }
  23. //新增,修改
  24. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  25. _, err = etcdClient.Put(ctx, "test", "张志玉")
  26. cancel()
  27. if err != nil {
  28. fmt.Println("put etcd key err", err)
  29. return
  30. }
  31. //查看
  32. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  33. resp, err := etcdClient.Get(ctx, "test")
  34. cancel()
  35. if err != nil {
  36. fmt.Println("get etcd key err", err)
  37. return
  38. }
  39. for _, ev := range resp.Kvs {
  40. fmt.Println(string(ev.Key), string(ev.Value))
  41. }
  42. //删除
  43. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  44. _, err = etcdClient.Delete(ctx, "test")
  45. cancel()
  46. if err != nil {
  47. fmt.Println("delete etcd key err", err)
  48. return
  49. }
  50. }

watch操作

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "go.etcd.io/etcd/clientv3"
  6. "time"
  7. )
  8. var (
  9. etcdClient *clientv3.Client
  10. err error
  11. )
  12. func main() {
  13. config := clientv3.Config{
  14. Endpoints: []string{"192.168.72.10:2379"}, //etcd地址
  15. DialTimeout: 5 * time.Second, //连接超时时间
  16. }
  17. etcdClient, err = clientv3.New(config)
  18. defer etcdClient.Close()
  19. if err != nil {
  20. fmt.Println("connect etcd err", err)
  21. return
  22. }
  23. //观察
  24. //创建一个哨兵,监控某个key的新增(PUT)、修改(PUT)、删除(DELETE)操作,
  25. watchCh := etcdClient.Watch(context.Background(), "test")
  26. //从通道取值
  27. for watch := range watchCh {
  28. for _, evt := range watch.Events {
  29. //输出该key的类型、key,value
  30. fmt.Println(evt.Type, evt.Kv.Key, evt.Kv.Value)
  31. }
  32. }
  33. }


异常汇总

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
原因:
客户端连接如要证书认证