
Go gRPC 实战从 proto 到流式 RPC 全链路gRPC 是微服务通信的事实标准性能是 HTTP/JSON 的 5-10 倍。本文从 IDL 到流式调用一次性讲透。一、安装 protocgoinstallgoogle.golang.org/protobuf/cmd/protoc-gen-golatest goinstallgoogle.golang.org/grpc/cmd/protoc-gen-go-grpclatest写入.proto文件syntax proto3; package user.v1; option go_package example.com/user/proto;userpb; service UserService { rpc GetUser(GetUserRequest) returns (GetUserResponse); rpc ListUsers(ListUsersRequest) returns (stream User); } message User { string id 1; string name 2; int32 age 3; } message GetUserRequest { string id 1; } message GetUserResponse { User user 1; } message ListUsersRequest {}执行protoc --go_out. --go-grpc_out. user.proto二、四个调用模式Unary标准 RPC客户端发、收一次Server Streaming服务器流式推送Client Streaming客户端流式推送Bidirectional Streaming双向流式三、Server 实现import(google.golang.org/grpcuserpbexample.com/user/proto)typeserverstruct{userpb.UnimplementedUserServiceServer}func(s*server)GetUser(ctx context.Context,req*userpb.GetUserRequest)(*userpb.GetUserResponse,error){returnuserpb.GetUserResponse{User:userpb.User{Id:req.Id,Name:tom,Age:18}},nil}func(s*server)ListUsers(req*userpb.ListUsersRequest,stream userpb.UserService_ListUsersServer)error{for_,u:rangerepo.All(){iferr:stream.Send(u);err!nil{returnerr}}returnnil}funcmain(){lis,_:net.Listen(tcp,:50051)s:grpc.NewServer(grpc.UnaryInterceptor(recoverInterceptor()))userpb.RegisterUserServiceServer(s,server{})s.Serve(lis)}四、Client 实现conn,err:grpc.Dial(:50051,grpc.WithInsecure())deferconn.Close()cli:userpb.NewUserServiceClient(conn)ctx,cancel:context.WithTimeout(context.Background(),time.Second)defercancel()u,err:cli.GetUser(ctx,userpb.GetUserRequest{Id:42})// 流式stream,err:cli.ListUsers(ctx,userpb.ListUsersRequest{})for{u,err:stream.Recv()iferrio.EOF{break}iferr!nil{returnerr}fmt.Println(u)}五、拦截器链路grpc.UnaryServerInterceptor(loggingInterceptor,recoverInterceptor,traceInterceptor,)跨多拦截器时用 go-grpc-middleware 库。六、性能压测BenchmarkGRPC-8 100000 14000 ns/op 400 B/op 2 allocs/op BenchmarkJSON-8 50000 28000 ns/op 1200 B/op 9 allocs/opgRPC 性能明显占优。七、踩坑清单ctx 必须传否则无法超时与取消Default 流量是 TCP 不加密在线环境用 TLS大消息拆流式proto 序列化包含大值时单个 message 不要过大字段编号不能修改旧字段只能 deprecated 新字段八、总结与展望gRPC 是云原生时代服务通信的最佳实践。掌握 proto、stream、interceptor 三件套能构建高性能微服务集群。未来grpcurl buf schema 工具链 Connect 等兼容 HTTP/JSON 的形态值得关注。九、参考文献gRPC 官方文档https://grpc.io/protobuf-go 文档grpcurl 工具