簡體   English   中英

如何請求在 Cloud Run 上運行的 Go 制作的 gRPC 服務器

[英]How to request gRPC server made by Go running on Cloud Run

我通過 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”

其次,如果您嘗試使用 auth 訪問 gRPC,請檢查:

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

curl 命令中的 Authorization 標頭是錯誤的。 在 gcloud 命令之前缺少$ 改用這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM