etcd版本3.2,开放端口2379
package main
import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"time"
)
func main() {
const (
key1 = "/etc/etcd/etcd.conf"
val2 = "etcd.conf"
keepTime = 100*time.Second
)
var (
config clientv3.Config
client *clientv3.Client
Newlease clientv3.Lease
leaseResp *clientv3.LeaseGrantResponse
kv clientv3.KV
err error
putResp *clientv3.PutResponse
getResp *clientv3.GetResponse
keepaliveResp *clientv3.LeaseKeepAliveResponse
keepRespChan <-chan *clientv3.LeaseKeepAliveResponse
)
config = clientv3.Config{
Endpoints: []string{"192.168.1.102:2379"},
DialTimeout: 10 * time.Second,
}
if client, err = clientv3.New(config); err != nil {
fmt.Println(err)
return
}
Newlease = clientv3.NewLease(client)
if leaseResp, err = Newlease.Grant(context.TODO(), 10); err != nil {
fmt.Println(err)
return
}
leaseID := leaseResp.ID
ctx, _ := context.WithTimeout(context.TODO(), keepTime)
if keepRespChan, err = Newlease.KeepAlive(ctx, leaseID); err != nil {
fmt.Println(err)
return
}
go func() {
for {
select {
case keepaliveResp = <-keepRespChan:
if keepRespChan == nil {
fmt.Println("租约终止.")
goto END
} else {
fmt.Println("收到续租应答.", keepaliveResp.ID)
}
}
}
END:
}()
kv = client.KV
putResp, err = kv.Put(context.TODO(), key1, val2, clientv3.WithLease(leaseID))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("写入成功: Revision ->", putResp.Header.Revision)
for {
if getResp, err = kv.Get(context.TODO(), key1); err != nil {
fmt.Println(err)
return
}
fmt.Println("值:", getResp.Kvs)
if getResp.Count == 0 {
fmt.Println("租约过期了")
break
}
time.Sleep(2 * time.Second)
fmt.Println(time.Now())
}
}