登录
首页 >  Golang >  Go问答

使用反射在 CloudRun 上列出 gRPC 服务的方法

来源:stackoverflow

时间:2024-03-10 12:33:26 376浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《使用反射在 CloudRun 上列出 gRPC 服务的方法》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

按照这个例子:

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})
+       // 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 cloudrun 测试。请注意: 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

回复:

超时

底线。本地测试表明反射工作正常。 部署到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 Reflection 需要双向流,因此请确保在部署时选中启用 HTTP/2 选项 (--use-http2)。这将启用双向流。

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

好了,本文到此结束,带大家了解了《使用反射在 CloudRun 上列出 gRPC 服务的方法》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>