服务包括:增加 删除 监听 事务处理
声明全局变量
var (
dialTimeout= 5*time.Second
requestTimeout=2*time.Second
endpoints=[]string{"10.0.154.197:2379"}
)
添加数据
func putValue(cli *clientv3.Client,key,value string)string{
//传入客户端
_,err:=cli.Put(context.TODO(),key,value);
if err!=nil{
log.Fatal(err)
}else{
return "ok"
}
return "FAIL"
}
获取展示数据
//获取数据
func getValue(cli *clientv3.Client,key string)[]*mvccpb.KeyValue{
fmt.Println("" )
log.Println("获取值")
resp,err:=cli.Get(context.TODO(),key)
if err!=nil{
log.Fatal(err)
}else{
return resp.Kvs
}
return nil
}
删除数据
//delete
func delValue (cli *clientv3.Client,key string)int64{
log.Printf("删除%s的值",key)
delRes,err:=cli.Delete(context.TODO(),key)
if err!=nil{
log.Fatal(err)
}else{
return delRes.Deleted
}
return 0
}
按照前缀查询数据
//按照前缀获取数据
func getValueByPrefix(cli *clientv3.Client,key string)[]*mvccpb.KeyValue{
fmt.Println("按照key的前缀获取数据")
resp,err:=cli.Get(context.TODO(),key,clientv3.WithPrefix())
if err!=nil{
fmt.Println(err)
}else{
return resp.Kvs
}
return nil
}
按照前缀删除数据
func delValueByprefix(cli *clientv3.Client,key string )int64{
fmt.Println("按前缀删除数据")
resp,err:=cli.Delete(context.TODO(),key,clientv3.WithPrefix())
if err!=nil{
fmt.Println(err)
}else {
return resp.Deleted
}
return 0
}
主函数
创建访问ETCD数据
/创建一个访问etcd的对象
cli,err:=clientv3.New(clientv3.Config{
Endpoints:endpoints,
DialTimeout:dialTimeout,
})
if err!=nil{
log.Fatal(err)
}
defer cli.Close()
调用增删改
putResult:=putValue(cli,"a","111a1")
fmt.Println("储存结果",putResult)
getResult:=getValue(cli,"a")
if len(getResult)>=1{
for _,item:=range getResult {
log.Printf("获取值的key:%s,valu:%s", string(item.Key), string(item.Value))
}
}else{
log.Printf("获取值的key:%s ,value:%S","a","")
}
//删除
delReslt:=delValue(cli,"a")
fmt.Println(delReslt)
putValue(cli,"abc","1000")
putValue(cli,"abb","2000")
putValue(cli,"abd","3000")
getRES:=getValueByPrefix(cli,"ab")
if len(getRES)>=1{
for _,item:=range getRES{
fmt.Println(string(item.Value))
}
}
ResDEl:=delValueByprefix(cli,"ab")
fmt.Println(ResDEl)
事务处理
//事务处理
putValue(cli,"user1","good")
_,err1:=cli.Txn(context.TODO()).
If(clientv3.Compare(clientv3.Value("user1"),"=","good")).
Then(clientv3.OpPut("user1","bad-bad")).Commit()
if err1!=nil{
fmt.Println(err)
}
rlt:=getValue(cli,"user1")
log.Printf("获取值的key:%s,value:%s",string(rlt[0].Key),string(rlt[0].Value))
监听
go func (){
rch:=cli.Watch(context.Background(),"foo")
for watch:=range rch{
for _,ev:=range watch.Events{
fmt.Println("watch foo---%s %q:%q\n",ev.Type,ev.Kv.Key,ev.Kv.Value)
}
}
}()
putValue(cli,"foo","bcd")
设置数据有效时间
//设置租期时间
resplease,err2:=cli.Grant(context.TODO(),5)
_,err2=cli.Put(context.TODO(),"fooo","bar",clientv3.WithLimit(resplease.TTL))
if err2!=nil{
fmt.Println(err2)
}
getResplease,err3:=cli.Get(context.TODO(),"foo")
if err3!=nil{
fmt.Println(err3)
}
fmt.Println("这是6秒前",string(getResplease.Kvs[0].Key),string(getResplease.Kvs[0].Value))
time.Sleep(6*time.Second)
getResplease,err3=cli.Get(context.TODO(),"foo")
if err3!=nil{
fmt.Println(err3)
}
if len(getResplease.Kvs)>=1 {
fmt.Println("这是6秒前", string(getResplease.Kvs[0].Key), string(getResplease.Kvs[0].Value))