package global import ( "math/rand" "speech-nlu-parse/model" "speech-nlu-parse/pkg/consul" "github.com/hashicorp/consul/api" ) // 从consul中获取 var serviceMap map[string][]*model.Service var ( consulUrl string consulToken string ) var ConsulCient *api.Client func InitConsulCient(url, token string) { config := api.DefaultConfig() config.Address = url config.Token = token var err error ConsulCient, err = api.NewClient(config) if err != nil { panic(err) } } func InitServiceMap(url string, token string) { consulUrl = url consulToken = token InitConsulCient(consulUrl, consulToken) } func GetService(name string) *model.Service { // 重新从consul上获取 services := consul.GetService2(ConsulCient, name, "") if services == nil || len(services) <= 0 { return nil } else { index := rand.Intn(len(services)) return &model.Service{Address: services[index].Service.Address, Port: services[index].Service.Port} } }