使用反射在 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知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习