grpc的入门使用( 二 )

用户端
目录结构如下:
package mainimport ("context""fmt""google.golang.org/grpc""test/studentpb""time")func main() {addr := "127.0.0.1:8601"timeout := 10//建立rpc通道client, err := grpc.Dial(addr, grpc.WithInsecure())if err != nil {panic("连接失败")}defer client.Close()// 创建studentrpc对象rpcClient := studentpb.NewStudentClient(client)// 创建上线文ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)defer cancel()//封装请求参数req := &studentpb.StudentReqs{}req.S = Append(req.S, &studentpb.StudentReq{Name:"张三", Age:12})// 打印结果res , err := rpcClient.Add(ctx, req)if err != nil {fmt.Println("请求错误", err)} else {fmt.Println(res.GetErrno(), res.GetErrmsg())}}

关于360技术:360技术是360技术团队打造的技术分享公众号,每天推送技术干货内容,更多技术信息欢迎关注“360技术”微信公众号
 




推荐阅读