首先在后台启动etcd
写一段go代码
go
package main
import (
"context"
"fmt"
clientv3 "go.etcd.io/etcd/client/v3"
"time"
)
func main() {
// 创建客户端,连接etcd
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
fmt.Printf("connect to etcd failed, err:%v\n", err)
return
}
fmt.Println("connect to etcd success")
defer cli.Close()
str := `[{"path":"d:/logs/s4.logs","topic":"s4_logs"},{"path":"d:/logs/web.logs","topic":"web_logs"}`
// put操作
_, err = cli.Put(context.TODO(), "collect_log_conf", str)
if err != nil {
fmt.Printf("put to etcd failed, err:%v\n", err)
return
}
get操作
resp, err := cli.Get(context.TODO(), "collect_log_conf") // 获取指定Key的值
if err != nil {
fmt.Printf("get to etcd failed")
return
}
for _, ev := range resp.Kvs {
fmt.Printf("key:%s,value:%s\n", ev.Key, ev.Value)
}
delete 删除key
//if _,err = cli.Delete(context.TODO(), "testetcd");err != nil{
// fmt.Println("delete key failed")
//}else{
// fmt.Println("delete key success")
//}
}
也可以直接使用ectdctl工具查看key
go
etcdctl.exe --endpoints=127.0.0.1:2379 get collect_log_conf