示例#1
0
/**
 * [HttpBackends description]
 */
func HttpBackends() {
	url := get("consul") + "/v1/catalog/services"
	request := gorequest.New().Timeout(10 * 1000 * time.Millisecond)
	_, body, err := request.Get(url).EndBytes()
	if err != nil {
		fmt.Println(err)
		return
	}

	debug("service list:%s", body)
	json, _ := simplejson.NewJson(body)
	data, _ := json.Map()
	// nodes = make([]serviceNode, len(arr))
	services := make([]string, 0)
	backendTagArr := strings.Split(get("backendTag"), ",")
	for name, _ := range data {
		for _, backendTag := range backendTagArr {
			tags, _ := json.GetPath(name).Array()
			index := indexOf(backendTag, tags)
			if index == -1 {
				break
			}
			index = indexOf(name, services)
			if index == -1 {
				services = append(services, name)
			}
		}
	}
	for _, name := range services {
		nodes := getService(name)
		debug("ndoes:%s", nodes)
	}
}
示例#2
0
/**
 * [getService description]
 * @param  {[type]} service string        [description]
 * @return {[type]}         [description]
 */
func getService(service string) (nodes []*simplejson.Json) {
	url := get("consul") + "/v1/catalog/service/" + service
	debug("get service url:%s", url)
	request := gorequest.New().Timeout(2000 * time.Millisecond)
	_, body, err := request.Get(url).EndBytes()
	if err != nil {
		panic("get service from consul fail")
	}

	debug("get service:%s", body)
	json, _ := simplejson.NewJson(body)

	arr, _ := json.Array()
	nodes = make([]*simplejson.Json, len(arr))
	for i, _ := range arr {
		tmp := json.GetIndex(i)
		weightKey := "weight:"
		weight := 1
		for _, tag := range tmp.Get("ServiceTags").MustStringArray() {
			if strings.Index(tag, weightKey) == 0 {
				tmpWeight, _ := strconv.Atoi(tag[len(weightKey):])
				weight = tmpWeight
			}
		}

		t := simplejson.New()
		t.Set("name", tmp.Get("ServiceName").MustString())
		t.Set("ip", tmp.Get("ServiceAddress").MustString())
		t.Set("port", tmp.Get("ServicePort").MustInt())
		t.Set("weight", weight)
		nodes[i] = t
	}
	return
}