7.基本方式调用Api(http api)

当服务很简单的时候可以用这种方法来做基本调用

package main

import (
    "fmt"
    "github.com/micro/go-micro/client/selector"
    "github.com/micro/go-micro/registry"
    "github.com/micro/go-plugins/registry/consul"
    "io/ioutil"
    "log"
    "net/http"
)

func callAPI(addr string, path string, method string) (string, error) { //封装http请求函数
    req, _ := http.NewRequest(method, "http://"+addr+path, nil)
    client := http.DefaultClient
    res, err := client.Do(req)
    if err != nil {
        return "", err
    }
    defer res.Body.Close()
    buf, _ := ioutil.ReadAll(res.Body)
    return string(buf), nil
}

func main() {
    consulReg := consul.NewRegistry( //新建一个consul注册的地址,也就是我们consul服务启动的机器ip+端口
        registry.Addrs("localhost:8500"),
    )
    for {
        getService, err := consulReg.GetService("prodservice") //使用服务名获取服务
        if err != nil {
            log.Fatal(err)
        }
        next := selector.RoundRobin(getService) //go-micro很智能当服务列表中一个服务出现问题后,他会自动帮我们从轮询列表中删除调,我们的轮询只会访问有效的服务,如果getService有多个服务,从第一个开始轮询 如localhost:8080/v1/user,localhost:8081/v1/user
        node, err := next()                     //type Next func() (*registry.Node, error)
        if err != nil {
            log.Fatal()
        }
        callRes, err := callAPI(node.Address, "/user", "GET")
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(callRes)
    }
}




posted @ 2019-12-26 14:49  离地最远的星  阅读(583)  评论(0编辑  收藏  举报