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 }