本文整理汇总了Golang中github.com/hashicorp/consul/api.KV.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang KV.Get方法的具体用法?Golang KV.Get怎么用?Golang KV.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/hashicorp/consul/api.KV
的用法示例。
在下文中一共展示了KV.Get方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: storeData
func storeData(kv *api.KV, key string, value []byte) {
pair, _, err := kv.Get(key, nil)
if err != nil {
Error.Fatal("Failed reading from Consul for key:", key, "Error:", err)
}
if pair == nil || string(pair.Value) != string(value) {
var p *api.KVPair
if pair == nil {
p = &api.KVPair{Key: key, Value: value}
} else {
p = &api.KVPair{Key: key, Value: value, ModifyIndex: pair.ModifyIndex}
}
success, meta, err := kv.CAS(p, nil)
if err != nil {
Error.Fatal(err)
}
if !success {
storeData(kv, key, value)
} else {
Trace.Print("Request took: ", meta.RequestTime.Seconds(), " seconds")
Info.Print("Updated key '", key, "' to '", string(value), "'")
}
} else {
Trace.Print("not updating key", key)
}
}
示例2: keyExists
func keyExists(key string, kv *consul.KV) bool {
kp, _, err := kv.Get(key, nil)
if err != nil {
log.Warnf("Could not get key path %s: %v", key, err)
return false
}
return kp != nil
}
示例3: fetchConsul
func fetchConsul(kv *consul.KV, env string, key string) (value string, conf_err error) {
pair, _, _ := kv.Get("go-sms/"+env+"/"+key, nil)
if pair == nil {
conf_err = errors.New("Failed to fetch \"" + key + "\" key for you environment. Leave default: " + defaults[key])
value = defaults[key]
return
}
value = string(pair.Value)
return
}
示例4: getSequencer
// getSequencer gets the lockindex, session id and the key which constitutes the sequencer for the current lock.
func getSequencer(kv *api.KV, key string) (*sequencer, error) {
kvPair, _, err := kv.Get(key, nil)
if err != nil {
log.Println("Can't get the sequencer")
return nil, err
}
if kvPair == nil {
return nil, nil
}
seq := sequencer{}
seq.lockIndex = kvPair.LockIndex
seq.session = kvPair.Session
seq.key = kvPair.Key
return &seq, nil
}
示例5: NewConsulProvider
keysPairs, _, err := kvClient.Keys(joinedKeys, "/", nil)
if err != nil {
log.Error("Error getting keys ", joinedKeys, err)
return nil
}
keysPairs = fun.Filter(func(key string) bool {
if key == joinedKeys {
return false
}
return true
}, keysPairs).([]string)
return keysPairs
},
"Get": func(keys ...string) string {
joinedKeys := strings.Join(keys, "")
keyPair, _, err := kvClient.Get(joinedKeys, nil)
if err != nil {
log.Error("Error getting key ", joinedKeys, err)
return ""
}
return string(keyPair.Value)
},
"Last": func(key string) string {
splittedKey := strings.Split(key, "/")
return splittedKey[len(splittedKey)-2]
},
}
func NewConsulProvider() *ConsulProvider {
consulProvider := new(ConsulProvider)
// default values
示例6: consulGet
func consulGet(kv *api.KV, key string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
return kv.Get(key, &api.QueryOptions{WaitIndex: lastIndex})
}