乔克视界 乔克视界
首页
  • 运维
  • 开发
  • 监控
  • 安全
  • 随笔
  • Docker
  • Golang
  • Python
  • AIOps
  • 心情杂货
  • 读书笔记
  • 面试
  • 实用技巧
  • 博客搭建
友链
关于
收藏
  • 分类
  • 标签
  • 归档

乔克

云原生爱好者
首页
  • 运维
  • 开发
  • 监控
  • 安全
  • 随笔
  • Docker
  • Golang
  • Python
  • AIOps
  • 心情杂货
  • 读书笔记
  • 面试
  • 实用技巧
  • 博客搭建
友链
关于
收藏
  • 分类
  • 标签
  • 归档
  • Docker

  • Golang

    • Golang基础知识

    • Golang进阶知识

      • 使用database_sql
      • 使用sqlx
      • 操作Redis
      • 操作NSQ
      • 操作kafka
      • 操作Etcd
        • 操作ElasticSearch
      • Golang常用包

    • AIOps

    • 专栏
    • Golang
    • Golang进阶知识
    乔克
    2025-07-13
    目录

    操作Etcd

    这里使用官方的etcd/clientv3 (opens new window)包来连接 etcd 并进行相关操作。

    # 安装

    go get go.etcd.io/etcd/clientv3
    
    1

    # put 和 get 操作

    put命令用来设置键值对数据,get命令用来根据 key 获取值。

    package main
    
    import (
    	"context"
    	"fmt"
    	"time"
    	"go.etcd.io/etcd/clientv3"
    )
    
    func main(){
    	cli, err := clientv3.New(clientv3.Config{
    		Endpoints:   []string{"122.51.79.172:2379"},
    		DialTimeout: 5 * time.Second,
    	})
    	if err != nil {
    		// handle error!
    		fmt.Printf("connect to etcd failed, err:%v\n", err)
    		return
    	}
    	fmt.Println("connect to etcd success")
    	defer cli.Close()
    	// put
    	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    	_, err = cli.Put(ctx, "coolops", "test")
    	cancel()
    	if err != nil {
    		fmt.Printf("put to etcd failed, err:%v\n", err)
    		return
    	}
    	// get
    	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
    	resp, err := cli.Get(ctx, "coolops")
    	cancel()
    	if err != nil {
    		fmt.Printf("get from etcd failed, err:%v\n", err)
    		return
    	}
    	for _, ev := range resp.Kvs {
    		fmt.Printf("%s:%s\n", ev.Key, ev.Value)
    	}
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41

    # watch 操作

    watch用来获取未来更改的通知。

    package main
    
    import (
    	"context"
    	"fmt"
    	"time"
    	"go.etcd.io/etcd/clientv3"
    )
    
    func main(){
    	cli, err := clientv3.New(clientv3.Config{
    		Endpoints:   []string{"122.51.79.172:2379"},
    		DialTimeout: 5 * time.Second,
    	})
    	if err != nil {
    		// handle error!
    		fmt.Printf("connect to etcd failed, err:%v\n", err)
    		return
    	}
    	fmt.Println("connect to etcd success")
    	defer cli.Close()
    	// watch 操作,返回的是一个通道
    	rch := cli.Watch(context.Background(), "coolops") // <-chan WatchResponse
    	for wresp := range rch {
    		for _, ev := range wresp.Events {
    			fmt.Printf("Type: %s Key:%s Value:%s\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
    		}
    	}
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

    # 安装报错:

    go: finding github.com/coreos/pkg latest
    # github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
    E:\DEV\Go\pkg\mod\github.com\coreos\etcd@v3.3.19+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
    E:\DEV\Go\pkg\mod\github.com\coreos\etcd@v3.3.19+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOption
    # github.com/coreos/etcd/clientv3/balancer/picker
    E:\DEV\Go\pkg\mod\github.com\coreos\etcd@v3.3.19+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
    E:\DEV\Go\pkg\mod\github.com\coreos\etcd@v3.3.19+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
    
    
    1
    2
    3
    4
    5
    6
    7
    8

    解决:

    将 go.mod 里的 prpc 改为 1.26.0 版本

    google.golang.org/grpc v1.26.0
    
    1
    上次更新: 2025/07/18, 11:04:43
    操作kafka
    操作ElasticSearch

    ← 操作kafka 操作ElasticSearch→

    最近更新
    01
    2025年,SRE在企业中可以做哪些事
    07-18
    02
    SRE 如何提升自己在团队中的影响力
    07-18
    03
    使用Go开发MCP服务
    07-18
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 乔克 | MIT License | 渝ICP备20002153号 |
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式