添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have added below new code in protobuf file and compiled to get the generated grpc_pb files.

service EchoService {
  rpc Echo(EchoMessage) returns (EchoMessage) {
    #-----New code start-----
    option (google.api.http) = {
      post: "/v1/echo"
      body: "*"
    #-----New code end------

From cURL command executed below command

curl -X POST -k https://localhost:10000/v1/echo -d '{"Key": "Value"}'

After making above request, not able to get the proper response.

My doubt is, any server side code changes needed to prepare the response to send back to caller? If so, Please suggest me with the code (Java) & also how to make request. If not, how we need to make http request to grpc?

Working example is much appreciated.

In order to test gRPC server without client, we have to use grpcurl not curl. Please take a look at https://github.com/fullstorydev/grpcurl

However, based on my experience there is a requirement to make it works. First, please ensure that your service support Reflection, you can read about it from https://github.com/sourcegraph/gophercon-2018-liveblog/issues/27. There are different ways of doing Reflection across programming languages. My advice is, just do this for the development phase, otherwise, people may querying your gRPC endpoint. Maybe you can use if() to make a conditional block for it. For Golang, i did this

import "google.golang.org/grpc/reflection"
if os.Getenv("GO_ENV") == "development" {
    reflection.Register(s)

then, you need to know available services in your gRPC server. There is two way you can know the structure. First You can read them from your proto file, second by executing command grpcurl localhost:10000 list

  • If you like to read from your proto file, the path should be packageName.Service/rpcMethodName. So, based on your proto, it should be something like EchoService/Echo or if you have package name it will be packageName.EchoService/Echo
  • If you prefer to do grpcurl localhost:10000 list, just type those command and it will output service path.
  • The last thing to note when you test it locally and you don't setup SSL/TLS, please use -plaintext option, otherwise, it will tell you that TLS handshake failed.

    Example command, based on your proto, a call in local will looks like:

    grpcurl -plaintext -d '{"Key": "Value"}' 127.0.0.1:10000 EchoService/Echo

    Hope it helps.

    UPDATE 30 June 2020:

    After a few months working with gRPC I found another interesting gRPC tool:

  • Interactive tool: https://github.com/ktr0731/evans
  • even easier GUI tool: https://github.com/uw-labs/bloomrpc
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.