Giter Site home page Giter Site logo

protoc-gen-gogorpcx's Introduction

本工具基于gogo/protobuf, 在gogo protobuf的基础上增加了生成rpcx服务的能力。

gogo 生成的 protobuf代码性能更好,人性化高,并且兼容protobu协议:

  • fast marshalling and unmarshalling
  • more canonical Go structures
  • goprotobuf compatibility
  • less typing by optionally generating extra helper code
  • peace of mind by optionally generating test and benchmark code
  • other serialization formats

安装

首先,确保你已经安装了protoc编译器,并且安装了Go。

本工具提供了四种代码生成的方式。

Speed

安装protoc-gen-gofast二进制:

go get github.com/rpcxio/protoc-gen-gogorpcx/protoc-gen-gofast

使用它你可以产生更快的序列化代码:

protoc --gofast_out=plugins=rpcx:. myproto.proto

但是你不能使用其它的 gogoprotobu 扩展

更快,更便利

字段无指针可以让GC花费更好的时间,而且可以产生更多的辅助方法。

protoc-gen-gogofast (在gofast一基础上, 可以导入gogoprotobuf)
protoc-gen-gogofaster (在gogofast基础上, 去掉XXX_unrecognized方法, 更少的指针字段)
protoc-gen-gogoslick (在gogofaster基础上, 辅助方法string, gostring 和 equal)

例子

  • proto文件

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

syntax = "proto3";

option go_package = "helloword";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}
  • 使用protoc编译器编译出Go代码
protoc --gofast_out=plugins=rpcx:. helloworld.proto

上述命令生成了 helloworld.pb.go 文件, 它包含各种struct的定义, 还有服务端的一个骨架, 以及客户端的代码。

  • 服务端代码

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

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

package main

import (
	context "context"
	"fmt"

	helloworld "github.com/golang/protobuf/protoc-gen-go/testdata/rpcx"
	server "github.com/smallnest/rpcx/server"
)

func main() {
	s := server.NewServer()
	s.RegisterName("Greeter", new(GreeterImpl), "")
	err := s.Serve("tcp", ":8972")
	if err != nil {
		panic(err)
	}
}

type GreeterImpl struct{}

// SayHello is server rpc method as defined
func (s *GreeterImpl) SayHello(ctx context.Context, args *helloworld.HelloRequest, reply *helloworld.HelloReply) (err error) {
	*reply = helloworld.HelloReply{
		Message: fmt.Sprintf("hello %s!", args.Name),
	}
	return nil
}
  • 客户端代码

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

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

package main

import (
	"context"
	"fmt"

	helloworld "github.com/golang/protobuf/protoc-gen-go/testdata/rpcx"
)

func main() {
	xclient := helloworld.NewXClientForGreeter("127.0.0.1:8972")
	client := helloworld.NewGreeterClient(xclient)

	args := &helloworld.HelloRequest{
		Name: "rpcx",
	}

	reply, err := client.SayHello(context.Background(), args)
	if err != nil {
		panic(err)
	}

	fmt.Println("reply: ", reply.Message)
}

protoc-gen-gogorpcx's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

protoc-gen-gogorpcx's Issues

根據範例沒有產生 Client

嘗試先用指令安裝
go get github.com/rpcxio/protoc-gen-gogorpcx/protoc-gen-gogofast
然後跑
protoc --gofast_out=plugins=rpcx:. helloworld.proto
只有產生 message 的程式碼

这个插件安装不上?

执行了以下安装命令,还是报错
`$ go get github.com/rpcxio/protoc-gen-gogorpcx/protoc-gen-gofast

go: downloading github.com/rpcxio/protoc-gen-gogorpcx v0.2.1
go: added github.com/rpcxio/protoc-gen-gogorpcx v0.2.1`

错误:
`$ protoc --gofast_out=plugins=rpcx:. hello.proto

protoc-gen-gofast: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--gofast_out: protoc-gen-gofast: Plugin failed with status code 1.
`

我看了PATH里确实没有 protoc-gen-gofast 这个可执行文件,GOPATH下是有github.com/rpcxio/protoc-gen-gogorpcx 的代码的,只是没有可执行的文件

module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/compare

go version:go1.14.9
os:windows 7

D:\cmder
λ go get github.com/rpcxio/protoc-gen-gogorpcx/protoc-gen-gofast
go: downloading github.com/rpcxio/protoc-gen-gogorpcx v0.0.0-20200921090426-0684feea2433
go: found github.com/rpcxio/protoc-gen-gogorpcx/protoc-gen-gofast in github.com/rpcxio/protoc-gen-gogorpcx v0.0.0-20200921090426-0684feea2433
go: finding module for package github.com/gogo/protobuf/vanity
go: finding module for package github.com/gogo/protobuf/plugin/equal
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/plugin
go: finding module for package github.com/gogo/protobuf/plugin/compare
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/grpc
go: finding module for package github.com/gogo/protobuf/plugin/face
go: finding module for package github.com/gogo/protobuf/plugin/unmarshal
go: finding module for package github.com/gogo/protobuf/plugin/oneofcheck
go: finding module for package github.com/gogo/protobuf/plugin/size
go: finding module for package github.com/gogo/protobuf/plugin/union
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/generator
go: finding module for package github.com/gogo/protobuf/plugin/description
go: finding module for package github.com/gogo/protobuf/plugin/testgen
go: finding module for package github.com/gogo/protobuf/proto
go: finding module for package github.com/gogo/protobuf/plugin/embedcheck
go: finding module for package github.com/gogo/protobuf/plugin/gostring
go: finding module for package github.com/gogo/protobuf/plugin/populate
go: finding module for package github.com/gogo/protobuf/plugin/enumstringer
go: finding module for package github.com/gogo/protobuf/plugin/stringer
go: finding module for package github.com/gogo/protobuf/plugin/marshalto
go: finding module for package github.com/gogo/protobuf/plugin/defaultcheck
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/descriptor
go: finding module for package github.com/gogo/protobuf/vanity
go: finding module for package github.com/gogo/protobuf/plugin/equal
go: finding module for package github.com/gogo/protobuf/plugin/unmarshal
go: finding module for package github.com/gogo/protobuf/plugin/marshalto
go: finding module for package github.com/gogo/protobuf/plugin/description
go: finding module for package github.com/gogo/protobuf/plugin/stringer
go: finding module for package github.com/gogo/protobuf/plugin/defaultcheck
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/plugin
go: finding module for package github.com/gogo/protobuf/plugin/populate
go: finding module for package github.com/gogo/protobuf/plugin/oneofcheck
go: finding module for package github.com/gogo/protobuf/plugin/embedcheck
go: finding module for package github.com/gogo/protobuf/proto
go: finding module for package github.com/gogo/protobuf/plugin/face
go: finding module for package github.com/gogo/protobuf/plugin/union
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/grpc
go: finding module for package github.com/gogo/protobuf/plugin/enumstringer
go: finding module for package github.com/gogo/protobuf/plugin/testgen
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/generator
go: finding module for package github.com/gogo/protobuf/plugin/size
go: finding module for package github.com/gogo/protobuf/plugin/compare
go: finding module for package github.com/gogo/protobuf/plugin/gostring
go: finding module for package github.com/gogo/protobuf/protoc-gen-gogo/descriptor
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:38:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/compare
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:39:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/defaultcheck
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:40:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/description
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:41:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/embedcheck
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:42:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/enumstringer
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:43:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/equal
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:44:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/face
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:45:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/gostring
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:46:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/marshalto
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:47:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/oneofcheck
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:48:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/populate
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:49:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/size
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:50:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/stringer
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:51:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/testgen
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:52:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/union
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:53:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/plugin/unmarshal
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:54:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/proto
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\rpcx\rpcx.go:7:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/protoc-gen-gogo/descriptor
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:55:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/protoc-gen-gogo/generator
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:56:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/protoc-gen-gogo/grpc C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\command\command.go:57:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/protoc-gen-gogo/plugin
C:\Users\Administrator\go\pkg\mod\github.com\rpcxio\[email protected]\protoc-gen-gofast\main.go:32:2: module github.com/gogo/protobuf@latest found (v1.3.1), but does not contain package github.com/gogo/protobuf/vanity

D:\cmder
λ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.