1. 程式人生 > >golang基礎-etcd介紹與使用、etcd存取值、etcd監測資料寫入

golang基礎-etcd介紹與使用、etcd存取值、etcd監測資料寫入

golang基礎-etcd介紹與使用、etcd存取值、etcd監測資料寫入

來自: Saflyer

原文:https://blog.csdn.net/u013210620/article/details/78608988

 

 

etcd介紹與使用

概念:高可用的分散式key-value儲存,可以用於配置共享和服務發現。 
類似專案:zookeeper和consul 
開發語言:Go 
介面:提供restful的http介面,使用簡單 
實現演算法:基於raft演算法的強一致性、高可用的服務儲存目錄

etcd搭建 
a. 下載etcd release版本:https://github.com/coreos/etcd/releases/ 
b. ./bin/etcd即可以啟動etcd 
c. 使用etcdctl工具更改配置

這裡寫圖片描述

etcd測試連結

package main

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

func main() {
    /*
        DialTimeout time.Duration `json:"dial-timeout"`
        Endpoints []string `json:"endpoints"`
    */
    cli, err := clientv3.New(clientv3.Config{
        Endpoints:   []string{"localhost:2379", "localhost:22379", "localhost:32379"},
        DialTimeout: 5 * time.Second,
    })
    if err != nil {
        fmt.Println("connect failed, err:", err)
        return
    }

    fmt.Println("connect succ")
    defer cli.Close()
}

輸出如下:

PS E:\golang\go_pro\src\safly> go run main.go
connect succ
PS E:\golang\go_pro\src\safly>

etcd存取值

package main

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

func main() {

    cli, err := clientv3.New(clientv3.Config{
        Endpoints:   []string{"localhost:2379", "localhost:22379", "localhost:32379"},
        DialTimeout: 5 * time.Second,
    })
    if err != nil {
        fmt.Println("connect failed, err:", err)
        return
    }

    fmt.Println("connect succ")
    defer cli.Close()
    //設定1秒超時,訪問etcd有超時控制
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    //操作etcd
    _, err = cli.Put(ctx, "/logagent/conf/", "sample_value")
    //操作完畢,取消etcd
    cancel()
    if err != nil {
        fmt.Println("put failed, err:", err)
        return
    }
    //取值,設定超時為1秒
    ctx, cancel = context.WithTimeout(context.Background(), time.Second)
    resp, err := cli.Get(ctx, "/logagent/conf/")
    cancel()
    if err != nil {
        fmt.Println("get failed, err:", err)
        return
    }
    for _, ev := range resp.Kvs {
        fmt.Printf("%s : %s\n", ev.Key, ev.Value)
    }
}

輸出如下:

PS E:\golang\go_pro\src\safly> go run example.go
connect succ
/logagent/conf/ : sample_value

etcd檢測Watch

package main

import (
    "context"
    "fmt"
    "time"

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

func main() {

    cli, err := clientv3.New(clientv3.Config{
        Endpoints:   []string{"localhost:2379", "localhost:22379", "localhost:32379"},
        DialTimeout: 5 * time.Second,
    })
    if err != nil {
        fmt.Println("connect failed, err:", err)
        return
    }

    fmt.Println("connect succ")
    defer cli.Close()

    cli.Put(context.Background(), "/logagent/conf/", "8888888")
    for {
        rch := cli.Watch(context.Background(), "/logagent/conf/")
        for wresp := range rch {
            for _, ev := range wresp.Events {
                fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
            }
        }
    }
}

這裡寫圖片描述