本文整理汇总了Golang中github.com/hashicorp/consul/api.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: NewConsulClient
func NewConsulClient(config *consulAPI.Config) (KVClient, error) {
var (
c *consulAPI.Client
err error
)
if config != nil {
c, err = consulAPI.NewClient(config)
} else {
c, err = consulAPI.NewClient(consulAPI.DefaultConfig())
}
if err != nil {
return nil, err
}
maxRetries := 30
i := 0
for {
leader, err := c.Status().Leader()
if err != nil || leader == "" {
log.Info("Waiting for consul client to be ready...")
time.Sleep(2 * time.Second)
i++
if i > maxRetries {
e := fmt.Errorf("Unable to contact consul: %s", err)
log.Error(e)
return nil, e
}
} else {
log.Info("Consul client ready")
break
}
}
return &ConsulClient{c}, nil
}
开发者ID:cilium-team,项目名称:cilium,代码行数:33,代码来源:consul.go
示例2: SetUpSuite
func (s *ConsulCatalogSuite) SetUpSuite(c *check.C) {
dockerHost := os.Getenv("DOCKER_HOST")
if dockerHost == "" {
// FIXME Handle windows -- see if dockerClient already handle that or not
dockerHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket)
}
// Make sure we can speak to docker
dockerClient, err := docker.NewClient(dockerHost)
c.Assert(err, checker.IsNil, check.Commentf("Error connecting to docker daemon"))
s.dockerClient = dockerClient
s.createComposeProject(c, "consul_catalog")
err = s.composeProject.Up()
c.Assert(err, checker.IsNil, check.Commentf("Error starting project"))
consul, err := s.GetContainer("integration-test-consul_catalog_consul_1")
c.Assert(err, checker.IsNil, check.Commentf("Error finding consul container"))
s.consulIP = consul.NetworkSettings.IPAddress
config := api.DefaultConfig()
config.Address = s.consulIP + ":8500"
consulClient, err := api.NewClient(config)
if err != nil {
c.Fatalf("Error creating consul client")
}
s.consulClient = consulClient
// Wait for consul to elect itself leader
time.Sleep(2000 * time.Millisecond)
}
开发者ID:tayzlor,项目名称:traefik,代码行数:30,代码来源:consul_catalog_test.go
示例3: consulFactory
func consulFactory(conf map[string]string) (Client, error) {
path, ok := conf["path"]
if !ok {
return nil, fmt.Errorf("missing 'path' configuration")
}
config := consulapi.DefaultConfig()
if token, ok := conf["access_token"]; ok && token != "" {
config.Token = token
}
if addr, ok := conf["address"]; ok && addr != "" {
config.Address = addr
}
if scheme, ok := conf["scheme"]; ok && scheme != "" {
config.Scheme = scheme
}
client, err := consulapi.NewClient(config)
if err != nil {
return nil, err
}
return &ConsulClient{
Client: client,
Path: path,
}, nil
}
开发者ID:jrperritt,项目名称:terraform,代码行数:27,代码来源:consul.go
示例4: NewConsulBackend
func NewConsulBackend(address string) (Backend, error) {
if address == "" {
address = "http://127.0.0.1:8500/vault"
}
url, err := url.Parse(address)
if err != nil {
return nil, maskAny(err)
}
path := url.Path
// Ensure path is suffixed but not prefixed
if !strings.HasSuffix(path, "/") {
path += "/"
}
if strings.HasPrefix(path, "/") {
path = strings.TrimPrefix(path, "/")
}
consulConf := api.DefaultConfig()
consulConf.Address = url.Host
client, err := api.NewClient(consulConf)
if err != nil {
return nil, maskAny(err)
}
return &consulBackend{
path: path,
client: client,
kv: client.KV(),
}, nil
}
开发者ID:pulcy,项目名称:vault-monkey,代码行数:32,代码来源:consul_backend.go
示例5: createAPIClient
func (c *Consul) createAPIClient() (*api.Client, error) {
config := api.DefaultConfig()
if c.Address != "" {
config.Address = c.Address
}
if c.Scheme != "" {
config.Scheme = c.Scheme
}
if c.Datacentre != "" {
config.Datacenter = c.Datacentre
}
if c.Username != "" {
config.HttpAuth = &api.HttpBasicAuth{
Username: c.Username,
Password: c.Password,
}
}
tlsCfg, err := internal.GetTLSConfig(
c.SSLCert, c.SSLKey, c.SSLCA, c.InsecureSkipVerify)
if err != nil {
return nil, err
}
config.HttpClient.Transport = &http.Transport{
TLSClientConfig: tlsCfg,
}
return api.NewClient(config)
}
开发者ID:jeichorn,项目名称:telegraf,代码行数:35,代码来源:consul.go
示例6: backupAcls
func backupAcls(ipaddress string, token string, outfile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
client, _ := api.NewClient(config)
acl := client.ACL()
tokens, _, err := acl.List(nil)
if err != nil {
panic(err)
}
// sort.Sort(ByCreateIndex(tokens))
outstring := ""
for _, element := range tokens {
// outstring += fmt.Sprintf("%s:%s:%s:%s\n", element.ID, element.Name, element.Type, element.Rules)
outstring += fmt.Sprintf("====\nID: %s\nName: %s\nType: %s\nRules:\n%s\n", element.ID, element.Name, element.Type, element.Rules)
}
file, err := os.Create(outfile)
if err != nil {
panic(err)
}
if _, err := file.Write([]byte(outstring)[:]); err != nil {
panic(err)
}
}
开发者ID:blairham,项目名称:consul-backup,代码行数:30,代码来源:main.go
示例7: NewConsulClient
func NewConsulClient() (ConsulClient, error) {
client, err := consul.NewClient(consul.DefaultConfig())
if err != nil {
return ConsulClient{}, err
}
return ConsulClient{client: client}, nil
}
开发者ID:waterlink,项目名称:pencil-go,代码行数:7,代码来源:consul.go
示例8: Sources
// Sources implements the TargetProvider interface.
func (cd *ConsulDiscovery) Sources() []string {
clientConf := *cd.clientConf
clientConf.HttpClient = &http.Client{Timeout: 5 * time.Second}
client, err := consul.NewClient(&clientConf)
if err != nil {
// NewClient always returns a nil error.
panic(fmt.Errorf("discovery.ConsulDiscovery.Sources: %s", err))
}
srvs, _, err := client.Catalog().Services(nil)
if err != nil {
log.Errorf("Error refreshing service list: %s", err)
return nil
}
cd.mu.Lock()
defer cd.mu.Unlock()
srcs := make([]string, 0, len(srvs))
for name := range srvs {
if _, ok := cd.scrapedServices[name]; ok {
srcs = append(srcs, consulSourcePrefix+":"+name)
}
}
return srcs
}
开发者ID:hustbill,项目名称:prometheus,代码行数:27,代码来源:consul.go
示例9: restoreKv
/* File needs to be in the following format:
KEY1:VALUE1
KEY2:VALUE2
*/
func restoreKv(ipaddress string, token string, infile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
data, err := ioutil.ReadFile(infile)
if err != nil {
panic(err)
}
client, _ := api.NewClient(config)
kv := client.KV()
for _, element := range strings.Split(string(data), "\n") {
split := strings.Split(element, ":")
key := strings.Join(split[:len(split)-1], ":")
value := split[len(split)-1]
if key != "" {
decoded_value, decode_err := base64.StdEncoding.DecodeString(value)
if decode_err != nil {
panic(decode_err)
}
p := &api.KVPair{Key: key, Value: decoded_value}
_, err := kv.Put(p, nil)
if err != nil {
panic(err)
}
}
}
}
开发者ID:victortrac,项目名称:consul-backup,代码行数:37,代码来源:main.go
示例10: NewDiscovery
// NewDiscovery returns a new Discovery for the given config.
func NewDiscovery(conf *config.ConsulSDConfig) (*Discovery, error) {
clientConf := &consul.Config{
Address: conf.Server,
Scheme: conf.Scheme,
Datacenter: conf.Datacenter,
Token: conf.Token,
HttpAuth: &consul.HttpBasicAuth{
Username: conf.Username,
Password: conf.Password,
},
}
client, err := consul.NewClient(clientConf)
if err != nil {
return nil, err
}
cd := &Discovery{
client: client,
clientConf: clientConf,
tagSeparator: conf.TagSeparator,
watchedServices: conf.Services,
}
// If the datacenter isn't set in the clientConf, let's get it from the local Consul agent
// (Consul default is to use local node's datacenter if one isn't given for a query).
if clientConf.Datacenter == "" {
info, err := client.Agent().Self()
if err != nil {
return nil, err
}
cd.clientDatacenter = info["Config"]["Datacenter"].(string)
} else {
cd.clientDatacenter = clientConf.Datacenter
}
return cd, nil
}
开发者ID:tommyulfsparre,项目名称:prometheus,代码行数:35,代码来源:consul.go
示例11: NewSyncer
// NewSyncer returns a new consul.Syncer
func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logger *log.Logger) (*Syncer, error) {
var consulClientConfig *consul.Config
var err error
consulClientConfig, err = consulConfig.ApiConfig()
if err != nil {
return nil, err
}
var consulClient *consul.Client
if consulClient, err = consul.NewClient(consulClientConfig); err != nil {
return nil, err
}
consulSyncer := Syncer{
client: consulClient,
logger: logger,
consulAvailable: true,
shutdownCh: shutdownCh,
servicesGroups: make(map[ServiceDomain]map[ServiceKey]*consul.AgentServiceRegistration),
checkGroups: make(map[ServiceDomain]map[ServiceKey][]*consul.AgentCheckRegistration),
trackedServices: make(map[consulServiceID]*consul.AgentServiceRegistration),
trackedChecks: make(map[consulCheckID]*consul.AgentCheckRegistration),
checkRunners: make(map[consulCheckID]*CheckRunner),
periodicCallbacks: make(map[string]types.PeriodicCallback),
}
return &consulSyncer, nil
}
开发者ID:paultyng,项目名称:terraform,代码行数:28,代码来源:syncer.go
示例12: Get
func (consulSource *ConsulSource) Get() (map[string]interface{}, error) {
config := consul.DefaultConfig()
config.Address = consulSource.Address
if consulSource.Scheme != "" {
config.Scheme = consulSource.Scheme
}
client, err := consul.NewClient(config)
if err != nil {
return nil, err
}
pairs, _, err := client.KV().List(consulSource.Prefix, nil)
if err != nil {
return nil, err
}
result := make(map[string]interface{})
for _, pair := range pairs {
parts := strings.Split(pair.Key, "/")
result[parts[len(parts)-1]] = string(pair.Value)
}
return result, nil
}
开发者ID:zeroturnaround,项目名称:configo,代码行数:27,代码来源:consul.go
示例13: NewSource
func NewSource(opts ...config.SourceOption) config.Source {
options := config.SourceOptions{
Name: DefaultPath,
}
for _, o := range opts {
o(&options)
}
// use default config
config := api.DefaultConfig()
// check if there are any addrs
if len(options.Hosts) > 0 {
addr, port, err := net.SplitHostPort(options.Hosts[0])
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
port = "8500"
addr = options.Hosts[0]
config.Address = fmt.Sprintf("%s:%s", addr, port)
} else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port)
}
}
// create the client
client, _ := api.NewClient(config)
return &consul{
addr: config.Address,
opts: options,
client: client,
}
}
开发者ID:ZhuJingfa,项目名称:go-platform,代码行数:33,代码来源:consul.go
示例14: Provide
func (provider *ConsulProvider) Provide(configurationChan chan<- *Configuration) {
config := &api.Config{
Address: provider.Endpoint,
Scheme: "http",
HttpClient: http.DefaultClient,
}
consulClient, _ := api.NewClient(config)
provider.consulClient = consulClient
if provider.Watch {
var waitIndex uint64
keypairs, meta, err := consulClient.KV().Keys("", "", nil)
if keypairs == nil && err == nil {
log.Error("Key was not found.")
}
waitIndex = meta.LastIndex
go func() {
for {
opts := api.QueryOptions{
WaitIndex: waitIndex,
}
keypairs, meta, err := consulClient.KV().Keys("", "", &opts)
if keypairs == nil && err == nil {
log.Error("Key was not found.")
}
waitIndex = meta.LastIndex
configuration := provider.loadConsulConfig()
if configuration != nil {
configurationChan <- configuration
}
}
}()
}
configuration := provider.loadConsulConfig()
configurationChan <- configuration
}
开发者ID:hotelzululima,项目名称:traefik,代码行数:35,代码来源:consul.go
示例15: verifyConsulUp
func verifyConsulUp(timeout string) error {
timeoutDur, err := time.ParseDuration(timeout)
if err != nil {
return err
}
if timeoutDur == 0 {
return nil
}
config := api.DefaultConfig()
config.Token = *consulToken
client, err := api.NewClient(config)
if err != nil {
return util.Errorf("Could not construct consul client: '%s'", err)
}
consulIsUp := make(chan struct{})
go func() {
for {
time.Sleep(200 * time.Millisecond)
err := Ping(client)
if err == nil {
consulIsUp <- struct{}{}
return
}
}
}()
select {
case <-time.After(timeoutDur):
return util.Errorf("Consul did not start or was not available after %v", timeoutDur)
case <-consulIsUp:
return nil
}
}
开发者ID:petertseng,项目名称:p2,代码行数:33,代码来源:bootstrap.go
示例16: NewConsulLocker
func NewConsulLocker(cfg map[string]string) (*ConsulLocker, error) {
config := initializeConsulConfig(cfg)
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
name, _ := os.Hostname()
var ttl = api.DefaultSemaphoreSessionTTL
if cfg["ttl"] != "" {
ttl = cfg["ttl"]
}
s, err := client.SemaphoreOpts(&api.SemaphoreOptions{
Prefix: filepath.Join("dropship/services/", cfg["prefix"]),
Limit: 1,
SessionTTL: ttl,
SessionName: name,
})
if err != nil {
return nil, err
}
l := &ConsulLocker{s}
return l, nil
}
开发者ID:ChrisMcKenzie,项目名称:dropship,代码行数:30,代码来源:locker_consul.go
示例17: roleList
func roleList(c cli.Command) {
client, _ := api.NewClient(api.DefaultConfig())
agent := client.Agent()
services, err := agent.Services()
if err != nil {
log.Fatalln("err: ", err)
}
self, err := agent.Self()
if err != nil {
log.Fatalln("err: ", err)
}
for _, service := range services {
if service.Service == "cascade" {
fmt.Println(self["Config"]["NodeName"], self["Config"]["AdvertiseAddr"].(string)+":")
for _, role := range service.Tags {
fmt.Println(" -", role)
}
}
}
}
开发者ID:edwardt,项目名称:cascade,代码行数:25,代码来源:role.go
示例18: serviceList
func serviceList(c cli.Command) {
client, _ := api.NewClient(api.DefaultConfig())
catalog := client.Catalog()
services, meta, err := catalog.Services(nil)
if err != nil {
log.Fatalln("Err:", err)
}
if meta.LastIndex == 0 {
log.Fatalln("Bad: ", meta)
}
sorted := make([]string, 0)
for index, _ := range services {
sorted = append(sorted, index)
}
sort.Strings(sorted)
for _, service := range sorted {
fmt.Println(" -", service)
}
}
开发者ID:jen20,项目名称:cascade,代码行数:26,代码来源:service.go
示例19: NewConsulDiscovery
// NewConsulDiscovery returns a new ConsulDiscovery for the given config.
func NewConsulDiscovery(conf *config.ConsulSDConfig) *ConsulDiscovery {
clientConf := &consul.Config{
Address: conf.Server,
Scheme: conf.Scheme,
Datacenter: conf.Datacenter,
Token: conf.Token,
HttpAuth: &consul.HttpBasicAuth{
Username: conf.Username,
Password: conf.Password,
},
}
client, err := consul.NewClient(clientConf)
if err != nil {
// NewClient always returns a nil error.
panic(fmt.Errorf("discovery.NewConsulDiscovery: %s", err))
}
cd := &ConsulDiscovery{
client: client,
clientConf: clientConf,
tagSeparator: conf.TagSeparator,
runDone: make(chan struct{}),
srvsDone: make(chan struct{}, 1),
scrapedServices: map[string]struct{}{},
services: map[string]*consulService{},
}
for _, name := range conf.Services {
cd.scrapedServices[name] = struct{}{}
}
return cd
}
开发者ID:hustbill,项目名称:prometheus,代码行数:31,代码来源:consul.go
示例20: lookupConsulServices
func lookupConsulServices() ([]consulapi.CatalogService, error) {
// Get a consul client
client, err := consulapi.NewClient(consulapi.DefaultConfig())
if err != nil {
return []consulapi.CatalogService{}, err
}
// Lookup catalog of all service names
catalog := client.Catalog()
services, _, err := catalog.Services(nil)
if err != nil {
return []consulapi.CatalogService{}, err
}
// Find all instances of each service
output := []consulapi.CatalogService{}
fmt.Println("consul services:")
for serviceName, _ := range services {
catalogServices, _, err := catalog.Service(serviceName, "", nil)
if err != nil {
return []consulapi.CatalogService{}, err
}
fmt.Println(serviceName)
for _, cs := range catalogServices {
output = append(output, *cs)
fmt.Printf("%#v\n", cs)
}
}
return output, nil
}
开发者ID:nathanleiby,项目名称:consul-service-pulse,代码行数:31,代码来源:main.go
注:本文中的github.com/hashicorp/consul/api.NewClient函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论