如何在 CloudRun 上反射 gRPC 服务的 grpcurl 列表?

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

下面这个例子:

Google 云中的 gRPC 运行

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

我已经在云上部署了带有反射的 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})
+       // 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

你尝试了什么:

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

有效的方法:

############################
# 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
}
############################

你尝试了什么: GCP Cloud运行 测试。请注意: grpcurl -plaintext 表示不是 TLS。省略 -plaintext 表示 TLS。

有效的方法:

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

result:
{
  "result": 5
}

什么不起作用: 我想用反射所以省略:

-proto protos/calculator.proto \

我想用TLS所以省略:

-plaintext

要求:

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

回复:

超时

底线。本地测试表明反射工作正常。 部署到云端时无法工作运行.

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

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);
}

gRPC 反射需要双向流,所以确保在部署时选中启用 HTTP/2 选项 (--use-http2)。这将启用双向流。

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