本文整理汇总了Golang中github.com/coreos/go-etcd/etcd.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: NewEtcd
func NewEtcd(prefixKey string, config *EtcdConfig) (*Etcd, error) {
var (
client *etcd.Client
err error
)
if config.CertFile != "" && config.KeyFile != "" {
if client, err = etcd.NewTLSClient(config.Machines, config.CertFile, config.KeyFile, config.CaFile); err != nil {
Logger.Error("Failed to connect to Etcd. Error: %+v.", err)
return nil, err
}
} else {
client = etcd.NewClient(config.Machines)
}
// Set the default value if not provided.
if config.EtcdConsistency == "" {
config.EtcdConsistency = etcd.STRONG_CONSISTENCY
}
if err = client.SetConsistency(config.EtcdConsistency); err != nil {
Logger.Error("Failed to set Etcd consitency. Error: %+v.", err)
return nil, err
}
return &Etcd{client: client, prefixKey: prefixKey}, nil
}
开发者ID:yonglehou,项目名称:maestro,代码行数:27,代码来源:etcd.go
示例2: RemoveService
func RemoveService(client *etcd.Client, s *Service) error {
basekey := appendPrefix(fmt.Sprintf("/beacon/registry/%s/haproxy/%s",
s.Cluster, s.Proto), s.Prefix)
var key string
if s.Proto == "tcp" {
key = fmt.Sprintf("%s/%s", basekey, s.Name)
} else {
key = fmt.Sprintf("%s/%s/backends/%s", basekey, s.Name, s.Backend)
}
log.WithFields(log.Fields{
"key": key,
}).Debugln("start to delete a service key.")
if _, err := client.Delete(key, true); err != nil {
return err
}
log.WithFields(log.Fields{
"name": s.Name,
"backend": s.Backend,
"proto": s.Proto,
}).Infoln("service unregistered.")
return nil
}
开发者ID:jmptrader,项目名称:beacon,代码行数:25,代码来源:service.go
示例3: acquire
func acquire(c *etcd.Client, key, value string, ttl uint64) (uint64, error) {
resp, err := c.Create(key, value, ttl)
if err != nil {
return 0, err
}
return resp.EtcdIndex, nil
}
开发者ID:joshie,项目名称:lochness,代码行数:7,代码来源:lock.go
示例4: cleanup
func cleanup(r *bytes.Buffer, e *etcd.Client, ep *testProcess, dp *testProcess) {
if r != nil {
log.Debug("Writing report")
rpath := testDir + "/report.txt"
if err := ioutil.WriteFile(rpath, r.Bytes(), 0644); err != nil {
log.WithFields(log.Fields{
"error": err,
"func": "ioutil.WriteFile",
"path": rpath,
}).Warning("Could not write report")
}
}
if dp != nil {
log.Debug("Exiting cdhcpd")
_ = dp.finish()
time.Sleep(time.Second)
}
if e != nil {
log.Debug("Clearing test data")
if _, err := e.Delete("/lochness", true); err != nil {
log.WithFields(log.Fields{
"error": err,
"func": "etcd.Delete",
}).Warning("Could not clear test-created data from etcd")
}
time.Sleep(time.Second)
}
if ep != nil {
log.Debug("Exiting etcd")
_ = ep.finish()
}
log.Info("Done")
}
开发者ID:joshie,项目名称:lochness,代码行数:33,代码来源:main.go
示例5: Acquire
//Acquire will attempt to acquire the lock, if blocking is set to true it will wait forever to do so.
//Setting blocking to false would be the equivalent of a fictional TryAcquire, an immediate return
//if locking fails.
func Acquire(c *etcd.Client, key, value string, ttl uint64, blocking bool) (*Lock, error) {
index := uint64(0)
var err error
tryAcquire := true
LOOP:
for {
if tryAcquire {
index, err = acquire(c, key, value, ttl)
if err == nil {
break LOOP
}
if !blocking {
return nil, err
}
tryAcquire = false
}
resp, err := c.Watch(key, 0, false, nil, nil)
if err != nil {
return nil, err
}
if resp.Action != "compareAndSwap" {
tryAcquire = true
}
}
return &Lock{
c: c,
key: key,
value: value,
ttl: ttl,
index: index,
held: true,
}, nil
}
开发者ID:joshie,项目名称:lochness,代码行数:36,代码来源:lock.go
示例6: NewEtcdClient
// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
var c *goetcd.Client
var err error
if cert != "" && key != "" {
c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
if err != nil {
return &Client{c}, err
}
} else {
c = goetcd.NewClient(machines)
}
// Configure the DialTimeout, since 1 second is often too short
c.SetDialTimeout(time.Duration(3) * time.Second)
maxConnectAttempts := 10
for attempt := 1; attempt <= maxConnectAttempts; attempt++ {
success := c.SetCluster(machines)
if success {
break
return &Client{c}, nil
}
if attempt == maxConnectAttempts {
break
return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
}
log.Info(fmt.Sprintf("[Attempt: %d] Attempting access to etcd after 5 second sleep", attempt))
time.Sleep(5 * time.Second)
}
return &Client{c}, nil
}
开发者ID:Lineberty,项目名称:confd,代码行数:34,代码来源:client.go
示例7: GetAddress
// getAddress will return the host:port address of the service taking care of
// the task that we want to talk to.
// Currently we grab the information from etcd every time. Local cache could be used.
// If it failed, e.g. network failure, it should return error.
func GetAddress(client *etcd.Client, name string, id uint64) (string, error) {
resp, err := client.Get(TaskMasterPath(name, id), false, false)
if err != nil {
return "", err
}
return resp.Node.Value, nil
}
开发者ID:yangjunpro,项目名称:taskgraph,代码行数:11,代码来源:task.go
示例8: MustCreate
func MustCreate(c *etcd.Client, logger *log.Logger, key, value string, ttl uint64) *etcd.Response {
resp, err := c.Create(key, value, ttl)
if err != nil {
logger.Panicf("Create failed. Key: %s, err: %v", key, err)
}
return resp
}
开发者ID:bikong2,项目名称:taskgraph,代码行数:7,代码来源:util.go
示例9: GetAndWatchEpoch
func GetAndWatchEpoch(client *etcd.Client, appname string, epochC chan uint64, stop chan bool) (uint64, error) {
resp, err := client.Get(EpochPath(appname), false, false)
if err != nil {
log.Fatal("etcdutil: can not get epoch from etcd")
}
ep, err := strconv.ParseUint(resp.Node.Value, 10, 64)
if err != nil {
return 0, err
}
receiver := make(chan *etcd.Response, 1)
go client.Watch(EpochPath(appname), resp.EtcdIndex+1, false, receiver, stop)
go func() {
for resp := range receiver {
if resp.Action != "compareAndSwap" && resp.Action != "set" {
continue
}
epoch, err := strconv.ParseUint(resp.Node.Value, 10, 64)
if err != nil {
log.Fatal("etcdutil: can't parse epoch from etcd")
}
epochC <- epoch
}
}()
return ep, nil
}
开发者ID:bikong2,项目名称:taskgraph,代码行数:26,代码来源:epoch.go
示例10: latestRunningVersion
// latestRunningVersion retrieves the highest version of the application published
// to etcd. If no app has been published, returns 0.
func latestRunningVersion(client *etcd.Client, appName string) int {
r := regexp.MustCompile(appNameRegex)
if client == nil {
// FIXME: client should only be nil during tests. This should be properly refactored.
if appName == "ceci-nest-pas-une-app" {
return 3
}
return 0
}
resp, err := client.Get(fmt.Sprintf("/deis/services/%s", appName), false, true)
if err != nil {
// no app has been published here (key not found) or there was an error
return 0
}
var versions []int
for _, node := range resp.Node.Nodes {
match := r.FindStringSubmatch(node.Key)
// account for keys that may not be an application container
if match == nil {
continue
}
version, err := strconv.Atoi(match[2])
if err != nil {
log.Println(err)
return 0
}
versions = append(versions, version)
}
return max(versions)
}
开发者ID:gpxl,项目名称:deis,代码行数:32,代码来源:publisher.go
示例11: dispatchCommand
func dispatchCommand(c *cli.Context, client *etcd.Client, cmd *command.Command) {
targets := c.StringSlice("target")
if targets == nil || len(targets) == 0 {
log.Warningln("no target set! try to send command to all registered host.")
targets = fetchHosts(c, client)
}
if targets == nil {
log.Fatalln("no target to send command.")
} else {
log.Infoln("send command to: ", targets)
}
for _, target := range targets {
key := fmt.Sprintf("/beacon/commands/single/%s/%s/",
target, cmd.Id)
if c.GlobalString("prefix") != "" {
key = fmt.Sprintf("/%s%s", strings.Trim(c.GlobalString("prefix"), "/"), key)
}
if _, err := client.Set(key, cmd.Marshal(), 0); err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatalln("send command failed.")
}
}
}
开发者ID:jmptrader,项目名称:beacon,代码行数:25,代码来源:utils.go
示例12: NewEtcdClient
func NewEtcdClient(addr string, dialTimeout time.Duration) (*EtcdClient, error) {
var c *api.Client
/*
var err error
if cert != "" && key != "" {
c, err = etcd.NewTLSClient(machines, cert, key, caCert)
if err != nil {
return &Client{c}, err
}
} else {
c = etcd.NewClient(machines)
}
*/
// machine addresses
machines := []string{addr}
// create custom client
c = api.NewClient(machines)
if !c.SetCluster(machines) {
return nil, errors.New("cannot connect to etcd cluster: " + addr)
}
// configure dial timeout
c.SetDialTimeout(dialTimeout)
return &EtcdClient{addr: addr, client: c}, nil
}
开发者ID:haoyixin,项目名称:parkeeper,代码行数:28,代码来源:etcdclient.go
示例13: addDNS
func addDNS(record string, service *kapi.Service, etcdClient *etcd.Client) error {
// if PortalIP is not set, a DNS entry should not be created
if !kapi.IsServiceIPSet(service) {
log.Printf("Skipping dns record for headless service: %s\n", service.Name)
return nil
}
for i := range service.Spec.Ports {
svc := skymsg.Service{
Host: service.Spec.PortalIP,
Port: service.Spec.Ports[i].Port,
Priority: 10,
Weight: 10,
Ttl: 30,
}
b, err := json.Marshal(svc)
if err != nil {
return err
}
// Set with no TTL, and hope that kubernetes events are accurate.
log.Printf("Setting DNS record: %v -> %s:%d\n", record, service.Spec.PortalIP, service.Spec.Ports[i].Port)
_, err = etcdClient.Set(skymsg.Path(record), string(b), uint64(0))
if err != nil {
return err
}
}
return nil
}
开发者ID:SivagnanamCiena,项目名称:calico-kubernetes,代码行数:29,代码来源:kube2sky.go
示例14: etcdClientSetUp
func etcdClientSetUp(t *testing.T, rawClient *etcd.Client) map[string]interface{} {
var err error
if _, err = rawClient.SetDir("test", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.Set("test/index", "1", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.SetDir("test/values", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.Set("test/values/a", "aye", 0); err != nil {
t.Fatal(err)
}
if _, err = rawClient.Set("test/values/b", "bee", 0); err != nil {
t.Fatal(err)
}
want := make(map[string]interface{})
wantValues := make(map[string]interface{})
wantValues["a"] = "aye"
wantValues["b"] = "bee"
want["values"] = wantValues
want["index"] = "1"
return want
}
开发者ID:BlueDragonX,项目名称:sentinel,代码行数:26,代码来源:client_test.go
示例15: SetKey
// SetKey is used to log key value pairs to stdout/etcd so that dray can pass
// them down to subsequent images as needed. By default keys are logged to
// stdout as ----BEGIN PANAMAX DATA----\nkey=value\n----END PANAMAX DATA----
// tags. If LOG_TO env var is set to etcd, then keys are logged to etcd.
// The etcd api is set using ETCD_API env variable.
func SetKey(key string, value string) error {
logTo := os.Getenv("LOG_TO")
if logTo == "" {
logTo = "stdout"
}
logTo = strings.ToLower(logTo)
if logTo == "etcd" {
log.Info("Logging Keys to etcd...")
var ec *etcd.Client
eIP := os.Getenv("ETCD_API")
if eIP == "" {
eIP = "172.17.42.1:4001"
}
eIP = fmt.Sprintf("http://%s", eIP)
ms := []string{eIP}
ec = etcd.NewClient(ms)
_, e := ec.Set(key, value, 0)
if e != nil {
return e
}
} else {
fmt.Printf("\n----BEGIN PANAMAX DATA----\n%s=%s\n----END PANAMAX DATA----\n", key, value)
}
return nil
}
开发者ID:rupakg,项目名称:kube-cluster-deploy,代码行数:32,代码来源:keys.go
示例16: EtcdMkDir
func EtcdMkDir(client *etcd.Client, path string, ttl uint64) error {
_, err := client.SetDir(path, ttl)
if err != nil {
return err
}
return nil
}
开发者ID:xingskycn,项目名称:duang,代码行数:7,代码来源:utils.go
示例17: CanConnect
// CanConnect checks a given SSH key against the list of authorized users in etcd
// to check if a user with a given key is allowed to connect. It takes in an active
// etcd.Client struct pointer and the ssh key to test and returns a username
// and boolean representing if they are allowed to connect.
func CanConnect(e *etcd.Client, sshkey string) (user string, allowed bool) {
reply, err := e.Get("/flitter/builder/users/", true, true)
if err != nil {
log.Printf("etcd: %s", err)
return "", false
}
keybit := strings.Split(sshkey, " ")[1]
fp := GetFingerprint(keybit)
for _, userdir := range reply.Node.Nodes {
for _, fpnode := range userdir.Nodes {
thisFpSplit := strings.Split(fpnode.Key, "/")
thisFp := thisFpSplit[len(thisFpSplit)-1]
if fp == thisFp {
userpath := strings.Split(userdir.Key, "/")
user := userpath[len(userpath)-1]
return user, true
}
}
}
return
}
开发者ID:mehulsbhatt,项目名称:flitter,代码行数:30,代码来源:etcdssh.go
示例18: getRuntimeConfFromEtcd
func getRuntimeConfFromEtcd(client *etcd.Client, etcd_path string) (*RuntimeConfig, error) {
resp, err := client.Get(etcd_path, false, false)
if err != nil {
return nil, fmt.Errorf("failed to get RuntimeConfig:%v", err)
}
r := bytes.NewReader([]byte(resp.Node.Value))
return ReadRuntimeConfig(r)
}
开发者ID:homingway,项目名称:hickwall,代码行数:8,代码来源:etcd_runtime_config.go
示例19: SetEtcd
// SetEtcd sets an array of values into a test etcd instance.
func SetEtcd(t *testing.T, keys []string, values []string, c *etcd.Client) {
for i, key := range keys {
_, err := c.Set(key, values[i], 0)
if err != nil {
t.Fatal(err)
}
}
}
开发者ID:ericcapricorn,项目名称:deis,代码行数:9,代码来源:etcdutils.go
示例20: AnnounceSite
func AnnounceSite(id string, site Site, client *etcd.Client) {
key := fmt.Sprintf("/nginx/sites/%s", id)
value := FormatSite(site)
client.Set(key, value, 60)
log.Println("announcing", id, "as", value)
time.Sleep(30 * time.Second)
go AnnounceSite(id, site, client)
}
开发者ID:lrolaz,项目名称:nginx-confd,代码行数:9,代码来源:app.go
注:本文中的github.com/coreos/go-etcd/etcd.Client类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论