0

https://pastebin.com/7vqdrHyg greet.proto

https://pastebin.com/4PYDYZ6Q greet_grpc.pb.go

https://pastebin.com/2n6n8JjS greet_pb.go

https://pastebin.com/FPpCJEGR main.go

https://pastebin.com/CXuxG5fB handler/greet.go

I have implemented a simple grpc server using Golang. The server accepts a connection from a client and returns a simple response message.

I have a Python client that is trying to connect to the server through localhost:8080. The gRPC code for the client and server are created with the same IDL interface.

https://pastebin.com/RLp3QUWX client.py

However, the connection is unsuccessful and I got the following error. https://pastebin.com/Cji9Pkhj

I don't know how GreetService is called at the server side, but I assumed that the interface is defined somewhere in greet_grpc.pb.go

UPDATE: I have pushed the code to this repo for better readability.

disguisedtoast
  • 149
  • 1
  • 4
  • 15
  • Please consider alternatives to PasteBin'ning each file. It makes it difficult to read your code and I think it makes it difficult for you to maintain your code (it contains error and does not compile). I encourage you to consider either pasting source directly into the question or to reference the code in e.g. a Git repo or Gist. – DazWilkin Oct 30 '22 at 21:57
  • After recreating your code, correcting your errors (per your `greet.proto`, the Golang sources are generated in `"grpc-service/proto/greet"` **not** as your PasteBin code references `grpc-service/grpc-service/greet/go"`, your code works for me. – DazWilkin Oct 30 '22 at 21:59
  • I encourage you to follow conventions in your code. (a) Your Go code imports the Protobuf and aliases as `greet` but the convention is to use `pb`; (b) Your Go code creates a type `greet` for the implementation of `GreetServiceServer` but the convention is to call the type `server`. These may appear trifling but it reduces readability because your code is not as-expected and this increases the effort for others to understand your code. – DazWilkin Oct 30 '22 at 22:07
  • To be correct, your Python client should provide a `name` field value for `GreetingRequest` i.e. `request = GreetingRequest(name="Fred")` and, you should not ignore the `GreetingResponse`, i.e. `response = client.Greeting(request)` and then e.g. `print(response)`. – DazWilkin Oct 30 '22 at 22:09
  • Lastly (!) and to help you debug your issue(s), your Go server should import `"google.golang.org/grpc/reflection"` and you can then `reflection.Register(s)` and this then enables you to use e.g. [`gRPCurl`](https://github.com/fullstorydev/grpcurl) to reflect against your running service i.e. `grpcurl -plaintext localhost:8080 list` – DazWilkin Oct 30 '22 at 22:11
  • @DazWilkin Hi Daz, thanks for the feedback. I have pushed the code to GitHub, so it should be easier to troubleshoot the code. – disguisedtoast Oct 31 '22 at 16:34
  • I have tried to implement the things mentioned in your feedback, but I still could not connect my client to the server. `grpcurl -plaintext localhost:8080 list greet.GreetService` gives me `greet.GreetService.Greeting`, so at least I know that the `Greeting` API is exposed. – disguisedtoast Oct 31 '22 at 16:54
  • Thanks for considering my feedback. The repo helps! The issue is that you're duplicating the proto file for the client and server. You should really try to share the same file between the client and server. The proto file represents the interface and -- one problem with duplicating protos -- is that (as you've done) you can introduce differences. The client version of the proto does not include `package greet` whereas the server version does. For this reason, the client implements a different service (one not under `greet`) than the server, hence your error. – DazWilkin Oct 31 '22 at 16:58
  • I encourage you to delete the separate `pb/proto` directories in the client and the server and share one between both. Regenerate the protobufs and it will work. – DazWilkin Oct 31 '22 at 16:58
  • I created a shared directory for generating the gRPC bindings. But `GreetingRequest` and `GreetingResponse` structs inside `grpc/greet/proto_out/greet_grpc_pb.go` are undefined. I modified my Makefile, but I don't think that is the issue. – disguisedtoast Nov 01 '22 at 06:09
  • I've helped you identify the original issue which was that your duplicated proto had different packages. It sounds as though you're now having a different issue. – DazWilkin Nov 01 '22 at 16:06
  • @DazWilkin I moved everything (grpc code generation, driver) under the same folder. Generated the client and server gRPC code and ran the client and server code with no issue. Thanks for your help and sorry for the trouble. – disguisedtoast Nov 01 '22 at 16:46

0 Answers0