Etcd(四)[Golang操作客户端]

Etcd-Golang操作客户端


需要导入的包:

"go.etcd.io/etcd/clientv3"

1.下载

go get -u -v go.etcd.io/etcd/clientv3

2.操作

参考地址:

https://pkg.go.dev/go.etcd.io/etcd/clientv3?tab=doc

或者:

https://godoc.org/go.etcd.io/etcd/clientv3

1.初始化项目

mkdir -p $GOPATH/src/etcdDemo && cd $_
go mod init etcdDemo

2.go.mod

module etcdDemo

go 1.14

require (
	github.com/coreos/bbolt v1.3.4 // indirect
	github.com/coreos/etcd v3.3.20+incompatible // indirect
	github.com/coreos/go-semver v0.3.0 // indirect
	github.com/coreos/go-systemd v0.0.0-00010101000000-000000000000 // indirect
	github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
	github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
	github.com/gogo/protobuf v1.3.1 // indirect
	github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
	github.com/google/btree v1.0.0 // indirect
	github.com/google/uuid v1.1.1 // indirect
	github.com/gorilla/websocket v1.4.2 // indirect
	github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect
	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
	github.com/grpc-ecosystem/grpc-gateway v1.14.4 // indirect
	github.com/jonboulle/clockwork v0.1.0 // indirect
	github.com/prometheus/client_golang v1.6.0 // indirect
	github.com/soheilhy/cmux v0.1.4 // indirect
	github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 // indirect
	github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
	go.etcd.io/bbolt v1.3.4 // indirect
	go.etcd.io/etcd v3.3.20+incompatible
	go.uber.org/zap v1.15.0 // indirect
	golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
	google.golang.org/grpc v1.29.1 // indirect
	sigs.k8s.io/yaml v1.2.0 // indirect
)

replace github.com/coreos/bbolt v1.3.4 => go.etcd.io/bbolt v1.3.4

replace go.etcd.io/bbolt v1.3.4 => github.com/coreos/bbolt v1.3.4

replace github.com/coreos/go-systemd => github.com/coreos/go-systemd/v22 v22.0.0

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

3.main.go

cd 
vim main.go

内容如下:

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"go.etcd.io/etcd/clientv3"
)

func main() {
	// 新建etcd/v3的client,连接本地的etcd-server
	cli, err := clientv3.New(clientv3.Config{
		// 节点信息
		Endpoints: []string{"http://127.0.0.1:2379"},
		// 设置超时时间为5秒
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()
	// put 操作的超时控制设置为1秒
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	// put 操作
	putResp, err := cli.Put(ctx, "aaa", "123456789")
	cancel()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("PUT aaa=123456789,response:", putResp)
	// get
	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
	// put 操作
	getResp, err := cli.Get(ctx, "aaa")
	cancel()
	if err != nil {
		log.Fatal(err)
	}
	for _, val := range getResp.Kvs {
		fmt.Printf("GET response:%s=%s\n", val.Key, val.Value)
	}
	// del操作
	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
	delResp, err := cli.Delete(ctx, "aaa")
	cancel()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Delete aaa,response:", delResp)
}

4.运行

cd $GOPATH/src/etcdDemo
go run main.go

Etcd(四)[Golang操作客户端]

上一篇:【AECC】Adobe After Effects CC软件下载及安装教程(附软件及安装教程)


下一篇:unity2018 lightmap烘焙最佳实践