Have a relatively simple helloworld.proto file down below
syntax = "proto3";
package helloworld;
service Greeter { rpc SayHello(HelloRequest) returns (HelloResponse); }
message HelloRequest { string name = 1; }
message HelloResponse { string message = 1; }
When I run protoc --js_out=import_style=commonjs,binary:. .\helloworld.proto it generates a helloworld_pb.js file but it doesn't include my Greeter service nor my SayHello rpc function. Looked around a few other post and also Google's reference (https://developers.google.com/protocol-buffers/docs/reference/overview) and it seems like I need to include a --plugin option but I can't seem to find any. Does anybody have a solution to for this?
The protoc plugin for Node gRPC is distributed in the grpc-tools npm package. That package provides a tool grpc_tools_node_protoc that is a version of protoc that automatically includes the plugin.
As described in that package's README, when you run the tool you will also need to use the --grpc_out argument to control the plugin. The question is tagged grpc-js, so you will probably want to use the grpc_js option for that argument to generate code that interacts with grpc-js.
For those that have been looking for an example that also generates typescript see below
grpc_tools_node_protoc.cmd --js_out=import_style=commonjs,binary:.\output --grpc_out=generate_package_definition:.\output *.proto
grpc_tools_node_protoc.cmd --plugin=protoc-gen-ts.cmd=./node_modules/.bin/protoc-gen-ts --ts_out=.\typescript -I .\output *.proto
Related
In previos products i use old protoc-gen-go which allow to use plugins and generate serialization/deserialization and gRPC client/server in same pb file
as far i understand protoc-gen-go v1.27.1 will not allow plugins and demand to use go-grpc_out flag for client\server code
Follow this command
protoc -I /usr/local/include -I $PWD/api/dummy-proto --go_out=generated --go-grpc_out=generated --go_opt=paths=source_relative proto/v1/foo.proto
i got
generated
|_proto
|_v1
|_dummy
| |_foo_grpc.pb.go //package dummy
|_foo.pb.go //package dummy
Because of created "dummy" folder foo_grpc.pb.go functions do not see Request and Response which generated in foo.pb.go
What i am doing wrong? Is there is option to generate one file as before? It will be work properly after move foo_grpc.pb.go on same level with foo.pb.go.
Also is there is possible to use old flag like --go_out=import_path=" and declare package with M without slashes and without go_options in proto like -go_out=import_path=grpc_v1_proto,M$PWD/proto/v1/foo.proto=grpc_v1_proto"
foo.proto
syntax = "proto3";
package dummy.v1.foo;
option go_package = "proto/v1/dummy";
import "proto/v1/structures.proto";
service FooService {
rpc reverse(ReverseRequest) returns (ReverseResponse);
rpc getBar(GetBarRequest) returns (GetBarResponse);
}
message ReverseRequest {
string text = 1;
}
message ReverseResponse {
string reversed_text = 1;
}
message GetBarRequest {
}
message GetBarResponse {
structures.Bar bar = 1;
}
As per the comments you need to add --go-grpc_opt=paths=source_relative. This is covered in the basics tutorial (but that really just gives the command without much detail).
protoc-gen-go-grpc uses code shared with protoc-gen-go to process most of these options so the documentation for Go Generated Code will probably answer your questions (just change go_opt to go-grpc_opt).
I am trying to create a grpc service with a very basic single action which is GetDeployment, takes a namespace and a name as an input, and returns a Kubernetes deployment. The thing is that I do not want to define my own message for the Deployment as it already exists on the official Kubernetes repository.
I am pretty new to grpc and probably do not understand well enough how it works but can I import this message to my own file in a way I could then write the following .proto file ?
syntax = "proto3";
package api;
import "google/api/annotations.proto";
import "k8s.io/kubernetes/pkg/api/v1/generated.proto";
message GetDeploymentOptions {
string namespace = 1;
string name = 2;
}
service AppsV1 {
rpc GetDeployment(GetDeploymentOptions) returns (k8s.io.kubernetes.pkg.api.v1.Deployment) {}
}
Thank you in advance
GRPC codegen is just a protoc plugin. It generates code for service and rpc but it follows the normal protobuf rules for imports.
In your example, if your file is in src/api.proto and the k8s api repo is a git submodule checked out into thirdparty/k8s.io/api folder you would generate the files you'd need by running:
root>protoc.exe -I thirdparty k8s.io/api/core/v1/generated.proto --go_out=go
root>protoc.exe -I thirdparty src/api.proto --go_out=plugins=grpc:go
The first command is generating the .pb.go file which contains the k8s messages, while the second command is generating the .pb.go file which contains your messages and your service.
Looking at the transient imports of that file, you may also need to checkout api-machinery into k8s.io/apimachinery and run protoc on that file as well.
I've been using protoc to generate golang gRPC client and server code without issues. Since I have multiple gRPC services that use the same data types, I'd like to refer to a base.proto for these types rather than copy and paste, which is extra work and may result in out of sync issues.
Here's a sample of base.proto:
syntax = "proto3";
package base;
message Empty {
}
message Label {
string Key = 1;
string Value = 2;
}
Here's a sample specific .proto:
syntax = "proto3";
import = "base.proto";
package publisher;
service ClientPublisher {
rpc Publish(stream base.Label) returns (base.Empty) {}
}
And, here's my command:
protoc -I system-client-go/ system-client-go/client/publisher.proto --go_out=plugins=grpc:system-client-go --proto_path=system-client-go/
No matter what I try, it throws this:
2019/08/01 15:31:31 protoc-gen-go: error:bad Go source code was generated: 273:7: expected type, found '.' (and 10 more errors)
which corresponds to the line:
rpc Publish(stream base.Label) returns (base.Empty) {}
Any ideas?
This kind of error normally is because your relative path is wrong. Try to put the specific proto file in a directory and import it like
import "exampleproject/specific.proto";
If both files are in the same directory the solution is explained in this thread => https://github.com/golang/protobuf/issues/322
Basically, golang only allows one package per directory. So protoc-gen-go is considering them like 2 separate libraries.
Problem:
I have been coding along to a Golang microservices course on Udemy the last week or so and have encountered a problem.
Basically the instructor has introduced us to Go-Micro and RPC by writing a .proto file. Now I have a bit of experience with GRPC, but none with Go-Micro. The problem is that the instructor doesn't show the actual protoc command and eventual flags, but just brushes over it. I assumed it would be a trivial command, but after running protoc greeter.proto go_out=. I am missing the client snippets..
Expected:
That the pb.go file would look the same as the instructor's, with client side and server snippets in the pb.go file.
Actual:
Missing client snippets.
Command run:
protoc greeter.proto go_out=.
Code:
.proto file:
syntax = "proto3";
service Greeter {
rpc Hello(HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 2;
}
I use this command:
protoc --proto_path=$GOPATH/src:. --micro_out=. --go_out=plugins=grpc:. *.proto
from the directory where the proto-files are. It generates as well service as client code. I found this command in one of the many examples from the go-micro github repository and the go-micro web-site.
This is, however, for use with grpc, but the idea is alright.
Hope this helps :
First one is to generate the proto file and 2nd one is for reverse proxy
In this filename.proto :- filename is your file name
# Generate proto
protoc -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --go_out=google/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:. filename.proto
#Reverse Proxy For REST
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:. filename.proto
Here is an example of protoc command:
protoc proto/employee.proto --go_out=plugins=grpc:.
A new file employee.pb.go will be generated in the proto folder.
If you are looking for a simple example of a Golang gRPC Microservice, please check the post below which has all the steps explained with working code in GitHub:
https://softwaredevelopercentral.blogspot.com/2021/03/golang-grpc-microservice.html
I have just started playing with google proto. When I try to compile proto file present in proto-java example, it does not generate any grpc file.
proto file,
https://github.com/grpc/grpc-java/blob/master/examples/src/main/proto/hello_world.proto
terminal output,
rsonkhla#raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc
--version libprotoc 3.0.0 rsonkhla#raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ protoc
--java_out=test/ -I../../grpc-java/examples ../../grpc-java/examples/src/main/proto/hello_world.proto
rsonkhla#raman-OptiPlex-9020:~/sandbox/grpc-java/examples$ ls -R test/
test/: io
test/io: grpc
test/io/grpc: examples
test/io/grpc/examples: helloworld
test/io/grpc/examples/helloworld: HelloRequest.java
HelloResponse.java HelloWorldProto.java
HelloRequestOrBuilder.java HelloResponseOrBuilder.java
Has anybody else faced this issue?
The command line you are showing does not enable the grpc plugin. You need to specify an _out argument for the grpc plugin, which enables the plugin and specifies where it should output files. Since the plugin is likely not in your PATH, you also need to tell protoc how to find the plugin with --plugin.
So you need to add two arguments:
--plugin=protoc-gen-grpc-java=path/to/protoc-gen-grpc-java --grpc-java_out=path/to/output/dir
For more info, see the gRPC compiler documentation.
You can add these option to your .proto (base on your language) to generate abstract services:
option cc_generic_services = true;
option java_generic_services = true;
option py_generic_services = true;
You can also add --plugin=EXECUTABLE option in your protoc cmd to use custom code generator plugin to generate code more specific to each system, rather than rely on the "abstract" services. Just like Eric's suggestion.