如下示例:
gRPC在Google Cloud Run
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})
+ // 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 try:
是本地测试。请注意:grpcurl -明文表示不是TLS。提交-明文表示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 CloudRun测试。请注意:grpcurl -明文表示不是TLS。提交-明文表示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
反应:超时
底线。本地测试显示反射工作正常。当部署到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);
}
gRPC反射需要双向流,所以确保在部署时勾选启用HTTP/2选项(——use-http2)。这将启用bi-di流。
还要确保使用:443端口,并在需要时通过添加身份验证元数据对服务器进行身份验证(请参阅服务到服务身份验证文档)。