简体   繁体   English

protoc-gen-go-grpc:程序未找到或不可执行

[英]protoc-gen-go-grpc: program not found or is not executable

go version : go version go1.14 linux/amd64 go version :go 版本 go1.14 linux/amd64

go.mod go.mod

module [redacted]

go 1.14

require (
    github.com/golang/protobuf v1.4.0-rc.2
    google.golang.org/grpc v1.27.1
    google.golang.org/protobuf v1.20.0 // indirect
)

I am running the following command:我正在运行以下命令:

protoc -I ./src/pbdefs/protos/ --go-grpc_out=. src/pbdefs/protos/*.proto

to generate my GRPC output files from .proto files, with I am getting an error从 .proto 文件生成我的 GRPC .proto文件,但出现错误

protoc-gen-go-grpc: 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
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.

the missing plugin has been implemented at https://github.com/grpc/grpc-go .缺失的插件已在https://github.com/grpc/grpc-go实现。

command below should fix it下面的命令应该修复它

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

The Golang Protobuf has released a new version of Go protocol buffers which they are calling it as the APIv2. Golang Protobuf 发布了新版本的 Go 协议缓冲区,他们将其称为 APIv2。

Because APIv2 is not backwards compatible with APIv1, therefore we will need to adapt all our Golang code for the new API.由于 APIv2 不向后兼容 APIv1,因此我们需要为新 API 调整所有 Golang 代码。

You can learn more about the new API here and here您可以在此处此处了解有关新 API 的更多信息

Migration steps:迁移步骤:

In order to run the new code generation will be necessary to install the following gRPC gen plugins:为了运行新的代码生成,需要安装以下 gRPC gen 插件:

go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go

go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc

Then use the following command to generate the code.然后使用以下命令生成代码。

  # generate the messages
 protoc --go_out="$GO_GEN_PATH" -I "$dependecies" "$proto"

 # generate the services
 protoc --go-grpc_out="$GO_GEN_PATH" -I "$dependecies" "$proto"

OK, just found out, as per https://github.com/protocolbuffers/protobuf-go/releases/tag/v1.20.0好的,刚刚发现,根据https://github.com/protocolbuffers/protobuf-go/releases/tag/v1.20.0

The v1.20 protoc-gen-go does not support generating gRPC service definitions. v1.20 protoc-gen-go不支持生成 gRPC 服务定义。 In the future, gRPC service generation will be supported by a new protoc-gen-go-grpc plugin provided by the Go gRPC project.未来,gRPC 服务生成将由 Go gRPC 项目提供的新 protoc-gen-go-grpc 插件支持。

The github.com/golang/protobuf version of protoc-gen-go continues to support gRPC and will continue to do so for the foreseeable future. github.com/golang/protobuf -gen-go 的github.com/golang/protobuf版本继续支持 gRPC,并将在可预见的未来继续支持。

EDIT 29/05/2020:编辑 29/05/2020:

Following an update from @Mark in the comments, according to the Tracking issue on github , protoc-gen-go-grpc has now been merged.根据评论中@Mark 的更新,根据github 上跟踪问题protoc-gen-go-grpc现已合并。 However, according to the same issue:但是,根据同一个问题:

Even with this merged, it is unreleased (v0.0) and subject to change.即使合并后,它也未发布(v0.0)并且可能会发生变化。 We do plan to add a requirement that the Unimplemented service implementation be included in all registered services, with an escape hatch in the form of a command-line arg as suggested by @neild.我们确实计划添加一项要求,即未实现的服务实现包含在所有已注册的服务中,并按照@neild 的建议以命令行参数的形式提供一个逃生舱口。

If you haven't done, so you need to install the protoc-gen-go plugin like so:如果你还没有这样做,那么你需要像这样安装protoc-gen-go插件:

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

this will install the plugin (if your GOPATH is ${HOME}/go ) to:这会将插件(如果您的GOPATH${HOME}/go )安装到:

${HOME}/go/bin/protoc-gen-go

Then when running protoc , either update your path, or set it dynamically like so:然后在运行protoc ,要么更新你的路径,要么像这样动态设置它:

PATH="${PATH}:${HOME}/go/bin" protoc ...

For all who aren't much into the topic (like me) and still have trouble to figure out a working solution, here's a step-by-step approach:对于所有对这个话题不太感兴趣(像我一样)并且仍然难以找到可行解决方案的人,这里有一个循序渐进的方法:

  1. apt install protobuf-compiler installs the compiler under apt install protobuf-compiler , available via protoc from then. apt install protobuf-compilerapt install protobuf-compilerapt install protobuf-compiler ,从那时起可以通过protoc获得。
  2. Install the old go generator plugin to be used by protoc : go get -u google.golang.org/protobuf/cmd/protoc-gen-go and go install google.golang.org/protobuf/cmd/protoc-gen-go .安装老去发电机插件通过使用protocgo get -u google.golang.org/protobuf/cmd/protoc-gen-gogo install google.golang.org/protobuf/cmd/protoc-gen-go Also, make sure that the installed plugin can be found in $PATH or add it with export PATH=$PATH:$(go env GOPATH)/bin if needed.另外,请确保可以在$PATH找到已安装的插件,或者在需要时使用export PATH=$PATH:$(go env GOPATH)/bin添加它。
  3. To tell that plugin not only to generate the protobuf message type information but also the grcp methods, use a command like protoc --go_out=plugins=grpc:my/relative/output/path ./my_file.proto .要告诉该插件不仅生成 protobuf 消息类型信息而且生成 grcp 方法,请使用类似protoc --go_out=plugins=grpc:my/relative/output/path ./my_file.proto

Looks kinda trivial once you've figured that out, but it's quite hard to figure that out if you aren't into that topic and only have scarce information about how the go files generator generator and the grcp extension are supposed to work together.一旦你弄清楚了,看起来有点微不足道,但是如果你不关心这个话题并且只有很少的关于 go 文件生成器生成器和 grcp 扩展应该如何协同工作的信息,就很难弄清楚。

我一直在寻找一个好的答案,最后,它对我有用:

protoc --go-grpc_out=. file_path/file_name*.proto 

i solve using this command :我使用这个命令解决:

  • protoc calculator/calculatorpb/calculator.proto --go-grpc_out=. protoc 计算器/calculatorpb/calculator.proto --go-grpc_out=.
  • protoc -I=.协议 -I=. --go_out=. --go_out=. calculator/calculatorpb/calculator.proto计算器/计算器pb/calculator.proto

to generate calculator_pb.go and calculator_grpc.go生成calculator_pb.go 和calculator_grpc.go

syntax = "proto3";

package calculator;
option go_package = "calculator/calculatorpb";

message SumRequest {
  int32 first_number = 1;
  int32 second_number = 2;
}

message SumResponse {
  int32 sum_result =1 ;
}

service CalculatorService{
  rpc Sum(SumRequest) returns(SumResponse) {}
}

I had to install all these:我必须安装所有这些:

go install \
   github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
   github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
   google.golang.org/protobuf/cmd/protoc-gen-go \
   google.golang.org/grpc/cmd/protoc-gen-go-grpc

Use go get to download the following packages:使用go get下载以下软件包:

$ go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
$ go get google.golang.org/protobuf/cmd/protoc-gen-go
$ go get google.golang.org/grpc/cmd/protoc-gen-go-grpc

This installs the protoc generator plugins we need to generate the stubs.这将安装我们需要生成存根的protoc生成器插件。 Make sure to add $GOPATH/bin to your $PATH so that executables installed via go get are available on your $PATH .确保将$GOPATH/bin添加到您的$PATH以便通过go get安装的可执行文件在您的$PATH上可用。

Here's an example of what a protoc command might look like to generate Go stubs, assuming that you're at the root of your repository and you have your proto files in a directory called proto :下面是一个protoc命令生成 Go 存根的示例,假设您位于存储库的根目录,并且您的 proto 文件位于名为proto的目录中:

$ protoc -I ./proto \
   --go_out ./proto --go_opt paths=source_relative \
   --go-grpc_out ./proto --go-grpc_opt paths=source_relative \
   ./proto/helloworld/hello_world.proto

We use the go and go-grpc plugins to generate Go types and gRPC service definitions.我们使用gogo-grpc插件来生成 Go 类型和 gRPC 服务定义。 We're outputting the generated files relative to the proto folder, and we're using the paths=source_relative option, which means that the generated files will appear in the same directory as the source .proto file.我们正在输出相对于proto文件夹的生成文件,并且我们使用了paths=source_relative选项,这意味着生成的文件将与源.proto文件出现在同一目录中。

You should check out the tutorial series on gRPC-Gateway, ie, https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/ .您应该查看关于 gRPC-Gateway 的系列教程,即https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/ Also, you can refer to my simple hello world program, which uses gRPC-Gateway, ie, https://github.com/iamrajiv/helloworld-grpc-gateway .另外,您可以参考我的简单 hello world 程序,该程序使用 gRPC-Gateway,即https://github.com/iamrajiv/helloworld-grpc-gateway

Share my useful bash command here:在这里分享我有用的 bash 命令:

go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go

go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc

protoc --proto_path=./proto ./proto/*.proto --plugin=$(go env GOPATH)/bin/protoc-gen-go-grpc --go-grpc_out=./pb 

protoc --proto_path=./proto ./proto/*.proto --plugin=$(go env GOPATH)/bin/protoc-gen-go --go_out=./pb

您还可以使用https://github.com/storyicon/powerproto 一键安装所有与 protoc 相关的依赖项(包括 protoc-gen-go-grpc)并进行版本控制。

更新您的 PATH 以便 protoc 编译器可以找到插件: export PATH="$PATH:$(go env GOPATH)/bin"

Please check the Go Environment variables by running the Go env command on the Terminal and make sure that the following values are set.请通过在终端上运行Go env命令检查 Go 环境变量,并确保设置了以下值。

GOBIN=some folder location GOBIN=某个文件夹位置

GOPATH= some folder location GOPATH=某个文件夹位置

If these are looking good try installing the go plugin如果这些看起来不错,请尝试安装 go 插件

go get -u google.golang.org/grpc

or run the following on the terminal或在终端上运行以下命令

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM