溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

go任務(wù)調(diào)度5(go操作etcd)

發(fā)布時(shí)間:2020-06-18 20:14:01 來(lái)源:網(wǎng)絡(luò) 閱讀:681 作者:梁十八 欄目:編程語(yǔ)言

連接etcd:

package main

import (
    "fmt"
    "go.etcd.io/etcd/clientv3"
    "time"
)

var (
    config clientv3.Config
    client *clientv3.Client
    err error
)

func main()  {
    //客戶端配置
    config = clientv3.Config{
        Endpoints: []string{"0.0.0.0:2379"}, //集群列表
        DialTimeout: 5 * time.Second,
    }
    //建立連接
    if client, err = clientv3.New(config); err != nil {
        fmt.Println(err)
        return
    }
    client = client
}

k-v操作:

package main

import (
    "context"
    "fmt"
    "go.etcd.io/etcd/clientv3"
    //"github.com/coreos/etcd/clientv3"
    "time"
)

var (
    config  clientv3.Config
    client  *clientv3.Client
    err     error
    kv      clientv3.KV
    putResp *clientv3.PutResponse
)

func main() {
    //客戶端配置
    config = clientv3.Config{
        Endpoints:   []string{"0.0.0.0:2379"}, //集群列表
        DialTimeout: 5 * time.Second,
    }
    //建立客戶端
    if client, err = clientv3.New(config); err != nil {
        fmt.Println(err)
        return
    }
    //操作k-v(用于讀寫(xiě)etcd的鍵值對(duì))
    kv = clientv3.NewKV(client)
    //context.TODO()代表什么都不做,占位就可以了
    //不在這里指定clientv3.WithPrevKV(),則不能得到putResp.PrevKv.Key,putResp.PrevKv.Value
    if putResp, err = kv.Put(context.TODO(), "/cron/jobs/job1", "world", clientv3.WithPrevKV()); err != nil {
        fmt.Println(err)
    } else {
        //輸出版本號(hào)
        fmt.Println("Revision:", putResp.Header.Revision)
        if putResp.PrevKv != nil {
            //輸出上一個(gè)k-v
            fmt.Println(string(putResp.PrevKv.Key), ":", string(putResp.PrevKv.Value))
        }
    }
}

運(yùn)行輸出

[root@bogon etcd]# go run demo2.go
Revision: 19
/cron/jobs/job1 : hello
查看:
[root@bogon etcd-v3.3.12-linux-amd64]# ETCDCTL_API=3 ./etcdctl get "/cron/jobs/job1"
/cron/jobs/job1
world


get:

package main

import (
    "context"
    "fmt"
    "go.etcd.io/etcd/clientv3"
    //"github.com/coreos/etcd/clientv3"
    "time"
)

func main() {
    var (
        config  clientv3.Config
        client  *clientv3.Client
        err     error
        kv      clientv3.KV
        getResp *clientv3.GetResponse
    )

    //客戶端配置
    config = clientv3.Config{
        Endpoints:   []string{"0.0.0.0:2379"}, //集群列表
        DialTimeout: 5 * time.Second,
    }
    //建立客戶端
    if client, err = clientv3.New(config); err != nil {
        fmt.Println(err)
        return
    }
    //用于讀寫(xiě)etcd的鍵值對(duì)
    kv = clientv3.NewKV(client)

    if getResp, err = kv.Get(context.TODO(), "/cron/jobs/job1"); err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(getResp.Kvs)
    }
}

[root@bogon etcd]# go run demo3.go
[key:"/cron/jobs/job1" create_revision:4 mod_revision:21 version:16 value:"world" ]
(create_revision:創(chuàng)建版本。mod_revision:修改版本。version:自從創(chuàng)建以來(lái)修改的次數(shù)。)

go任務(wù)調(diào)度5(go操作etcd)
(敲with列出所有選項(xiàng))


獲取指定“目錄”下的kvs:

package main

import (
    "context"
    "fmt"
    "go.etcd.io/etcd/clientv3"
    //"github.com/coreos/etcd/clientv3"
    "time"
)

func main() {
    var (
        config  clientv3.Config
        client  *clientv3.Client
        err     error
        kv      clientv3.KV
        getResp *clientv3.GetResponse
    )

    //客戶端配置
    config = clientv3.Config{
        Endpoints:   []string{"0.0.0.0:2379"}, //集群列表
        DialTimeout: 5 * time.Second,
    }
    //建立客戶端
    if client, err = clientv3.New(config); err != nil {
        fmt.Println(err)
        return
    }
    //用于讀寫(xiě)etcd的鍵值對(duì)
    kv = clientv3.NewKV(client)

    //讀取/cron/jobs/為前綴的所有key
    if getResp, err = kv.Get(context.TODO(), "/cron/jobs/", clientv3.WithPrefix()); err != nil {
        fmt.Println(err)
    } else { //獲取成功,我們遍歷所有kvs
        fmt.Println(getResp.Kvs)
    }
}

[root@bogon etcd]# go run demo4.go
[key:"/cron/jobs/job1" create_revision:4 mod_revision:21 version:16 value:"world" key:"/cron/jobs/job2" create_revision:5 mod_revision:6 version:2 value:"changed" ]


刪除:

package main

import (
    "context"
    "fmt"
    "go.etcd.io/etcd/clientv3"
    "go.etcd.io/etcd/mvcc/mvccpb"

    //"github.com/coreos/etcd/clientv3"
    "time"
)

func main() {
    var (
        config  clientv3.Config
        client  *clientv3.Client
        err     error
        kv      clientv3.KV
        delResp *clientv3.DeleteResponse
        //idx int
        kvpair *mvccpb.KeyValue
    )

    //客戶端配置
    config = clientv3.Config{
        Endpoints:   []string{"0.0.0.0:2379"}, //集群列表
        DialTimeout: 5 * time.Second,
    }
    //建立客戶端
    if client, err = clientv3.New(config); err != nil {
        fmt.Println(err)
        return
    }
    //用于讀寫(xiě)etcd的鍵值對(duì)
    kv = clientv3.NewKV(client)

    //刪除kv
    if delResp, err = kv.Delete(context.TODO(), "/cron/jobs/job2/", clientv3.WithPrevKV()); err != nil {
        fmt.Println(err)
        return
    }

    //被刪除之前的value是什么
    if len(delResp.PrevKvs) != 0 { //必須有clientv3.WithPrevKV()才能獲取delResp.PrevKvs
        for _, kvpair = range delResp.PrevKvs {
            fmt.Println("刪除了key:", string(kvpair.Key), " value:", string(kvpair.Value))
        }
    }
}

[root@bogon etcd]# go run demo5.go
刪除了key: /cron/jobs/job2 value: changed
再次運(yùn)行什么也不輸出,因?yàn)橐呀?jīng)刪除了:
[root@bogon etcd]# go run demo5.go
[root@bogon etcd]#
如果要?jiǎng)h除以/cron/jobs/job2/“目錄”開(kāi)頭的所有kv,則將clientv3.WithPrevKV()改為:clientv3.WithPrefix()。
clientv3.WithFromKey()則是刪除以此開(kāi)頭的所有kv(kv存儲(chǔ)是有序的)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI