创建客户端并调用

package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    "grpccli/services"
    "log"
)

func main() {
    conn, err := grpc.Dial("localhost:8081", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    prodClient := services.NewProdServiceClient(conn)//把在服务端生成的protobuf的go文件拷贝到services目录下
    prodRes, err := prodClient.GetProdStock(context.Background(), &services.ProdRequest{ProdId: 12})
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(prodRes.ProdStock)
}




相关推荐