From 8671b408847aab9fce029e25b581b8f12b73f2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B5=B7=E4=BB=B2=E5=AD=90?= Date: Sun, 24 Jan 2021 09:23:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=AE=E5=80=BC=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/main.go | 5 +++++ consul/consul-service.go | 31 ++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/client/main.go b/client/main.go index 04d62f5..d3ab784 100644 --- a/client/main.go +++ b/client/main.go @@ -12,6 +12,11 @@ import ( ) func main() { + v := []byte("asdfsdfdsf") + consul.KVPut("hello", &v, 0) + vv, _ := consul.KVGet("hello", 0) + fmt.Println(string(*vv)) + for { time.Sleep(10 * time.Millisecond) diff --git a/consul/consul-service.go b/consul/consul-service.go index d1f4b91..cc6d4e4 100644 --- a/consul/consul-service.go +++ b/consul/consul-service.go @@ -135,27 +135,28 @@ func CheckHeath(serviceid string) { fmt.Println(b) } -//KVTest test -func KVTest() { +//KVPut test +func KVPut(key string, values *[]byte, flags uint64) (*consulapi.WriteMeta, error) { client := getClient() if client == nil { - return + return nil, nil } - // KV, put值 - values := "test" - key := "go-consul-test/127.0.0.1:8100" - client.KV().Put(&consulapi.KVPair{Key: key, Flags: 0, Value: []byte(values)}, nil) + return client.KV().Put(&consulapi.KVPair{Key: key, Flags: flags, Value: *values}, nil) +} + +//KVGet 获取值 +func KVGet(key string, flags uint64) (*[]byte, error) { + client := getClient() + if client == nil { + return nil, nil + } // KV get值 data, _, _ := client.KV().Get(key, nil) - fmt.Println(string(data.Value)) - - // KV list - datas, _, _ := client.KV().List("go", nil) - for _, value := range datas { - fmt.Println(value) + if data != nil { + return &data.Value, nil } - keys, _, _ := client.KV().Keys("go", "", nil) - fmt.Println(keys) + + return nil, nil }