- Go语言QQ群: 102319854, 1055927514
- 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa
██████╗ ██████╗ ██████╗ ████████╗ ██████╗ ██████╗ ██████╗ ██████╗
██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝██╔═══██╗ ██╔══██╗██╔══██╗██╔════╝
██████╔╝██████╔╝██║ ██║ ██║ ██║ ██║█████╗██████╔╝██████╔╝██║
██╔═══╝ ██╔══██╗██║ ██║ ██║ ██║ ██║╚════╝██╔══██╗██╔═══╝ ██║
██║ ██║ ██║╚██████╔╝ ██║ ╚██████╔╝ ██║ ██║██║ ╚██████╗
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═════╝
- C++ Version(Proto2): https://github.com/chai2010/protorpc.cxx
- C++ Version(Proto3): https://github.com/chai2010/protorpc3-cxx
- Talks: Go/C++语言Protobuf-RPC简介
Install protorpc
package:
go install github.com/golang/protobuf/protoc-gen-go
go get github.com/chai2010/protorpc
go run hello.go
Install protoc-gen-go
plugin:
- install
protoc
at first: https://github.com/google/protobuf/releases go get github.com/golang/protobuf/protoc-gen-go
go get github.com/chai2010/protorpc/protoc-gen-protorpc
go generate github.com/chai2010/protorpc/examples/service.pb
go test github.com/chai2010/protorpc/examples/service.pb
First, create echo.proto:
syntax = "proto3";
package service;
message EchoRequest {
string msg = 1;
}
message EchoResponse {
string msg = 1;
}
service EchoService {
rpc Echo (EchoRequest) returns (EchoResponse);
rpc EchoTwice (EchoRequest) returns (EchoResponse);
}
Second, generate echo.pb.go and echo.pb.protorpc.go
from echo.proto (we can use go generate
to invoke this command, see proto.go).
protoc --go_out=. echo.proto
protoc --protorpc_out=. echo.proto
Now, we can use the stub code like this:
package main
import (
"fmt"
"log"
"github.com/chai2010/protorpc"
service "github.com/chai2010/protorpc/examples/service.pb"
)
type Echo int
func (t *Echo) Echo(args *service.EchoRequest, reply *service.EchoResponse) error {
reply.Msg = args.Msg
return nil
}
func (t *Echo) EchoTwice(args *service.EchoRequest, reply *service.EchoResponse) error {
reply.Msg = args.Msg + args.Msg
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: "你好, 世界!"}
reply, err := echoClient.EchoTwice(args)
if err != nil {
log.Fatalf("echoClient.EchoTwice: %v", err)
}
fmt.Println(reply.Msg)
// or use normal client
client, err := protorpc.Dial("tcp", `127.0.0.1:9527`)
if err != nil {
log.Fatalf("protorpc.Dial: %v", err)
}
defer client.Close()
echoClient1 := &service.EchoServiceClient{client}
echoClient2 := &service.EchoServiceClient{client}
reply, err = echoClient1.EchoTwice(args)
reply, err = echoClient2.EchoTwice(args)
_, _ = reply, err
// Output:
// 你好, 世界!你好, 世界!
}
First, create echo.proto:
syntax = "proto3";
package service;
message EchoRequest {
string msg = 1;
}
message EchoResponse {
string msg = 1;
}
service EchoService {
rpc Echo (EchoRequest) returns (EchoResponse);
rpc EchoTwice (EchoRequest) returns (EchoResponse);
}
Second, generate echo.pb.go from echo.proto with protoc-gen-stdrpc
plugin.
protoc --stdrpc_out=. echo.proto
The stdrpc plugin generated code do not depends protorpc package, it use gob as the default rpc encoding.
$ ENV_PROTOC_GEN_PROTORPC_FLAG_PREFIX=abc protoc --protorpc_out=. x.proto
Report bugs to [email protected].
Thanks!