etcd.go 1017 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package clinit
  2. /*
  3. import (
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. "go.etcd.io/etcd/client"
  9. )
  10. var etcdClient client.Client
  11. func InitEtcd(etcdaddrs []string) {
  12. endpoints := []string{}
  13. for _, addr := range etcdaddrs {
  14. if strings.HasPrefix(addr, "http://") {
  15. endpoints = append(endpoints, addr)
  16. } else {
  17. endpoints = append(endpoints, fmt.Sprintf("http://%s", addr))
  18. }
  19. }
  20. cli, err := client.New(client.Config{
  21. Endpoints: endpoints,
  22. Transport: client.DefaultTransport,
  23. HeaderTimeoutPerRequest: 5 * time.Second,
  24. })
  25. if err != nil {
  26. fmt.Printf("new etcd client failed. error:%s\n", err)
  27. os.Exit(1)
  28. }
  29. etcdClient = cli
  30. }
  31. func GetEtcdClient() client.Client {
  32. return etcdClient
  33. }*/
  34. import (
  35. "go.etcd.io/etcd/client/v3"
  36. )
  37. var cli *clientv3.Client
  38. func GetEtcdClient() *clientv3.Client {
  39. return cli
  40. }
  41. func EctdHandler(etcdAddrs []string) {
  42. var err error
  43. cli , err = clientv3.New(clientv3.Config{
  44. Endpoints: etcdAddrs,
  45. })
  46. if err != nil {
  47. panic(err)
  48. }
  49. return
  50. }