英文:
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)
}
你尝试过的:
本地测试。请注意:
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)
}
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论