键值存储

This commit is contained in:
suguo.yao 2021-01-24 09:23:11 +08:00
parent d0ca855fdc
commit 8671b40884
2 changed files with 21 additions and 15 deletions

View File

@ -12,6 +12,11 @@ import (
) )
func main() { func main() {
v := []byte("asdfsdfdsf")
consul.KVPut("hello", &v, 0)
vv, _ := consul.KVGet("hello", 0)
fmt.Println(string(*vv))
for { for {
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)

View File

@ -135,27 +135,28 @@ func CheckHeath(serviceid string) {
fmt.Println(b) fmt.Println(b)
} }
//KVTest test //KVPut test
func KVTest() { func KVPut(key string, values *[]byte, flags uint64) (*consulapi.WriteMeta, error) {
client := getClient() client := getClient()
if client == nil { if client == nil {
return return nil, nil
} }
// KV, put值 return client.KV().Put(&consulapi.KVPair{Key: key, Flags: flags, Value: *values}, nil)
values := "test" }
key := "go-consul-test/127.0.0.1:8100"
client.KV().Put(&consulapi.KVPair{Key: key, Flags: 0, Value: []byte(values)}, nil) //KVGet 获取值
func KVGet(key string, flags uint64) (*[]byte, error) {
client := getClient()
if client == nil {
return nil, nil
}
// KV get值 // KV get值
data, _, _ := client.KV().Get(key, nil) data, _, _ := client.KV().Get(key, nil)
fmt.Println(string(data.Value)) if data != nil {
return &data.Value, nil
// KV list
datas, _, _ := client.KV().List("go", nil)
for _, value := range datas {
fmt.Println(value)
} }
keys, _, _ := client.KV().Keys("go", "", nil)
fmt.Println(keys) return nil, nil
} }