gRPC刚被Google开源, gRPC是啥? 先照抄一段说明:
gRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用、和电池寿命。
这段抄自info: http://www.infoq.com/cn/news/2015/03/grpc-google-http2-protobuf
官网地址: http://www.grpc.io
冲着面向移动应用开发这点,值得一试,参考其说明文档,用Golang弄了个例子:
1. 先创建一个rpcsrv.proto文件:
syntax = "proto3"; package xcl; enum UserStatus { OFFLINE = 0; ONLINE = 1; } message UserInfo { int32 id = 1; string name = 2; UserStatus status = 3 ; } message UserID { int32 id = 1; } message funcResponse { string reply = 1; } service UserService { rpc Login(UserInfo) returns (funcResponse){}; rpc logout(UserID) returns (funcResponse){}; }
2. 生成相应的rpcsrv.pb.go文件
主要的几个struct:
type UserInfo struct { Id int32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"` Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` Status UserStatus `protobuf:"varint,3,opt,name=status,enum=xcl.UserStatus" json:"status,omitempty"` } type UserID struct { Id int32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"` } type FuncResponse struct { Reply string `protobuf:"bytes,1,opt,name=reply" json:"reply,omitempty"` }
3. RPC服务端
//gRPC例子-服务端 //author: Xiong Chuan Liang //date: 2015-3-8 package main import ( pb "xcl" "log" "net" "os" "golang.org/x/net/context" "google.golang.org/grpc" ) func main() { log.SetOutput(os.Stdout) lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterUserServiceServer(s, &server{}) log.Println("Server......") s.Serve(lis) } const ( port = ":50051" ) type server struct{} func (s *server) Login(ctx context.Context, usr *pb.UserInfo) (*pb.FuncResponse, error) { log.Println("Server...... Login() UserInfo:", usr) usr.Id = 100 strId := "100" //strconv.Itoa(usr.Id) return &pb.FuncResponse{Reply: strId}, nil } func (s *server) Logout(ctx context.Context, uid *pb.UserID) (*pb.FuncResponse, error) { log.Println("Server...... Logout() UserID:", uid) return &pb.FuncResponse{Reply: "Logout Successed."}, nil }
4. 客户端:
//gRPC例子-客户端 //author: Xiong Chuan Liang //date: 2015-3-8 package main import ( "log" "os" "strconv" pb "xcl" "golang.org/x/net/context" "google.golang.org/grpc" ) const ( address = "localhost:50051" ) func main() { log.SetOutput(os.Stdout) conn, err := grpc.Dial(address) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewUserServiceClient(conn) var status pb.UserStatus status = pb.UserStatus_ONLINE userInfo := &pb.UserInfo{ //Id: 10, //proto.Int32(10), Name: "XCL-gRPC", //proto.String("XCL-gRPC"), Status: status, } r, err := c.Login(context.Background(), userInfo) if err != nil { log.Fatalf("登录失败! %v", err) } log.Println("Login():", r) uid, err := strconv.ParseInt(r.Reply, 10, 32) if err != nil { log.Fatalf("非数字 %v", err) } userID := &pb.UserID{int32(uid)} out, err := c.Logout(context.Background(), userID) log.Println("Logout():", out) }
运行结果:
gRPC 跑起来了,内部的东西还没去细究。不过app服务端相关的话,应当值得一试。
MAIL: [email protected]
BLOG:http://blog.csdn.net/xcl168
时间: 2024-10-15 21:27:49