1234567891011121314151617181920212223242526272829303132333435363738 |
- package parser
- import (
- "fmt"
- "os"
- "strings"
- client "go.etcd.io/etcd/client/v3"
- "xingjia-management-gateway/config"
- )
- var etcdClient *client.Client
- func EtcdHandler(conf *config.Configure) {
- InitEtcd(conf.EtcdAddrs)
- }
- func InitEtcd(etcdaddrs []string) {
- endpoints := []string{}
- for _, addr := range etcdaddrs {
- if strings.HasPrefix(addr, "http://") {
- endpoints = append(endpoints, addr)
- } else {
- endpoints = append(endpoints, fmt.Sprintf("http://%s", addr))
- }
- }
- cli, err := client.New(client.Config{
- Endpoints: endpoints,
- })
- if err != nil {
- fmt.Printf("new etcd client failed. error:%s\n", err)
- os.Exit(1)
- }
- etcdClient = cli
- }
- func GetEtcdClient() *client.Client {
- return etcdClient
- }
|