etcd.go 721 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package clinit
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "go.etcd.io/etcd/client"
  8. )
  9. var etcdClient client.Client
  10. func InitEtcd(etcdaddrs []string) {
  11. endpoints := []string{}
  12. for _, addr := range etcdaddrs {
  13. if strings.HasPrefix(addr, "http://") {
  14. endpoints = append(endpoints, addr)
  15. } else {
  16. endpoints = append(endpoints, fmt.Sprintf("http://%s", addr))
  17. }
  18. }
  19. cli, err := client.New(client.Config{
  20. Endpoints: endpoints,
  21. Transport: client.DefaultTransport,
  22. HeaderTimeoutPerRequest: 5 * time.Second,
  23. })
  24. if err != nil {
  25. fmt.Printf("new etcd client failed. error:%s\n", err)
  26. os.Exit(1)
  27. }
  28. etcdClient = cli
  29. }
  30. func GetEtcdClient() client.Client {
  31. return etcdClient
  32. }