protoc 工具插件和生態


protobuf-go

https://github.com/protocolbuffers/protobuf-go
https://github.com/golang/protobuf
https://github.com/grpc/grpc-go
go get github.com/golang/protobuf/protoc-gen-go

protoc --plugin=protoc-gen-go=$GOPATH/bin/protoc-gen-go --plugin=protoc-gen-micro=$GOPATH/bin/protoc-gen-micro --proto_path=$GOPATH/src:. --micro_out=. --go_out=. greeter.proto

protoc \
  --go_out=Mgrpc/service_config/service_config.proto=/internal/proto/grpc_service_config:. \
  --go-grpc_out=Mgrpc/service_config/service_config.proto=/internal/proto/grpc_service_config:. \
  --go_opt=paths=source_relative \
  --go-grpc_opt=paths=source_relative \
  helloworld/helloworld.proto
  
  protoc -I routeguide/routeguide/route_guide.proto --go_out=plugins=grpc:routeguide --go_out=plugins=grpc:. \

lb

https://github.com/liyue201/grpc-lb
https://github.com/go-kit/kit

protobuf的插件:它們是如何工作

當您調用以下命令時:

protoc -I. --go_out = gen / go foo.proto

以下是(大致)發生的情況:

  1. protoc編譯文件foo.proto(以及所有導入),並在內部生成 FileDescriptorSet,它只是FileDescriptorProto消息的列表。這些消息包含有關.proto文件的所有信息,包括可選的源代碼信息,例如.proto文件中每個元素的開始/結束行/列以及相關的注釋。
  2. FileDescriptorSet被轉換為CodeGeneratorRequest,其中包含protoc為foo.proto所有導入生成的FileDescriptorProtos,指定文件的列表(僅foo.proto在此示例中)以及在或=標記后提供的任何選項。--go_out--go_opt
  3. protoc然后查找名為的二進制文件protoc-gen-go並調用它,將序列化的CodeGeneratorRequest 命名為stdin。
  4. protoc-gen-go運行,並且出錯或產生 CodeGeneratorResponse,它指定要生成的文件及其內容。序列化的CodeGeneratorRequest寫入的stdout protoc-gen-go。
  5. 成功執行時protoc-gen-go,protoc讀取stdout,然后寫入這些生成的文件。
  6. 內置的protoc,即--java_out,--cpp_out等,在大致相同的方式工作,盡管而不是執行外部二進制,這是內部完成到protoc。
  7. FileDescriptorSets是整個Protobuf生態系統使用的原語,表示已編譯的Protobuf模式。它們也是協議產生的主要工件。
  8. 就是說,您所做的一切protoc以及您使用的任何插件都使用FileDescriptorSets進行交談。值得注意的是,它們也是gRPC Reflection在后台運行的方式。

https://github.com/nilslice/protolock

協議緩沖區配套工具。
跟蹤您的.proto文件,並防止更改影響API兼容性的消息和服務。

https://github.com/uber/prototool

您的瑞士軍刀用於協議緩沖區

如何使用協議創建FileDescriptorSets

protoc提供--descriptor_set_out別名為的標志,-o以允許編寫序列化的FileDescriptorSets。例如,給定一個文件foo.proto,您可以將FileDescriptorSet寫入stdout,如下所示:

protoc -I. -o / dev / stdout foo.proto

生成的FileDescriptorSet將包含一個名為name的FileDescriptorProto foo.proto。
默認情況下,FileDescriptorSets將不包含命令行上未指定的任何導入,並且不包含源代碼信息。源代碼信息對於在生成的存根內部生成文檔,以及諸如 linters and breaking變更檢測器之類的東西很有用。例如,假設foo.protoimport bar.proto。要產生同時包含foo.proto和的FileDescriptorSet bar.proto以及源代碼信息,請執行以下操作:

protoc -I . --include_imports --include_source_info -o /dev/stdout foo.proto

protoc-gen-micro

https://github.com/Leon2012/protoc-gen-micro-lite
https://github.com/Allenxuxu/protoc-gen-mMicro

-I參數:指定import路徑,可以指定多個-I參數,編譯時按照順序查找,不指定時默認查找當前目
--go_out:Golang編譯支持,支持以下參數
    - `plugins=plugin1+plugin2`:指定插件,支持grpc/micro,即:plugins=grpc+micro
    - `M`參數:指定導入的.proto文件路徑編譯后對應的goalng包名(不指定默認.proto文件中import語句路徑)
    - `import_prefix=xxx`:為所有import路徑添加前綴,主要用於編譯子目錄內的多個proto文件
    - `import_path=foo/bar`:指定未聲明package或go_package的文件的包名,最右邊的斜線前的字符會被忽略
GOPATH:=$(shell go env GOPATH)

PB_PATH=./pb
GEN_PATH=github.com/micro-in-cn/starter-kit/console/${gen}

proto:
	rm -rf ${gen}/*

protoc --proto_path=${GOPATH}/src:${PB_PATH} \
	--micro_out=Msrv/account.proto=${GEN_PATH}/srv,Mgithub.com/micro/go-micro/api/proto/api.proto=github.com/micro/go-micro/v2/api/proto:${gen} \
	--go_out=Msrv/account.proto=${GEN_PATH}/srv,Mgithub.com/micro/go-micro/api/proto/api.proto=github.com/micro/go-micro/v2/api/proto:${gen} \
	--validate_out="lang=go:${gen}" \
	api/account.proto

	protoc --proto_path=${GOPATH}/src:${PB_PATH} \
	--micro_out=${gen} \
	--go_out=:${gen} \
	--validate_out="lang=go:${gen}" \
	srv/account.proto

protoc-gen-go

github.com/golang/protobuf/protoc-gen-go

protoc-gen-validate

https://github.com/envoyproxy/protoc-gen-validate

protoc \
  -I . \
  -I ${GOPATH}/src \
  -I ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate \
  --go_out=":../generated" \
  --validate_out="lang=go:../generated" \
  example.proto

protoc-gen-gogofast

更快的序列化和反序列化方法
更規范的Go數據結構
兼容 go protobuf
非常多的輔助方法
可以產生測試代碼和benchmark代碼
其它序列化格式

github.com/gogo/protobuf/protoc-gen-gogofast
protoc-gen-gogo:和protoc-gen-go生成的文件差不多,性能也幾乎一樣(稍微快一點點)
protoc-gen-gofast:生成的文件更復雜,性能也更高(快5-7倍)
protoc-gen-gofast (在gofast一基礎上, 可以導入gogoprotobuf)
protoc-gen-gogofast (在gofast一基礎上, 可以導入gogoprotobuf)
protoc-gen-gogofaster (在gogofast基礎上, 去掉XXX_unrecognized方法, 更少的指針字段)
protoc-gen-gogoslick (在gogofaster基礎上, 輔助方法string, gostring 和 equal)

//gogo
protoc --gogo_out=. *.proto

//gofast
protoc --gofast_out=. *.proto

protoc --gofast_out=plugins=rpcx:. helloworld.proto
protoc --gogofast_out=plugins=rpcx:. helloworld.proto
protoc --gogofaster_out=plugins=rpcx:. helloworld.proto
protoc --gogoslick_out=plugins=rpcx:. helloworld.proto

protoc-gen-doc

https://github.com/pseudomuto/protoc-gen-doc
go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
--doc_out=html,index.html:$p/doc
--doc_opt= |<TEMPLATE_FILENAME>,<OUT_FILENAME>

protoc \
  --plugin=protoc-gen-doc=./protoc-gen-doc \
  --doc_out=./doc \
  --doc_opt=html,index.html \
  proto/*.proto
  
  protoc --doc_out=./doc --doc_opt=/path/to/template.tmpl,index.txt proto/*.proto
https://github.com/pseudomuto/protoc-gen-doc/blob/master/examples/proto/Booking.proto

<!---->
/**
 * This is a leading comment for a message
*/
message SomeMessage {
  // this is another leading comment
  string value = 1;
}

<!---->
enum MyEnum {
  DEFAULT = 0; // the default value
  OTHER   = 1; // the other value
}

<!---->
/**
 * @exclude
 * This comment won't be rendered
 */
message ExcludedMessage {
  string id   = 1; // the id of this message.
  string name = 2; // @exclude the name of this message

  /* @exclude the value of this message. */
  int32 value = 3;
}

go-proto-validators

protoc-gen-star

https://github.com/lyft/protoc-gen-star
protoc插件庫,用於高效地生成基於proto的代碼

protoc-gen-grpc-gateway

github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

protoc -I/usr/local/include -I. \
    -I$GOPATH/src \
    -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
    --grpc-gateway_out=logtostderr=true:. *.proto 
    
protoc -I. \
  --grpc-gateway_out=logtostderr=true,paths=source_relative:./gen/go \
  your/service/v1/your_service.proto

protoc -I$GOPATH/src/ -I./ \
    --swagger_out=logtostderr=true,grpc_api_configuration=api/api.yaml,allow_merge=true,merge_file_name=api/api:. \
    api/*.proto

protoc-gen-swagger

github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

protoc -I/usr/local/include -I. \
  -I$GOPATH/src \
  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  --swagger_out=logtostderr=true:. *.proto
  
  --swagger_out=logtostderr=true,repeated_path_param_separator=ssv:.

prototool buf

https://github.com/bufbuild/buf
一種使用協議緩沖區的新方法
https://buf.build
Buf的目標是讓Protobuf不僅在技術優點上成為一個不錯的選擇,而且易於使用,以至於決定不重要。您的組織不必為了有效地使用Protobuf而重新發明輪子。不必擔心您的Protobuf管理策略失控。我們會為您處理,因此您可以擔心重要的事情。

安裝
https://github.com/bufbuild/buf/releases

brew tap bufbuild/buf
brew install buf

protoc-gen-buf-check-breaking
protoc-gen-buf-check-lint

protoreflect

Go Protocol Buffers的反射
https://github.com/jhump/protoreflect


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM