本文整理汇总了Golang中github.com/coreos/go-etcd/etcd.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
示例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
}
示例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")
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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.")
}
}
}
示例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
}
示例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
}
示例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
}
示例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
}