etcd.go 734 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package parser
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. client "go.etcd.io/etcd/client/v3"
  7. "property-company-gateway/config"
  8. )
  9. var etcdClient *client.Client
  10. func EtcdHandler(conf *config.Configure) {
  11. InitEtcd(conf.EtcdAddrs)
  12. }
  13. func InitEtcd(etcdaddrs []string) {
  14. endpoints := []string{}
  15. for _, addr := range etcdaddrs {
  16. if strings.HasPrefix(addr, "http://") {
  17. endpoints = append(endpoints, addr)
  18. } else {
  19. endpoints = append(endpoints, fmt.Sprintf("http://%s", addr))
  20. }
  21. }
  22. cli, err := client.New(client.Config{
  23. Endpoints: endpoints,
  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. }