Unable to figure out missing protoc command from udemy course - go

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

Related

WHat is the difference between the filename_pb2_grpc.py and the filename_pb2.py files?

From what I understand, the pb2.py file is code from the protoc file and the pb2_grpc.py file is code generated by protoc from the .proto file. But I don't understand what that means. I am very confused, can someone please explain this to me.
Thank you
I have tried to google the question, but I couldn't find anything that I could understand
Both are generated by protoc or python -m grpc_tools.protoc (a Python module that wraps protoc).
A pb2.py file contains the core Protocol Buffer (message) definitions and is created as a result of the --python_out flag being specified.
A pb2_grpc.py file contains the service and rpc (method) definitions and is created as a result of the --grpc_python_out flag being specified.

protoc --go-grpc_out generate code in out of vision of --go_out generated code

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).

NodeJS: protoc generated files not generating service definition

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

Running GRPC example for golang

newbie here.
Currently learning how grpc works and was going through the tutorial in this link
https://grpc.io/docs/quickstart/go.html#update-and-run-the-application
When I ran the example using the helloworld.pb.go file provided, it works. But when I deleted that file and ran protoc --go_out=plugins=grpc:. *.proto to generate the that file again, I find that I can no longer run the greeter server.
The error I am getting is
google.golang.org/grpc/examples/helloworld/helloworld
helloworld/helloworld.pb.go:105:11: cannot use _Greeter_SayHello_Handler (type func(interface {}, "context".Context, grpc.Codec, []byte) (interface {}, error)) as type grpc.methodHandler in field value
Tried to google but couldn't find out why. My protobuf version is 3.5.1 and the grpc is freshly cloned from github.com/golang/protobuf/protoc-gen-go
Generate proto like this
file_name : Your proto file name with the path
//To generate the grpc code
protoc proto/file_name.proto --go_out=plugins=grpc:.
Take a look this link How to write proto and generate the proto file. It may help your problem
https://github.com/SXerox007/protos-

protoc not generating service stub files

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.

Resources