在 Google Cloud 上的微服务架构中部署示例事务工作流应用


本文档介绍如何使用 Cloud RunPub/SubWorkflowsDatastore 模式的 Firestore (Datastore) 来部署示例应用。本文档面向想要在基于微服务的应用中实现事务工作流的应用开发者。

本文档是系列文章中的一篇,该系列文章包括以下内容:

您在本教程中部署的示例应用实现了下面两种架构模式的微服务:

  • 基于编排的 Saga
  • 同步编排

该应用包含一个 Web 客户端。您可以通过 Google Cloud CLI 和 Web 客户端试用这两种模式。

目标

  • 为基于编排的 Saga 架构部署服务器端组件
  • 为同步编排架构部署服务器端组件
  • 使用 curl 命令测试服务器端组件
  • 部署 Web 应用并通过该应用执行工作流

费用

在本文档中,您将使用 Google Cloud 的以下收费组件:

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理

准备工作

  1. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  2. 确保您的 Google Cloud 项目已启用结算功能

  3. 启用 Cloud Services, Cloud Run, Workflows, Cloud Build, and Cloud Scheduler API。

    启用 API

克隆源代码

在本部分中,您将在 Cloud Shell 中设置项目 ID 并克隆源代码。

  1. 在 Cloud Shell 中,设置项目 ID:

    PROJECT_ID=PROJECT_ID
    

    PROJECT_ID 替换为您先前创建的 Google Cloud 项目的 ID。

  2. 设置项目 ID:

    gcloud config set project $PROJECT_ID
    
  3. 克隆教程代码库:

    cd $HOME
    git clone https://github.com/GoogleCloudPlatform/transactional-microservice-examples
    

为基于编排的 Saga 架构部署服务器端组件

在本部分中,您将部署示例应用的服务器端组件。该应用实现了基于编排的 Saga 架构,例如 Cloud Run 上的微服务、Cloud Scheduler 上的事件发布时间表和 Pub/Sub 主题。

构建和部署容器映像

  1. 在 Cloud Shell 中,为名为 order-asyncOrder 服务构建容器映像,并将其部署到 Cloud Run 上:

    cd $HOME/transactional-microservice-examples/services/order-async
    gcloud builds submit --tag gcr.io/$PROJECT_ID/order-service-async
    gcloud run deploy order-service-async \
      --image gcr.io/$PROJECT_ID/order-service-async \
      --platform=managed --region=us-central1 \
      --no-allow-unauthenticated
    
  2. 为名为 customer-asyncCustomer 服务构建容器映像,并将其部署到 Cloud Run 上:

    cd $HOME/transactional-microservice-examples/services/customer-async
    gcloud builds submit --tag gcr.io/$PROJECT_ID/customer-service-async
    gcloud run deploy customer-service-async \
      --image gcr.io/$PROJECT_ID/customer-service-async \
      --platform=managed --region=us-central1 \
      --no-allow-unauthenticated
    
  3. event-publisher 服务构建容器映像,并将其部署到 Cloud Run 上:

    cd $HOME/transactional-microservice-examples/services/event-publisher
    gcloud builds submit --tag gcr.io/$PROJECT_ID/event-publisher
    gcloud run deploy event-publisher \
      --image gcr.io/$PROJECT_ID/event-publisher \
      --platform=managed --region=us-central1 \
      --no-allow-unauthenticated \
      --set-env-vars "PROJECT_ID=$PROJECT_ID"
    

为 Datastore 创建索引

在本部分中,您将为 Datastore 创建索引。event-publisher 服务将使用此索引来选择必须发布的事件。

  1. 在 Google Cloud 控制台的 Datastore 菜单上,选择 Datastore 模式

  2. 点击选择数据存储位置,然后选择 us-east1 作为存储位置。然后,点击创建数据库

  3. 为 Datastore 创建索引:

    cd $HOME/transactional-microservice-examples/services/event-publisher
    gcloud datastore indexes create index.yaml --quiet
    
  4. 在 Google Cloud 控制台的 Datastore 菜单上,选择索引,然后等待索引状态由正在编制索引变为正在响应。此过程可能需要几分钟时间。

创建服务账号来在 Cloud Run 上调用微服务

  • 在 Cloud Shell 中创建 cloud-run-invoker 服务账号:

    SERVICE_ACCOUNT_NAME="cloud-run-invoker"
    SERVICE_ACCOUNT_EMAIL=${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com
    gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \
      --display-name "Cloud Run Invoker"
    

    在本教程的后面部分,您将使用此服务账号为 Cloud Run 上运行的微服务调用 REST API。

定义时间表来调用事件发布服务

在本部分中,您将定义以一分钟为间隔的时间表来调用名为 event-publisher 的事件发布服务。

  1. 在 Cloud Shell 中,将 run.invoker 角色分配给 event publisher 服务。

    SERVICE_NAME="event-publisher"
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.invoker \
      --platform=managed --region=us-central1
    
  2. 使用 cloud-run-invoker 服务账号定义以一分钟为间隔的时间表来调用名为 event-publisherEvent publisher 服务:

    SERVICE_NAME="event-publisher"
    SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)"
      --filter="SERVICE:${SERVICE_NAME}")
    SERVICE_URL="${SERVICE_URL}/api/v1/event/publish"
    gcloud scheduler jobs create http event-publisher-scheduler \
      --schedule='* * * * *' \
      --http-method=GET \
      --uri=$SERVICE_URL \
      --oidc-service-account-email=$SERVICE_ACCOUNT_EMAIL \
      --oidc-token-audience=$SERVICE_URL \
      --location=us-central1
    

创建 Pub/Sub 主题

  • 在 Cloud Shell 中,创建以下 Pub/Sub 主题:

    gcloud pubsub topics create order-service-event
    gcloud pubsub topics create customer-service-event
    

Order 服务和 event-publisher 服务将使用 order-service-event 主题来从 Order 服务发布事件。customer-service-event 主题则用来从 Customer 服务发布事件。

定义主题来向微服务发送事件通知

在本部分中,您将定义 push-subscription 主题来将 Pub/Sub 主题中的消息传送到微服务。

  1. 在 Cloud Shell 中,将 iam.serviceAccountTokenCreator 角色分配给项目的 Pub/Sub 服务账号:

    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format "value(projectNumber)")
    gcloud projects add-iam-policy-binding $PROJECT_ID \
      --member=serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com \
      --role=roles/iam.serviceAccountTokenCreator
    

    此命令让该服务账号能够创建访问令牌,以在 Cloud Run 上调用微服务。

  2. run.invoker 角色分配给 customer-service-async 服务:

    SERVICE_NAME="customer-service-async"
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.invoker \
      --platform=managed --region=us-central1
    
  3. 创建 push-subscription 主题:

    SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    SERVICE_URL="${SERVICE_URL}/api/v1/customer/pubsub"
    gcloud pubsub subscriptions create push-order-to-customer \
      --topic order-service-event \
      --push-endpoint=$SERVICE_URL \
      --push-auth-service-account=$SERVICE_ACCOUNT_EMAIL
    

    该主题会使用 cloud-run-invoker 服务账号将 order-service-event 主题中的消息传送到 Customer 服务。

  4. run.invoker 角色分配给 order-service-async 服务:

    SERVICE_NAME="order-service-async"
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.invoker \
      --platform=managed --region=us-central1
    
  5. 创建 push-subscription 主题:

    SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    SERVICE_URL="${SERVICE_URL}/api/v1/order/pubsub"
    gcloud pubsub subscriptions create push-customer-to-order \
      --topic customer-service-event \
      --push-endpoint=$SERVICE_URL \
      --push-auth-service-account=$SERVICE_ACCOUNT_EMAIL
    

    push-subscription 主题会使用 cloud-run-invoker 服务账号将 customer-service-event 主题中的消息传送到 Order 服务。

为同步编排架构部署服务器端组件

在本部分中,您将为示例应用部署服务器端组件。这些组件会在 Cloud Run 上实现一个同步编排架构,并会实现一个使用 Workflows 执行的工作流。

构建和部署容器映像

在本部分中,您将为微服务构建容器映像,并将其部署到 Cloud Run 上。

  1. 在 Cloud Shell 中,为名为 order-syncOrder 服务构建容器映像,并将其部署到 Cloud Run 上:

    cd $HOME/transactional-microservice-examples/services/order-sync
    gcloud builds submit --tag gcr.io/$PROJECT_ID/order-service-sync
    gcloud run deploy order-service-sync \
      --image gcr.io/$PROJECT_ID/order-service-sync \
      --platform=managed --region=us-central1 \
      --no-allow-unauthenticated
    
  2. 为名为 customer-syncCustomer 服务构建容器映像,并将其部署到 Cloud Run 上:

    cd $HOME/transactional-microservice-examples/services/customer-sync
    gcloud builds submit --tag gcr.io/$PROJECT_ID/customer-service-sync
    gcloud run deploy customer-service-sync \
      --image gcr.io/$PROJECT_ID/customer-service-sync \
      --platform=managed --region=us-central1 \
      --no-allow-unauthenticated
    
  3. 为名为 order-processorOrder 处理器服务构建容器映像,并将其部署到 Cloud Run 上:

    cd $HOME/transactional-microservice-examples/services/order-processor
    gcloud builds submit --tag gcr.io/$PROJECT_ID/order-processor-service
    gcloud run deploy order-processor-service \
      --image gcr.io/$PROJECT_ID/order-processor-service \
      --platform=managed --region=us-central1 \
      --no-allow-unauthenticated \
      --set-env-vars "PROJECT_ID=$PROJECT_ID"
    

创建服务账号来在 Cloud Run 上调用微服务

在本部分中,您将重复使用在创建服务账号来在 Cloud Run 上调用微服务部分创建的 cloud-run-invoker 服务账号。

部署工作流来处理订单

  1. 在 Cloud Shell 中,将 run.invokerrun.viewer 角色分配给 order-service-sync 服务的 cloud-run-invoker 服务账号。

    SERVICE_NAME="order-service-sync"
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.invoker \
      --platform=managed --region=us-central1
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.viewer \
      --platform=managed --region=us-central1
    
  2. run.invokerrun.viewer 角色分配给 customer-service-sync 服务的 cloud-run-invoker 服务账号:

    SERVICE_NAME="customer-service-sync"
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.invoker \
      --platform=managed --region=us-central1
    gcloud run services add-iam-policy-binding $SERVICE_NAME \
      --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \
      --role=roles/run.viewer \
      --platform=managed --region=us-central1
    
  3. 使用 cloud-run-invoker 服务账号部署工作流:

    SERVICE_NAME="order-service-sync"
    ORDER_SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    
    SERVICE_NAME="customer-service-sync"
    CUSTOMER_SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    
    cd $HOME/transactional-microservice-examples/services/order-processor
    cp order_workflow.yaml.template order_workflow.yaml
    sed -i "s#ORDER-SERVICE-URL#${ORDER_SERVICE_URL}#" order_workflow.yaml
    sed -i "s#CUSTOMER-SERVICE-URL#${CUSTOMER_SERVICE_URL}#" order_workflow.yaml
    
    gcloud beta workflows deploy order_workflow \
      --source=order_workflow.yaml \
      --service-account=$SERVICE_ACCOUNT_EMAIL
    

测试基于编排的 Saga 架构上的组件

在本部分中,您将使用 curl 命令测试基于编排的 Saga 架构上部署的组件。

  • 在 Cloud Shell 中,设置环境变量来指向 customer-service-asyncorder-service-async 微服务 API 端点的网址:

    SERVICE_NAME="customer-service-async"
    CUSTOMER_SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    
    SERVICE_NAME="order-service-async"
    ORDER_SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    

创建客户条目

  • 在 Cloud Shell 中,通过向名为 customer-service-asyncCustomer 服务发送 API 请求来创建名为 customer01 的客户 ID:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d '{"customer_id":"customer01", "limit":10000}' \
      -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/limit | jq .
    

    输出类似于以下内容:

    {
      "credit": 0,
      "customer_id": "customer01",
      "limit": 10000
    }
    

提交订单

在本部分中,您将提交订单并触发订单 ID 分配流程。

  1. 在 Cloud Shell 中,通过向名为 order-service-asyncOrder 服务发送 API 请求来提交订单:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d '{"customer_id":"customer01", "number":10}' \
      -s ${ORDER_SERVICE_URL}/api/v1/order/create | jq .
    

    在本例中,您将通过指定以下内容来订购 10 个商品:"number": 10

    输出类似于以下内容:

    {
      "customer_id": "customer01",
      "number": 10,
      "order_id": "720d1305-b6fd-4f57-aaf4-fd2ca5bdfe1e",
      "status": "pending"
    }
    

    输出中的 "order_id" 标志显示分配给订单的唯一订单 ID。请复制此 ID,您在下一步中会用到它。

  2. 在一个环境变量中设置订单 ID:

    ORDER_ID=ORDER_ID
    

    ORDER_ID 替换为您在上一步中复制的 "order_id" 标志。

检查订单状态

在本部分中,您将检查订单状态的变化情况。

  1. 在 Cloud Shell 中,通过向名为 order-service-asyncOrder 服务发送 API 请求来获取订单的状态:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d "{\"customer_id\":\"customer01\", \"order_id\":\"$ORDER_ID\"}" \
      -s ${ORDER_SERVICE_URL}/api/v1/order/get | jq .
    

    输出类似以下内容:

    {
      "customer_id": "customer01",
      "number": 10,
      "order_id": "720d1305-b6fd-4f57-aaf4-fd2ca5bdfe1e",
      "status": "pending"
    }
    

    输出中的 "status" 标志显示订单的状态。如果事务流程仍在运行,则状态为 "pending"。在这种情况下,请等待几分钟,然后运行相同的命令来再次检查状态。事务流程完成后,状态将显示为 "accepted"

  2. 获取 customer01 客户 ID 的客户信息:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d '{"customer_id":"customer01"}' \
      -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/get | jq .
    

    输出类似以下内容:

    {
      "credit": 1000,
      "customer_id": "customer01",
      "limit": 10000
    }
    

    "credit" 标志显示客户当前的信用额度用量。这里应该显示增加了 1,000 点,因为该事务的业务逻辑是设定为每一件商品增加 100 点信用额度用量。

    您可以通过重复执行提交订单部分的步骤来重复订单流程。如果您通过指定 "number": 100 来订购 100 件商品,则订单的最终状态将为 "rejected",因为信用额度用量超出了限额。

测试同步编排架构上的组件

在本部分中,您将使用 curl 命令来测试同步编排架构上部署的组件。

  • 在 Cloud Shell 中,设置环境变量来指向 customer-service-syncorder-service-syncorder-processor-service 微服务 API 端点的网址:

    SERVICE_NAME="customer-service-sync"
    CUSTOMER_SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    
    SERVICE_NAME="order-service-sync"
    ORDER_SERVICE_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    
    SERVICE_NAME="order-processor-service"
    ORDER_PROCESSOR_URL=$(gcloud run services list --platform managed \
      --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
    

创建客户条目

  • 在 Cloud Shell 中,通过向名为 customer-service-syncCustomer 服务发送 API 请求来创建名为 customer02 的客户 ID:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d '{"customer_id":"customer02", "limit":10000}' \
      -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/limit | jq .
    

    输出类似以下内容:

    {
      "credit": 0,
      "customer_id": "customer02",
      "limit": 10000
    }
    

提交订单

在本部分中,您将提交订单并检查结果。

  1. 在 Cloud Shell 中,通过向名为 order-processor-serviceOrder 服务发送 API 请求来提交订单:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d '{"customer_id":"customer02", "number":10}' \
      -s ${ORDER_PROCESSOR_URL}/api/v1/order/process | jq .
    

    在本例中,您将通过指定 "number": 10 来订购 10 个商品。

    输出类似于以下内容:

    {
      "customer_id": "customer02",
      "number": 10,
      "order_id": "fb6d5087-dd99-4d5a-84c2-0e381016b9d3",
      "status": "accepted"
    }
    

    由于事务工作流以同步方式执行,因此客户端会立即显示结果。在这种情况下,最终状态如下:"accepted"

  2. 使用 customer02 的客户 ID 获取客户信息:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
      -H "Content-Type: application/json" \
      -d '{"customer_id":"customer02"}' \
      -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/get | jq .
    

    输出类似于以下内容:

    {
      "credit": 1000,
      "customer_id": "customer02",
      "limit": 10000
    }
    

    客户的信用额度用量增加了 1,000 点。您可以通过重复执行上述步骤来重复订单流程。如果您通过指定 "number": 100 值来订购 100 件商品,则客户端立即会显示结果 "rejected",因为信用额度用量超出了限额。

可选:部署 Web 应用

在本部分中,作为本教程的可选扩展内容,您将部署一个示例 Web 应用,该应用将在您在上一部分中部署的微服务之上执行事务工作流。您将使用 Firebase Hosting 部署一个 Web 应用来与 Cloud Run 上运行的后端微服务进行交互。

Firebase Hosting 提供内置的 Cloud Run 集成功能,让您可以在同一网域中托管 Web 应用和后端微服务。而在其他类型的部署中,您可能会遇到跨域资源共享 (CORS) 问题。如需了解详情,请参阅对最终用户进行身份验证

Web 应用集成。

Firebase Hosting 集成有以下用途:

  • 托管 Web 应用的静态资源。
  • 将对 API 的访问重定向到 Cloud Run 上的以下后端服务:
    • /customer-service-sync/*customer-service-sync
    • /customer-service-async/*customer-service-async
    • /order-service-sync/*order-service-sync
    • /order-service-async/*order-service-async
    • /order-processor-service/*order-processor-service
  • 返回 Web 应用的静态资源。

设置 Firebase

在本部分中,您将设置 Firebase 来在 Firebase Hosting 上托管 Web 应用。

  1. 在 Cloud Shell 中,将 Node.js 更新到最新的 LTS 版本以使用 Firebase CLI:

    nvm install lts/gallium
    
  2. 将 Firebase 资源添加到现有 Google Cloud 项目:

    firebase projects:addfirebase $PROJECT_ID
    
  3. 为 Firebase CLI 添加默认项目别名:

    jq -n --arg project_id $PROJECT_ID \
      '{"projects": {"default":$project_id}}' > \
      $HOME/transactional-microservice-examples/frontend/.firebaserc
    

部署 Web 应用

在本部分中,您将在 Firebase Hosting 上部署 Web 应用。

  1. 在 Cloud Shell 中,构建 Web 前端应用:

    cd $HOME/transactional-microservice-examples/frontend
    yarn install && yarn build
    
  2. 在 Firebase Hosting 上部署 Web 应用:

    firebase deploy
    

使用 Web 应用

  • 在浏览器中输入您在上一部分中复制的 Hosting 网址。您应该会看到该 Web 应用的首页。

使用身份令牌初始化 Web 应用

在本部分中,您将使用身份令牌初始化 Web 应用,然后前往购物页面。

  1. 在 Cloud Shell 中,输出身份令牌:

    gcloud auth print-identity-token
    
  2. 复制上一个命令的输出结果,并将其粘贴到文本框中。

  3. 点击继续。如果身份令牌有效,您将被重定向到购物页面。

将商品加入购物车

  • 在该 Web 应用中,点击加入购物车以将商品加入购物车。

检查购物车中的商品

  • 在该 Web 应用中,点击导航栏上的购物车图标,以前往结算页面。您应该会在购物车中看到一些商品。

使用异步服务提交订单

  1. 在该 Web 应用中,点击导航栏上的购物车图标,以前往结算页面。
  2. 点击提交订单(异步)。如果订单成功提交,您将被重定向到订单历史记录页面。

使用同步服务提交订单

  1. 在该 Web 应用中,点击导航栏上的购物车图标,以前往结算页面。
  2. 点击提交订单(同步)。如果订单成功提交,您将被重定向到订单历史记录页面。

查看订单历史记录

  • 在该 Web 应用中,点击导航栏上的订单,以前往订单历史记录页面。

更新未完成订单的状态

  1. 在该 Web 应用中,点击导航栏上的订单,以前往订单历史记录页面。
  2. 点击某个订单的刷新图标。只有在异步处理订单且订单状态为 pending 时,才会显示该图标。

查看客户当前的预算

  • 在该 Web 应用中,点击导航栏上的客户名称 (customer-number),以前往其个人资料页面。

前往客户个人资料。

重置 Web 应用中的状态

  • 在浏览器中,重新加载该 Web 应用。购物车中的商品、订单历史记录和客户信息等状态会被重置,并且您会被重定向到首页。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

删除各个资源

在本部分中,您将删除在本教程中使用的各个资源。

停用 Firebase Hosting

  • 在 Cloud Shell 中,运行以下命令来停止处理 Firebase Hosting 流量:

     firebase hosting:disable -f
    

删除 Cloud Run 上的服务

  • 在 Cloud Shell 中,删除 Cloud Run 上的服务:

    gcloud run services delete order-service-async \
      --platform=managed --region=us-central1 --quiet
    
    gcloud run services delete customer-service-async \
      --platform=managed --region=us-central1 --quiet
    
    gcloud run services delete order-service-sync \
      --platform=managed --region=us-central1 --quiet
    
    gcloud run services delete customer-service-sync \
      --platform=managed --region=us-central1 --quiet
    
    gcloud run services delete order-processor-service \
      --platform=managed --region=us-central1 --quiet
    
    gcloud run services delete event-publisher \
      --platform=managed --region=us-central1 --quiet
    

删除 Cloud Run 使用的容器映像

  • 在 Cloud Shell 中,删除您为本教程创建的容器映像:

    gcloud container images delete \
      gcr.io/$PROJECT_ID/order-service-async --force-delete-tags --quiet
    gcloud container images delete \
      gcr.io/$PROJECT_ID/customer-service-async --force-delete-tags --quiet
    gcloud container images delete \
      gcr.io/$PROJECT_ID/event-publisher --force-delete-tags --quiet
    gcloud container images delete \
      gcr.io/$PROJECT_ID/order-service-sync --force-delete-tags --quiet
    gcloud container images delete \
      gcr.io/$PROJECT_ID/customer-service-sync --force-delete-tags --quiet
    gcloud container images delete \
      gcr.io/$PROJECT_ID/order-processor-service --force-delete-tags --quiet
    

删除工作流

  • 在 Cloud Shell 中,删除工作流:

    gcloud beta workflows delete order_workflow --quiet
    

删除 Pub/Sub 订阅和主题

  1. 在 Cloud Shell 中,删除 Pub/Sub 订阅:

    gcloud pubsub subscriptions delete push-customer-to-order --quiet
    gcloud pubsub subscriptions delete push-order-to-customer --quiet
    
  2. 删除 Pub/Sub 主题:

    gcloud pubsub topics delete order-service-event --quiet
    gcloud pubsub topics delete customer-service-event --quiet
    

删除 Datastore 中的记录

  1. 在 Google Cloud 控制台的“Datastore”菜单上,打开实体
  2. 勾选 Customer 字段中的所有实体。
  3. 点击删除,然后点击确认以永久删除这些实体。
  4. 对以下字段重复此过程中的第 2 步和第 3 步:
    1. Event
    2. Order
    3. ProcessedEvent

删除 Cloud Scheduler 中的作业

  • 在 Cloud Shell 中,删除在 Cloud Scheduler 上运行的作业:

    gcloud scheduler jobs delete event-publisher-scheduler --quiet \
      --location=us-central1
    

删除服务账号

  • 在 Cloud Shell 中, 删除服务账号:

    gcloud iam service-accounts delete $SERVICE_ACCOUNT_EMAIL --quiet
    

删除教程资源

  • 在 Cloud Shell 中,删除用于本教程的资源:

    cd $HOME
    rm -rf transactional-microservice-examples
    

后续步骤