demo1
package main
import (
"github.com/coreos/etcd/clientv3"
"time"
"fmt"
)
func main() {
var (
config clientv3.Config
client *clientv3.Client
err error
)
// 客户端配置
config = clientv3.Config{
Endpoints: []string{"36.111.184.221:2379"},
DialTimeout: 5 * time.Second,
}
// 建立连接
if client, err = clientv3.New(config); err != nil {
fmt.Println(err)
return
}
client = client
}
demo2
package main
import (
"github.com/coreos/etcd/clientv3"
"time"
"fmt"
"context"
)
func main() {
var (
config clientv3.Config
client *clientv3.Client
err error
kv clientv3.KV
putResp *clientv3.PutResponse
)
config = clientv3.Config{
Endpoints: []string{"36.111.184.221:2379"}, // 集群列表
DialTimeout: 5 * time.Second,
}
// 建立一个客户端
if client, err = clientv3.New(config); err != nil {
fmt.Println(err)
return
}
// 用于读写etcd的键值对
kv = clientv3.NewKV(client)
if putResp, err = kv.Put(context.TODO(), "/cron/jobs/job1", "bye", clientv3.WithPrevKV()); err != nil {
fmt.Println(err)
} else {
fmt.Println("Revision:", putResp.Header.Revision)
if putResp.PrevKv != nil { // 打印hello
fmt.Println("PrevValue:", string(putResp.PrevKv.Value))
}
}
}
demo3
package main
import (
"github.com/coreos/etcd/clientv3"
"time"
"fmt"
"context"
)
func main() {
var (
config clientv3.Config
client *clientv3.Client
err error
kv clientv3.KV
getResp *clientv3.GetResponse
)
config = clientv3.Config{
Endpoints: []string{"36.111.184.221:2379"}, // 集群列表
DialTimeout: 5 * time.Second,
}
// 建立一个客户端
if client, err = clientv3.New(config); err != nil {
fmt.Println(err)
return
}
// 用于读写etcd的键值对
kv = clientv3.NewKV(client)
if getResp, err = kv.Get(context.TODO(), "/cron/jobs/job1", /*clientv3.WithCountOnly()*/); err != nil {
fmt.Println(err)
} else {
fmt.Println(getResp.Kvs, getResp.Count)
}
}
demo4
package main
import (
"github.com/coreos/etcd/clientv3"
"time"
"fmt"
"context"
)
func main() {
var (
config clientv3.Config
client *clientv3.Client
err error
kv clientv3.KV
getResp *clientv3.GetResponse
)
config = clientv3.Config{
Endpoints: []string{"36.111.184.221:2379"}, // 集群列表
DialTimeout: 5 * time.Second,
}
// 建立一个客户端
if client, err = clientv3.New(config); err != nil {
fmt.Println(err)
return
}
// 用于读写etcd的键值对
kv = clientv3.NewKV(client)
// 写入另外一个Job
kv.Put(context.TODO(), "/cron/jobs/job