2

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;
}
geostocker
  • 1,190
  • 2
  • 17
  • 29

3 Answers3

0

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.

Bert Verhees
  • 1,057
  • 3
  • 14
  • 25
  • Yeah, so I made that work fine with GRPC too, but the server's definition is completely different from the one the instructor uses. Different interfaces etc, which definitely is doable, but not really what I was looking for. – geostocker Aug 13 '18 at 07:37
  • The interfaces do not matter for the command you have to use to generate code. Of course I have in my project also completely different interfaces and also very complex ones. I did not experience any problem. I am not sure what you are looking for, you must explain that better in your question. – Bert Verhees Aug 13 '18 at 09:15
0

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
Black_Dreams
  • 572
  • 1
  • 5
  • 11
0

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