键值存储

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() {
v := []byte("asdfsdfdsf")
consul.KVPut("hello", &v, 0)
vv, _ := consul.KVGet("hello", 0)
fmt.Println(string(*vv))
for {
time.Sleep(10 * time.Millisecond)

View File

@ -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
}