项目作者: rpcxio

项目描述 :
protoc插件, 可以生成rpcx客户端和服务端代码
高级语言: Go
项目地址: git://github.com/rpcxio/protoc-gen-rpcx.git
创建时间: 2020-05-09T11:33:07Z
项目社区:https://github.com/rpcxio/protoc-gen-rpcx

开源协议:BSD 3-Clause "New" or "Revised" License

下载


安装

  • 首先我们需要 protoc 编译器: protobuf。一般平台上都有编译好的二进制程序,直接下载加载PATH路径里即可。或者你也可以源代码安装。

protoc负责将proto文件编译成不同编程语言的代码,一般通过插件的方式实现。

  • 安装golang官方protoc-gen-go插件或gogo protoc-gen-gofast插件,任选其一即可。
  1. go install github.com/golang/protobuf/protoc-gen-go@latest // 支持标准库
  2. go install github.com/gogo/protobuf/protoc-gen-gofast@latest // 支持gogo gofast
  3. go install github.com/rpcxio/protoc-gen-go@latest // 本插件
  • 编译rpcx插件:
  1. go install github.com/rpcxio/protoc-gen-rpcx@latest

如果你到达了这一步,恭喜你,插件安装成功了,按照下面的命令你就可以将proto中定义的service编译成rpcx的服务和客户端代码了:

protobuf官方库(如果未设置GOPATH,请去掉-I${GOPATH}/src或者设置GOPAH):

  1. protoc -I. -I${GOPATH}/src \
  2. --go_out=. \
  3. --rpcx_out=. --rpcx_opt=paths=source_relative helloworld.proto

gogo库(如果未设置GOPATH,请去掉-I${GOPATH}/src或者设置GOPAH):

  1. protoc -I. -I${GOPATH}/src \
  2. --gofast_out=. --gofast_opt=paths=source_relative \
  3. --rpcx_out=. --rpcx_opt=paths=source_relative *.proto

例子

  • proto文件

最简单的一个打招呼的rpc服务。

  1. syntax = "proto3";
  2. option go_package = "github.com/rpcxio/protoc-gen-rpcx/testdata/rpcx/helloworld";
  3. package helloworld;
  4. // The greeting service definition.
  5. service Greeter {
  6. // Sends a greeting
  7. rpc SayHello (HelloRequest) returns (HelloReply) {}
  8. }
  9. // The request message containing the user's name.
  10. message HelloRequest {
  11. string name = 1;
  12. }
  13. // The response message containing the greetings
  14. message HelloReply {
  15. string message = 1;
  16. }
  • 使用protoc编译器编译出Go代码
  1. protoc -I. -I${GOPATH}/src \
  2. --gofast_out=. --gofast_opt=paths=source_relative \
  3. --rpcx_out=. --rpcx_opt=paths=source_relative helloworld.proto

上述命令生成了 helloworld.pb.gohelloworld.rpcx.pb.go 两个文件。 helloworld.pb.go 文件是由protoc-gen-gofast插件生成的,
当然你也可以选择官方的protoc-gen-go插件来生成。 helloworld.rpcx.pb.go 是由protoc-gen-rpcx插件生成的,它包含服务端的一个骨架, 以及客户端的代码。

  • 服务端代码

服务端的代码只是一个骨架,很显然你要实现你的逻辑。比如这个打招呼的例子, 客户端传入一个名称,你可以返回一个hello <name>的字符串。

它还提供了一个简单启动服务的方法,你可以在此基础上实现服务端的代码,注册很多的服务,配置注册中心和其它插件等等。

  1. package main
  2. import (
  3. context "context"
  4. "fmt"
  5. helloworld "github.com/rpcxio/protoc-gen-rpcx/testdata/rpcx"
  6. server "github.com/smallnest/rpcx/server"
  7. )
  8. func main() {
  9. s := server.NewServer()
  10. s.RegisterName("Greeter", new(GreeterImpl), "")
  11. err := s.Serve("tcp", ":8972")
  12. if err != nil {
  13. panic(err)
  14. }
  15. }
  16. type GreeterImpl struct{}
  17. // SayHello is server rpc method as defined
  18. func (s *GreeterImpl) SayHello(ctx context.Context, args *helloworld.HelloRequest, reply *helloworld.HelloReply) (err error) {
  19. *reply = helloworld.HelloReply{
  20. Message: fmt.Sprintf("hello %s!", args.Name),
  21. }
  22. return nil
  23. }
  • 客户端代码

客户端生成的代码更友好,它包装了XClient对象,提供了符合人工美学的方法调用格式(请求参数作为方法参数,返回结果作为方法的返回值)。并且提供了客户端的配置方式。

你也可以扩展客户端的配置,提供注册中心、路由算法,失败模式、重试、熔断等服务治理的设置。 

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. helloworld "github.com/rpcxio/protoc-gen-rpcx/testdata/rpcx"
  6. )
  7. func main() {
  8. xclient := helloworld.NewXClientForGreeter("127.0.0.1:8972")
  9. client := helloworld.NewGreeterClient(xclient)
  10. args := &helloworld.HelloRequest{
  11. Name: "rpcx",
  12. }
  13. reply, err := client.SayHello(context.Background(), args)
  14. if err != nil {
  15. panic(err)
  16. }
  17. fmt.Println("reply: ", reply.Message)
  18. }