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
21 "github.com/hashicorp/consul/api"
24 var client *api.Client
25 var consulAddress string
28 cfgFilePath = filepath.Join(util.GetCfgPath(), "consul.yml")
32 defaultAddress = "http://localhost:8500"
36 consulAddress = getConsulAddress(cfgFilePath)
38 conf := api.DefaultConfig()
39 conf.Address = consulAddress
41 client, err = api.NewClient(conf)
44 log.Log.Error("failed to init consul client", err)
48 func getConsulAddress(path string) string {
49 res := os.Getenv(models.EnvConsulAddress)
54 cfg, err := loadCfgInfo(path)
56 log.Log.Error("load consul config info error", err)
59 if addr, exist := cfg["address"]; exist {
67 func loadCfgInfo(path string) (map[interface{}]interface{}, error) {
68 log.Log.Informational("consul config path is:" + path)
69 cfg, err := util.Read(path)
74 result := make(map[interface{}]interface{})
75 err = util.UnmarshalYaml(cfg, result)
82 func GetServices() (map[string][]string, error) {
83 data, _, err := client.Catalog().Services(nil)
91 func GetInstances(serviceName string) ([]*api.CatalogService, error) {
92 endpoints, _, err := client.Catalog().Service(serviceName, "", nil)
94 log.Log.Error("can not get endpoints of ", serviceName)