本文整理汇总了Golang中github.com/coreos/go-etcd/etcd.NewTLSClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewTLSClient函数的具体用法?Golang NewTLSClient怎么用?Golang NewTLSClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewTLSClient函数的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: 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
}
示例3: New
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
urls := make([]string, 0)
if uri.Host != "" {
urls = append(urls, f.Scheme+"://"+uri.Host)
} else {
urls = append(urls, f.Scheme+"://127.0.0.1:4001")
}
if f.Scheme == "https" {
cert := os.Getenv("ETCD_CERTFILE")
key := os.Getenv("ETCD_KEYFILE")
caCert := os.Getenv("ETCD_CAFILE")
client, err := etcd2.NewTLSClient(urls, cert, key, caCert)
if err != nil {
log.Fatal("etcd: error creating tls client", err)
}
return &EtcdAdapter{client2: client, path: uri.Path}
}
res, err := http.Get(urls[0] + "/version")
if err != nil {
log.Fatal("etcd: error retrieving version", err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if match, _ := regexp.Match("0\\.4\\.*", body); match == true {
log.Println("etcd: using v0 client")
return &EtcdAdapter{client: etcd.NewClient(urls), path: uri.Path}
}
return &EtcdAdapter{client2: etcd2.NewClient(urls), path: uri.Path}
}
示例4: newEtcdClient
func newEtcdClient(c *EtcdConfig) (*etcd.Client, error) {
if c.Keyfile != "" || c.Certfile != "" || c.CAFile != "" {
return etcd.NewTLSClient(c.Endpoints, c.Certfile, c.Keyfile, c.CAFile)
} else {
return etcd.NewClient(c.Endpoints), nil
}
}
示例5: Client
func Client() *etcd.Client {
clientSingletonO.Do(func() {
host := "http://localhost:4001"
if len(os.Getenv("ETCD_HOST")) != 0 {
host = os.Getenv("ETCD_HOST")
}
cacert := os.Getenv("ETCD_CACERT")
tlskey := os.Getenv("ETCD_TLS_KEY")
tlscert := os.Getenv("ETCD_TLS_CERT")
if len(cacert) != 0 && len(tlskey) != 0 && len(tlscert) != 0 {
if !strings.Contains(host, "https://") {
host = strings.Replace(host, "http", "https", 1)
}
c, err := etcd.NewTLSClient([]string{host}, tlscert, tlskey, cacert)
if err != nil {
panic(err)
}
clientSingleton = c
} else {
clientSingleton = etcd.NewClient([]string{host})
}
})
return clientSingleton
}
示例6: New
func (f *Factory) New(uri *url.URL) bridge.RegistryAdapter {
urls := make([]string, 0)
if uri.Host != "" {
urls = append(urls, f.Scheme+"://"+uri.Host)
} else {
urls = append(urls, f.Scheme+"://127.0.0.1:4001")
}
if len(uri.Path) < 2 {
log.Fatal("skydns2: dns domain required e.g.: skydns2://<host>/<domain>")
}
if f.Scheme == "https" {
cert := os.Getenv("ETCD_CERTFILE")
key := os.Getenv("ETCD_KEYFILE")
caCert := os.Getenv("ETCD_CAFILE")
client, err := etcd.NewTLSClient(urls, cert, key, caCert)
if err != nil {
log.Fatal("etcd: error creating tls client", err)
}
return &Skydns2Adapter{client: client, path: domainPath(uri.Path[1:])}
}
return &Skydns2Adapter{client: etcd.NewClient(urls), path: domainPath(uri.Path[1:])}
}
示例7: newEtcdLockClient
func newEtcdLockClient(e *EtcdLock) (utils.Registry, error) {
var c utils.Registry
// if all are set, try to use tls
if *e.CertFile != "" && *e.KeyFile != "" && *e.CAFile != "" {
return etcd.NewTLSClient(*e.Members, *e.CertFile, *e.KeyFile, *e.CAFile)
}
c = etcd.NewClient(*e.Members)
return c, nil
}
示例8: NewClient
func NewClient(machines []string, cert, key, ca string) (client *Client, err error) {
client = &Client{}
if cert != "" && key != "" && ca != "" {
if client.Client, err = etcd.NewTLSClient(machines, cert, key, ca); err != nil {
return
}
} else {
client.Client = etcd.NewClient(machines)
}
return
}
示例9: NewEtcdClient
func NewEtcdClient() (client *etcd.Client) {
if etcdClient != nil {
return etcdClient
}
tlsEnabled := os.Getenv("ETCDCTL_TLS")
tls := len(tlsEnabled) > 0
certFile := os.Getenv("ETCDCTL_CERT_FILE")
keyFile := os.Getenv("ETCDCTL_KEY_FILE")
caFile := os.Getenv("ETCDCTL_TRUSTED_CA_FILE")
var err error
peersStr := os.Getenv("ETCDCTL_PEERS")
if len(peersStr) > 0 {
log.Println("Connecting to etcd peers : " + peersStr)
peers := strings.Split(peersStr, ",")
if tls {
etcdClient, err = etcd.NewTLSClient(peers, certFile, keyFile, caFile)
if err != nil {
log.Fatal(err)
}
} else {
etcdClient = etcd.NewClient(peers)
}
} else {
if tls {
etcdClient, err = etcd.NewTLSClient(nil, certFile, keyFile, caFile)
if err != nil {
log.Fatal(err)
}
} else {
etcdClient = etcd.NewClient(nil)
}
}
return etcdClient
}
示例10: NewClient
//新建注册中心客户端
func (c *Conn) NewClient() (result *registry, err error) {
var client *etcd.Client
if c.Tls == true {
//https conn
client, err = etcd.NewTLSClient(c.Hosts, c.Cert, c.Key, c.CaCert)
} else {
//http conn
client = etcd.NewClient(c.Hosts)
if client == nil {
err = fmt.Errorf("client init faild")
}
}
return ®istry{pool: client}, err
}
示例11: newClient
func newClient() (client *etcd.Client) {
if len(machines) == 1 && machines[0] == "" {
machines[0] = "http://127.0.0.1:4001"
}
if strings.HasPrefix(machines[0], "https://") {
var err error
if client, err = etcd.NewTLSClient(machines, tlspem, tlskey, ""); err != nil {
log.Fatal(err)
}
} else {
client = etcd.NewClient(machines)
}
client.SyncCluster()
return client
}
示例12: newClient
func newClient(machines []string, tlsCert, tlsKey, tlsCACert string) (client *etcd.Client) {
// set default if not specified in env
if len(machines) == 1 && machines[0] == "" {
machines[0] = "http://127.0.0.1:4001"
}
if strings.HasPrefix(machines[0], "https://") {
var err error
// TODO(miek): machines is local, the rest is global, ugly.
if client, err = etcd.NewTLSClient(machines, tlsCert, tlsKey, tlsCACert); err != nil {
// TODO(miek): would be nice if this wasn't a fatal error
log.Fatalf("skydns: failure to connect: %s", err)
}
return client
}
return etcd.NewClient(machines)
}
示例13: 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 *etcd.Client
if cert != "" && key != "" {
c, err := etcd.NewTLSClient(machines, cert, key, caCert)
if err != nil {
return &Client{c}, err
}
} else {
c = etcd.NewClient(machines)
}
success := c.SetCluster(machines)
if !success {
return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
}
return &Client{c}, nil
}
示例14: reconnect
func (n *ng) reconnect() error {
n.Close()
var client *etcd.Client
if n.options.EtcdCertFile == "" && n.options.EtcdKeyFile == "" {
client = etcd.NewClient(n.nodes)
} else {
var err error
if client, err = etcd.NewTLSClient(n.nodes, n.options.EtcdCertFile, n.options.EtcdKeyFile, n.options.EtcdCaFile); err != nil {
return err
}
}
if err := client.SetConsistency(n.options.EtcdConsistency); err != nil {
return err
}
n.client = client
return nil
}
示例15: NewClient
// NewClient returns an *etcd.Client with a connection to the named machines.
// It will return an error if a connection to the cluster cannot be made.
// The parameter machines needs to be a full URL with schemas.
// e.g. "http://localhost:4001" will work, but "localhost:4001" will not.
func NewClient(machines []string, cert, key, caCert string) (Client, error) {
var c *etcd.Client
var err error
if cert != "" && key != "" {
c, err = etcd.NewTLSClient(machines, cert, key, caCert)
if err != nil {
return nil, err
}
} else {
c = etcd.NewClient(machines)
}
success := c.SetCluster(machines)
if !success {
return nil, fmt.Errorf("cannot connect to the etcd cluster: %s", strings.Join(machines, ","))
}
return &client{c}, nil
}