如何在CloudRun上使用反射(reflection)来列出gRPC服务的grpcurl列表?

huangapple go评论77阅读模式
英文:

How to grpcurl list for gRPC service with reflection on CloudRun?

问题

以下是翻译好的内容:

根据这个示例:

在Google Cloud Run中使用gRPC

https://github.com/grpc-ecosystem/grpc-cloud-run-example/blob/master/golang/README.md

我已经在CloudRun上部署了一个带有反射的gRPC服务。

使用grpcurl进行测试:
https://github.com/fullstorydev/grpcurl

grpcurl \
-proto protos/calculator.proto \
-d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
${ENDPOINT}:443 \
Calculator.Calculate

GRPC服务器反射协议
https://github.com/grpc/grpc/blob/master/doc/server-reflection.md

现在我想按照这些说明使用反射。

--- a/examples/helloworld/greeter_server/main.go
+++ b/examples/helloworld/greeter_server/main.go
@@ -40,6 +40,7 @@ import (
        "google.golang.org/grpc"
        pb "google.golang.org/grpc/examples/helloworld/helloworld"
+       "google.golang.org/grpc/reflection"
 )

 const (
@@ -61,6 +62,8 @@ func main() {
        }
        s := grpc.NewServer()
        pb.RegisterGreeterService(s, &pb.GreeterService{SayHello: sayHello})
+       // 在gRPC服务器上注册反射服务。
+       reflection.Register(s)
        if err := s.Serve(lis); err != nil {
                log.Fatalf("failed to serve: %v", err)
        }

https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md#enable-server-reflection

你尝试过的:

本地测试。请注意:
grpcurl -plaintext 表示不使用TLS。省略 -plaintext 表示使用TLS。

成功的部分:

############################
# 本地测试
############################
请求列表:
grpcurl -plaintext localhost:8080 list

结果:
Calculator
grpc.reflection.v1alpha.ServerReflection

请求 ADD 函数:
grpcurl -plaintext \
    -d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
    localhost:8080 \
    Calculator.Calculate

结果:
{
  "result": 5
}
############################

你尝试过的:
GCP CloudRun 测试。请注意:
grpcurl -plaintext 表示不使用TLS。省略 -plaintext 表示使用TLS。

成功的部分:

请求:
grpcurl \
    -proto protos/calculator.proto \
    -d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
    ${ENDPOINT}:443 \
    Calculator.Calculate

结果:
{
  "result": 5
}

未成功的部分:
我想使用反射,所以省略了:

-proto protos/calculator.proto \

我想使用TLS,所以省略了:

-plaintext

请求:

grpcurl \
    -d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
    ${ENDPOINT}:443 \
    Calculator.Calculate

响应:

超时

总之,本地测试显示反射工作正常。
但是在部署到CloudRun时无法工作。

我猜是因为它需要双向流:

https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto

service ServerReflection {
  // The reflection service is structured as a bidirectional stream, ensuring
  // all related requests go to a single server.
  rpc ServerReflectionInfo(stream ServerReflectionRequest)
      returns (stream ServerReflectionResponse);
}
英文:

Following this example:

gRPC in Google Cloud Run

https://github.com/grpc-ecosystem/grpc-cloud-run-example/blob/master/golang/README.md

I've deployed a gRPC service with reflection on CloudRun.

Using grpcurl for testing:
https://github.com/fullstorydev/grpcurl

grpcurl \
-proto protos/calculator.proto \
-d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
${ENDPOINT}:443 \
Calculator.Calculate

GRPC Server Reflection Protocol
https://github.com/grpc/grpc/blob/master/doc/server-reflection.md

Now I want to use reflection following these instructions.

--- a/examples/helloworld/greeter_server/main.go
+++ b/examples/helloworld/greeter_server/main.go
@@ -40,6 +40,7 @@ import (
        "google.golang.org/grpc"
        pb "google.golang.org/grpc/examples/helloworld/helloworld"
+       "google.golang.org/grpc/reflection"
 )

 const (
@@ -61,6 +62,8 @@ func main() {
        }
        s := grpc.NewServer()
        pb.RegisterGreeterService(s, &pb.GreeterService{SayHello: sayHello})
+       // Register reflection service on gRPC server.
+       reflection.Register(s)
        if err := s.Serve(lis); err != nil {
                log.Fatalf("failed to serve: %v", err)
        }

https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md#enable-server-reflection

what you tried:

a local test. Please note:
grpcurl -plaintext means not TLS. Ommiting -plaintext means TLS.

what worked:

############################
# local testing
############################
request list:
grpcurl -plaintext localhost:8080 list

result:
Calculator
grpc.reflection.v1alpha.ServerReflection

request ADD function:
grpcurl -plaintext \
    -d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
    localhost:8080 \
    Calculator.Calculate

result:
{
  "result": 5
}
############################

what you tried:
a GCP CloudRun test. Please note:
grpcurl -plaintext means not TLS. Ommiting -plaintext means TLS.

what worked:

request:
grpcurl \
    -proto protos/calculator.proto \
    -d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
    ${ENDPOINT}:443 \
    Calculator.Calculate

result:
{
  "result": 5
}

what didn’t work:
I want to use reflection so omitted:

-proto protos/calculator.proto \

I want to use TLS so omitted:

-plaintext

request:

grpcurl \
    -d '{"first_operand": 2.0, "second_operand": 3.0, "operation": "ADD"}' \
    ${ENDPOINT}:443 \
    Calculator.Calculate

response:

timeout

Bottom line. The local test shows reflection is working fine.
It cannot work when deployed to CloudRun.

I suppose because it requires bidirectional stream:

https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto

service ServerReflection {
  // The reflection service is structured as a bidirectional stream, ensuring
  // all related requests go to a single server.
  rpc ServerReflectionInfo(stream ServerReflectionRequest)
      returns (stream ServerReflectionResponse);
}

答案1

得分: 2

gRPC Reflection 需要双向流式传输,请确保在部署时检查启用 HTTP/2 选项(--use-http2)。这将启用双向流式传输。

此外,请确保使用 :443 端口,并根据需要通过添加身份验证元数据(参见服务对服务身份验证文档)对服务器进行身份验证。

英文:

gRPC Reflection requires bidirectional streaming, so make sure to check the Enable HTTP/2 option (--use-http2) while deploying. That will enable bi-di streaming.

Also make sure to use the :443 port and authenticate to the server if needed by adding Authentication metadata (see Service-to-Service authentication documentation).

huangapple
  • 本文由 发表于 2021年3月7日 04:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/66510394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定