登录
首页 >  Golang >  Go问答

如何请求在Cloud Run上运行的Go制作的gRPC服务器

来源:stackoverflow

时间:2024-04-07 12:51:34 501浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何请求在Cloud Run上运行的Go制作的gRPC服务器》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我用go lang制作了grpc服务器。

https://github.com/takat0-h0rikosh1/grpc-with-go

这是一个 cafe-cli 应用程序。

我将容器注册到 gcr,并部署到 cloud run。

# docker build
docker build -t cafe .

# register image
$ docker tag cafe gcr.io/xxx/cafe
$ docker push gcr.io/xxx/cafe 

# deploy to cloud run
$ gcloud beta run deploy --project xxx --image gcr.io/xxx/cafe
please choose a target platform:
 [1] cloud run (fully managed)
 [2] cloud run for anthos deployed on google cloud
 [3] cloud run for anthos deployed on vmware
 [4] cancel
please enter your numeric choice:  1

to specify the platform yourself, pass `--platform managed`. or, to make this the default target platform, run `gcloud config set run/platform managed`.

service name (cafe):  cafe-service
allow unauthenticated invocations to [cafe-service] (y/n)?  n

deploying container to cloud run service [cafe-service] in project [xxx] region [us-central1]
✓ deploying new service... done.
  ✓ creating revision...
  ✓ routing traffic...
done.
service [cafe-service] revision [cafe-service-00001-pal] has been deployed and
is serving 100 percent of traffic at https://cafe-service-xxx-uc.a.run.app

如何向该 grpc 服务器发出请求?

我尝试了以下方法。

$ curl -H \
  "Authorization: Bearer (gcloud auth print-identity-token)" \
  https://cafe-service-xxx-uc.a.run.app

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL <code>/</code>.</h2>
<h2></h2>
</body></html>

$ grpcurl -plaintext cafe-service-xxx-uc.a.run.app:443 list
Failed to dial target host "cafe-service-xxx-uc.a.run.app:443": context deadline exceeded

$ grpcurl -authority (gcloud auth print-identity-token) -plaintext cafe-service-xxx-uc.a.run.app:443 list
Failed to dial target host "cafe-service-xxx-uc.a.run.app:443": context deadline exceeded

我必须做什么?


解决方案


首先要尝试的是让事情继续下去,创建一个新服务并允许未经身份验证:

“允许未经身份验证的调用 [](y/n)?y”

其次,如果您尝试通过身份验证访问 grpc,请检查:

https://medium.com/google-cloud/grpc-authentication-with-cloud-run-72e4d6c44739

curl 命令中的授权标头错误。 gcloud 命令之前缺少 $。使用这个代替:

curl -H \
  "Authorization: Bearer $(gcloud auth print-identity-token)" \
  https://cafe-service-xxx-uc.a.run.app

好了,本文到此结束,带大家了解了《如何请求在Cloud Run上运行的Go制作的gRPC服务器》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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