2 * Copyright (c) 2018 ZTE Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * ZTE - initial Project
19 "github.com/hashicorp/consul/api"
22 var client *api.Client
23 var consulAddress string
26 cfgFilePath = filepath.Join(util.GetCfgPath(), "consul.yml")
30 defaultAddress = "http://localhost:8500"
34 consulAddress = getConsulAddress(cfgFilePath)
36 conf := api.DefaultConfig()
37 conf.Address = consulAddress
39 client, err = api.NewClient(conf)
42 log.Log.Error("failed to init consul client", err)
46 func getConsulAddress(path string) string {
47 cfg, err := loadCfgInfo(path)
49 log.Log.Error("load consul config info error", err)
52 if addr, exist := cfg["address"]; exist {
60 func loadCfgInfo(path string) (map[interface{}]interface{}, error) {
61 log.Log.Informational("consul config path is:" + path)
62 cfg, err := util.Read(path)
67 result := make(map[interface{}]interface{})
68 err = util.UnmarshalYaml(cfg, result)
75 func GetServices() (map[string][]string, error) {
76 data, _, err := client.Catalog().Services(nil)
84 func GetInstances(serviceName string) ([]*api.CatalogService, error) {
85 endpoints, _, err := client.Catalog().Service(serviceName, "", nil)
87 log.Log.Error("can not get endpoints of ", serviceName)