██████╗ ██████╗ ██████╗ ████████╗ ██████╗ ██████╗ ██████╗ ██████╗
██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝██╔═══██╗ ██╔══██╗██╔══██╗██╔════╝
██████╔╝██████╔╝██║ ██║ ██║ ██║ ██║█████╗██████╔╝██████╔╝██║
██╔═══╝ ██╔══██╗██║ ██║ ██║ ██║ ██║╚════╝██╔══██╗██╔═══╝ ██║
██║ ██║ ██║╚██████╔╝ ██║ ╚██████╔╝ ██║ ██║██║ ╚██████╗
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═════╝
PkgDoc: http://godoc.org/github.com/chai2010/protorpc
C++ Version: https://github.com/chai2010/protorpc.cxx
Talks: Go/C++语言Protobuf-RPC简介
go get github.com/chai2010/protorpc
go get github.com/chai2010/protorpc/protoc-gen-go
go run hello.go
First, create echo.proto:
package service;
message EchoRequest {
optional string msg = 1;
}
message EchoResponse {
optional string msg = 1;
}
service EchoService {
rpc Echo (EchoRequest) returns (EchoResponse);
rpc EchoTwice (EchoRequest) returns (EchoResponse);
}
Second, generate echo.pb.go
from echo.proto (we can use go generate
to invoke this command, see proto.go).
protoc --go_out=. echo.proto
Now, we can use the stub code like this:
package main
import (
"fmt"
"log"
"github.com/chai2010/protorpc/proto"
service "github.com/chai2010/protorpc/internal/service.pb"
)
type Echo int
func (t *Echo) Echo(args *service.EchoRequest, reply *service.EchoResponse) error {
reply.Msg = proto.String(args.GetMsg())
return nil
}
func (t *Echo) EchoTwice(args *service.EchoRequest, reply *service.EchoResponse) error {
reply.Msg = proto.String(args.GetMsg() + args.GetMsg())
return nil
}
func init() {
go service.ListenAndServeEchoService("tcp", `127.0.0.1:9527`, new(Echo))
}
func main() {
echoClient, _, err := service.DialEchoService("tcp", `127.0.0.1:9527`)
if err != nil {
log.Fatalf("service.DialEchoService: %v", err)
}
defer echoClient.Close()
args := &service.EchoRequest{Msg: proto.String("你好, 世界!")}
reply := &service.EchoResponse{}
err = echoClient.EchoTwice(args, reply)
if err != nil {
log.Fatalf("echoClient.EchoTwice: %v", err)
}
fmt.Println(reply.GetMsg())
}
Report bugs to [email protected].
Thanks!