美文网首页
一个简单的etcdV3样例

一个简单的etcdV3样例

作者: fjxCode | 来源:发表于2018-10-08 06:29 被阅读0次
func main() {
    cliConfig := clientv3.Config{
        Endpoints:   []string{"127.0.0.1:2379"},
        DialTimeout: 100,
    }
    cli, err := clientv3.New(cliConfig)
    if err != nil {
        println(err)
    }
    defer cli.Close()
    //Put
    if _, err = cli.Put(context.TODO(), "/test/k1", "v1"); err != nil {
        println(err)
    }
    //Get
    if resp, err := cli.Get(context.TODO(), "/etst/k1"); err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("resp:", resp)
    }
    //Del
    if _, err = cli.Delete(context.TODO(), "/test/k1"); err != nil {
        println(err)
    }

    //TXN
    ctx, cancel := context.WithTimeout(context.Background(), 1000)
    _, err = cli.Txn(ctx).If(clientv3.Compare(clientv3.Value("/test/k1"), ">", "v1")).Then(clientv3.OpPut("k1", "abc")).Else(clientv3.OpPut("k1", "ABC")).Commit()
    cancel()
    if err != nil {
        println(err)
    }

    //Watch
    rangeWatch := cli.Watch(context.Background(),"",clientv3.WithPrefix())
    for watchResp := range rangeWatch {
        for _,ev := range watchResp.Events{
            fmt.Printf("%s %q :%q\n",ev.Type,ev.Kv.Key,ev.Kv.Value)
        }
    }
}

即使是本机启动,连接超时时间也不能少于1秒。

v3版 get的数据结构发生了变化,获取的是KV集合,字节数组需要转换,按如下方式获取KV值。

resp,err := cli.Get(context.TODO(),"/test/key1")
    if err != nil {
        println(err)
    }
    fmt.Println(string(resp.Kvs[0].Key))
    fmt.Println(string(resp.Kvs[0].Value))

Get节点及子节点的值:

//添加WithPrefix()属性
resp,err = cli.Get(context.TODO(),"/test",clientv3.WithPrefix())
    if err != nil {
        println(err)
    }
    fmt.Println(string(resp.Kvs[0].Key),string(resp.Kvs[0].Value))

Txn事务完成了一次CAS原子操作。需要在context中配置超时时间。ctx用完有调用cancel()取消(不等待)。

调用了clientV3的Value方法取值,Compare方法比较,OpPut方法赋值,最后提交完成CAS操作。此处路径全部加/,使用的是绝对路径。

Watch方法由WatchChan返回若干个WatchResponse,

type WatchResponse struct {
    Header pb.ResponseHeader
    Events []*Event

    // CompactRevision is the minimum revision the watcher may receive.
    CompactRevision int64

    // Canceled is used to indicate watch failure.
    // If the watch failed and the stream was about to close, before the channel is closed,
    // the channel sends a final response that has Canceled set to true with a non-nil Err().
    Canceled bool

    // Created is used to indicate the creation of the watcher.
    Created bool

    closeErr error

    // cancelReason is a reason of canceling watch
    cancelReason string
}

Get方法和Watch方法的返回结果,都附带了响应头:

type ResponseHeader struct {
    // cluster_id is the ID of the cluster which sent the response.
    ClusterId uint64 `protobuf:"varint,1,opt,name=cluster_id,json=clusterId,proto3" json:"cluster_id,omitempty"`
    // member_id is the ID of the member which sent the response.
    MemberId uint64 `protobuf:"varint,2,opt,name=member_id,json=memberId,proto3" json:"member_id,omitempty"`
    // revision is the key-value store revision when the request was applied.
    // For watch progress responses, the header.revision indicates progress. All future events
    // recieved in this stream are guaranteed to have a higher revision number than the
    // header.revision number.
    Revision int64 `protobuf:"varint,3,opt,name=revision,proto3" json:"revision,omitempty"`
    // raft_term is the raft term when the request was applied.
    RaftTerm uint64 `protobuf:"varint,4,opt,name=raft_term,json=raftTerm,proto3" json:"raft_term,omitempty"`
}

%q为单引号围绕的字符字面值。watchEvent.Kv.Key是字节数组,用fmt.Printf()加点位符%q打印的方式更好。

rangeWatch := cli.Watch(context.Background(),"",clientv3.WithPrefix())
    for watchResp := range rangeWatch{
        for _,watchEvent := range watchResp.Events{
            fmt.Println(watchEvent.Type,string(watchEvent.Kv.Key))
        }
    }

rangeWatch := cli.Watch(context.Background(),"",clientv3.WithPrefix())
    for watchResp := range rangeWatch{
        for _,watchEvent := range watchResp.Events{
            fmt.Printf("%s,%q,%q",watchEvent.Type,watchEvent.Kv.Key,watchEvent.Kv.Value)
        }
    }

事务Txn为context包装了一个超时时间。

context.TODO()和contest.Background()都返回一个空的context。contest.Background()没有截止日期,也没有cancel方法取消,一般用作main方法的cli操作上下文。

context.TODO()只对一个操作,单独配置一个上下文,如Txn事务,用完后调用cancel句柄取消。

相关文章

  • 一个简单的etcdV3样例

    即使是本机启动,连接超时时间也不能少于1秒。 v3版 get的数据结构发生了变化,获取的是KV集合,字节数组需要转...

  • RocketMQ的Pull模式如何实现负载均衡(MQPullCo

    RoceketMQ Push模式简单样例 RoceketMQ Pull模式简单样例 从上面的代码我们可以看出,pu...

  • python基础

    简单样例 判断循环

  • Spring AOP简单样例

    简介 AOP (Aspect Oriented Programming) 即 面向切面编程,是一种编程典范,它通过...

  • GraphX 介绍&简单样例

    一、总体框架 总体框架分为三个部分: l 存储层和原语层:Graph类是图计算的核心类,内部含有VertextRD...

  • Netty 简单样例分析

    Netty 是JBoss旗下的io传输的框架,他利用java里面的nio来实现高效,稳定的io传输。 作为io传输...

  • java8 Stream流

    下面是一些简单的方法的样例:

  • Hello GraphX

    本文将通过一个简单样例来讲解,Spark GraphX中的一些基本概念和常规操作。 样例 首先需要在pom中配置G...

  • 编程之美

    描述输入一个句子(一行),将句子中的每一个单词翻转后输出。 样例输入 样例输出 很简单的一道题,得出的方法很多,你...

  • 筛选N以内的素数

    1.题目描述用简单素数筛选法求N以内的素数。 2.格式与样例:输入格式N输出格式2~N的素数输入样例100输出样例...

网友评论

      本文标题:一个简单的etcdV3样例

      本文链接:https://www.haomeiwen.com/subject/tfcdaftx.html