这里的服务调用,咱们调用的能够是http api
也能够是gRPC
等。主要意思就是调用咱们从consul
获取到的服务的API。git
下面的全部示例以RESTful HTTP API
为例github
咱们在服务发现以后,确定要调用发现以后的服务,这里的服务能够是http的RESTful API
也能够是RPC
服务等,这里之前面的定义的productService
的RESTful API
做为被调用者web
其实就是用获取到的服务地址,调API算法
下面要演示的是使用标准库net/http
的httpclient
进行的比较原始的请求API的方法json
被调用的APIapi
/v1/listapp
服务调用的代码dom
func main() { // 1.链接到consul cr := consul.NewRegistry(registry.Addrs("47.100.220.174:8500")) // 2.根据service name获取对应的微服务列表 services, err := cr.GetService("productService") if err != nil { log.Fatal("cannot get service list") } // 3.使用random随机获取其中一个实例 next := selector.RoundRobin(services) svc, err := next() if err != nil { log.Fatal("cannot get service") } fmt.Println("[测试输出]:", svc.Address) // 4. 请求获取到的服务的API方法 resp, err := RequestApi(http.MethodGet, svc.Address, "/v1/list", nil) if err != nil { log.Fatal("request api failed") } fmt.Println("[请求API结果]:", resp) } // 简单封装一个请求api的方法 func RequestApi(method string, host string, path string, body io.Reader) (string, error) { // 1.若是没有http开头就给它加一个 if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") { host = "http://" + host } // 2. 新建一个request req, _ := http.NewRequest(method, host+path, body) // 3. 新建httpclient,而且传入request client := http.DefaultClient res, err := client.Do(req) if err != nil { return "", err } defer res.Body.Close() // 4. 获取请求结果 buff, err := ioutil.ReadAll(res.Body) if err != nil { return "", err } return string(buff), nil }
以下能够调用成功:函数
上面咱们调用api的方式是没什么问题,可是有缺点就是微服务
按照官方的说法:
go-plugins使您能够交换基础设施结构,而没必要重写全部代码。这样就能够在多个环境中运行相同的软件,而无需进行大量工做
查看go-plugins
的组成部分,client中有http api
服务端代码跟以前的差不太多
package main import ( "github.com/gin-gonic/gin" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/web" "github.com/micro/go-plugins/registry/consul" "gomicro-quickstart/product_service/model" "net/http" ) func main() { // 添加consul地址 cr := consul.NewRegistry(registry.Addrs("127.0.0.1:8500")) // 使用gin做为路由 router := gin.Default() v1 := router.Group("v1") { v1.POST("list", func(c *gin.Context) { var req ProdRequest if err := c.Bind(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "data": "模型绑定失败", }) c.Abort() return } c.JSON(http.StatusOK, gin.H{ "data": model.NewProductList(req.Size), }) }) } server := web.NewService( web.Name("ProductService"), // 当前微服务服务名 web.Registry(cr), // 注册到consul web.Address(":8001"), // 端口 web.Metadata(map[string]string{"protocol": "http"}), // 元信息 web.Handler(router)) // 路由 _ = server.Init() _ = server.Run() } type ProdRequest struct { Size int `json:"size"` }
下面是返回的model对象代码
package model import "strconv" type Product struct { Id int Name string } func NewProduct(id int, name string) *Product { return &Product{ Id: id, Name: name, } } func NewProductList(count int) []*Product { products := make([]*Product, 0) for i := 0; i < count; i++ { products = append(products, NewProduct(i+1, "productName"+strconv.Itoa(i+1))) } return products }
这里使用了go-plugins
中client
下的http
的包,优势是
getService
的步骤package main import ( "context" "fmt" "log" "github.com/micro/go-micro/client" "github.com/micro/go-micro/client/selector" "github.com/micro/go-micro/registry" "github.com/micro/go-plugins/client/http" "github.com/micro/go-plugins/registry/consul" ) func main() { // 1. 注册consul地址 cr := consul.NewRegistry(registry.Addrs("47.100.220.174:8500")) // 2. 实例化selector mySelector := selector.NewSelector( selector.Registry(cr), // 传入上面的consul selector.SetStrategy(selector.RoundRobin), // 指定获取实例的算法 ) // 3. 请求服务 resp, err := callByGoPlugin(mySelector) if err != nil { log.Fatal("request API failed", err) } fmt.Printf("[服务调用结果]:\r\n %v", resp) } func callByGoPlugin(s selector.Selector) (map[string]interface{}, error) { // 1. 调用`go-plugins/client/http`包的函数获取它们提供的httpClient gopluginClient := http.NewClient( client.Selector(s), // 传入上面的selector client.ContentType("application/json"), // 指定contentType ) // 2. 新建请求对象,传入: (1)服务名 (2)endpoint (3)请求参数 req := gopluginClient.NewRequest("ProductService", "/v1/list", map[string]interface{}{"size": 6}) // 3. 新建响应对象,并call请求,获取响应 var resp map[string]interface{} err := gopluginClient.Call(context.Background(), req, &resp) if err != nil { return nil, err } return resp, nil }
客户端调用结果: