本教程介绍了如何创建和运行一个父级工作流,以并行执行多个子级工作流。
在下图中,系统会调用子工作流的四个并行执行。这样,父级工作流就可以在并行分支中处理数据,并缩短总体执行时间。父级工作流会等待所有子级工作流执行完成,然后返回成功和失败执行的摘要,从而简化任何错误检测。
目标
在此教程中,您将学习以下操作:
- 创建和部署从父级工作流接收数据的子工作流。
- 创建并部署一个父级工作流,该工作流使用并行
for
循环来执行多个子工作流。 - 运行父级工作流,该工作流会调用子级工作流的并行执行。
- 所有成功和失败的子工作流执行结果都会存储在映射中并返回。
您可以在 Google Cloud 控制台中运行以下命令,也可以在终端或 Cloud Shell 中使用 Google Cloud CLI 运行这些命令。
费用
在本文档中,您将使用 Google Cloud 的以下收费组件:
准备工作
您的组织定义的安全限制条件可能会导致您无法完成以下步骤。如需了解相关问题排查信息,请参阅在受限的 Google Cloud 环境中开发应用。
控制台
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Workflows > Workflows Invoker role to the service account.
To grant the role, find the Select a role list, then select Workflows > Workflows Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Workflows > Workflows Invoker role to the service account.
To grant the role, find the Select a role list, then select Workflows > Workflows Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
gcloud
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs:
gcloud services enable workflowexecutions.googleapis.com
workflows.googleapis.com -
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/workflows.invoker
IAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/workflows.invoker
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs:
gcloud services enable workflowexecutions.googleapis.com
workflows.googleapis.com -
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/workflows.invoker
IAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/workflows.invoker
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
创建和部署子工作流
子工作流程可以接收和处理父级工作流程中的数据。子工作流通过执行以下操作来演示这一点:
- 接收整数作为参数
- 休眠 10 秒以模拟一些处理
返回一个指示器(基于整数是奇数还是偶数),以模拟工作流执行的成功或失败
控制台
在 Google Cloud 控制台中,前往工作流页面。
点击
创建。为新工作流输入名称
workflow-child
。在区域列表中,选择 us-central1。
选择您之前创建的服务账号。
点击下一步。
在工作流编辑器中,输入工作流的定义:
点击部署。
gcloud
为您的工作流创建源代码文件:
touch workflow-child.yaml
在文本编辑器中打开源代码文件,然后将以下工作流复制到该文件中。
部署工作流:
gcloud workflows deploy workflow-child \ --source=workflow-child.yaml \ --location=us-central1 \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
将
SERVICE_ACCOUNT_NAME
替换为您之前创建的服务账号的名称。
创建和部署父级工作流
父级工作流使用并行 for
循环来执行子工作流的多个分支。
复制工作流定义的源代码。它由以下部分组成:
部署工作流:
控制台
在 Google Cloud 控制台中,前往 Workflows 页面:
点击
创建。为新工作流输入名称
workflow-parent
。在区域列表中,选择 us-central1。
选择您之前创建的服务账号。
点击下一步。
在工作流编辑器中,粘贴父级工作流的定义。
点击部署。
gcloud
为您的工作流创建源代码文件:
touch workflow-parent.yaml
在文本编辑器中打开源代码文件,然后粘贴父级工作流的定义。
部署工作流:
gcloud workflows deploy workflow-parent \ --source=workflow-parent.yaml \ --location=us-central1 \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
将
SERVICE_ACCOUNT_NAME
替换为您之前创建的服务账号的名称。
执行父级工作流
执行父级工作流,以便子级工作流的调用并行运行。执行过程大约需要 10 秒钟才能完成。
控制台
在 Google Cloud 控制台中,前往 Workflows 页面:
在 Workflows 页面上,点击 workflow-parent 工作流以转到其详情页面。
在工作流详情页面上,选择 play_arrow 执行。
再次点击执行。
在输出窗格中查看工作流的结果。
结果应类似于以下内容,表明迭代 2 和 4 出现错误,迭代 1 和 3 成功。
"failure": { "2": { "message": "Execution failed or cancelled.", "operation": { "argument": "{\"iteration\":2}", "duration": "10.157992541s", "endTime": "2023-07-11T13:13:13.028424329Z", "error": { "context": "RuntimeError: \"Error with iteration 2\"\nin step \"raise_error\", routine \"main\", line: 18", "payload": "\"Error with iteration 2\"", ... "4": { "message": "Execution failed or cancelled.", "operation": { "argument": "{\"iteration\":4}", "duration": "10.157929734s", "endTime": "2023-07-11T13:13:13.061289142Z", "error": { "context": "RuntimeError: \"Error with iteration 4\"\nin step \"raise_error\", routine \"main\", line: 18", "payload": "\"Error with iteration 4\"", ... "success": { "1": "Hello world1", "3": "Hello world3"
gcloud
执行工作流:
gcloud workflows run workflow-parent \ --location=us-central1
结果应类似于以下内容,表明迭代 2 和 4 出现错误,迭代 1 和 3 成功。
Waiting for execution [06c753e4-6947-4c62-ac0b-2a9d53fb1b8f] to complete...done. argument: 'null' duration: 14.065415004s endTime: '2023-07-11T12:50:43.929023883Z' name: projects/386837416586/locations/us-central1/workflows/workflow-parent/executions/06c753e4-6947-4c62-ac0b-2a9d53fb1b8f result: '{"failure":{"2":{"message":"Execution failed or cancelled.","operation":{"argument":"{\"iteration\":2}","duration":"10.143718070s","endTime":"2023-07-11T12:50:40.673209821Z","error":{"context":"RuntimeError: ... "Error with iteration 2\"\nin step \"raise_error\", routine \"main\", line: 18","payload":"\"Error ... "Error with iteration 4\"\nin step \"raise_error\", routine \"main\", line: 18","payload":"\"Error ... "success":{"1":"Hello world1","3":"Hello world3"}}' startTime: '2023-07-11T12:50:29.863608879Z' state: SUCCEEDED
您已成功创建并部署了一个工作流,该工作流会调用子工作流,在并行分支中执行四次子工作流迭代,并针对每次子工作流执行返回成功或失败的指示器。
清理
如果您为本教程创建了一个新项目,请删除项目。 如果您使用的是现有项目,希望保留此项目且不保留本教程中添加的任何更改,请删除为教程创建的资源。
删除项目
为了避免产生费用,最简单的方法是删除您为本教程创建的项目。
要删除项目,请执行以下操作:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
删除教程资源
删除在本教程中创建的工作流:
gcloud workflows delete workflow-child gcloud workflows delete workflow-parent
后续步骤
- 如需详细了解 Workflows 语法,请参阅 Workflows 语法参考文档。
- 如需详细了解 Workflows 连接器,请参阅了解连接器。