您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure 逻辑应用创建自动化的基于计划的重复工作流Tutorial: Create automated, schedule-based, recurring workflows by using Azure Logic Apps

本教程介绍如何生成逻辑应用,并自动化一个按计划运行的重复工作流。This tutorial shows how to build a logic app and automate a recurring workflow that runs on a schedule. 具体而言,此示例逻辑应用将在每个工作日的早晨运行,检查两个地点之间的行程时间,包括交通情况。Specifically, this example logic app runs every weekday morning and checks the travel time, including traffic, between two places. 如果该时间超过特定的限制,逻辑应用会发送一封电子邮件,其中包括到达目的地所需的行程时间和额外时间。If the time exceeds a specific limit, the logic app sends email with the travel time and the extra time necessary for your destination.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 创建空白逻辑应用。Create a blank logic app.
  • 添加一个重复触发器用于指定逻辑应用的计划。Add a Recurrence trigger that specifies the schedule for your logic app.
  • 添加一个必应地图操作用于获取路线的行程时间。Add a Bing Maps action that gets the travel time for a route.
  • 添加一个操作用于创建变量、将行程时间从秒转换为分钟,并将结果存储在变量中。Add an action that creates a variable, converts the travel time from seconds to minutes, and stores that result in the variable.
  • 添加一个条件,将行程时间与指定的限制进行比较。Add a condition that compares the travel time against a specified limit.
  • 添加一个操作,以便在行程时间超过限制的情况下发送电子邮件。Add an action that sends you email if the travel time exceeds the limit.

完成后,逻辑应用看起来大致与以下工作流类似:When you're done, your logic app looks like this workflow at a high level:

高级逻辑应用工作流概述

必备条件Prerequisites

  • Azure 订阅。An Azure subscription. 如果你没有 Azure 订阅,请在开始之前注册一个免费 Azure 帐户If you don't have a subscription, sign up for a free Azure account before you begin.

  • 逻辑应用支持的电子邮件提供商(例如 Office 365 Outlook、Outlook.com 或 Gmail)提供的电子邮件帐户。An email account from an email provider that's supported by Logic Apps, such as Office 365 Outlook, Outlook.com, or Gmail. 至于其他提供商,请查看此处的连接器列表For other providers, review the connectors list here. 本快速入门使用 Office 365 Outlook 帐户。This quickstart uses an Office 365 Outlook account. 如果你使用其他电子邮件帐户,则常规步骤保持不变,但 UI 可能稍有不同。If you use a different email account, the general steps stay the same, but your UI might slightly differ.

  • 若要获取路线的行程时间,需要必应地图 API 的访问密钥。To get the travel time for a route, you need an access key for the Bing Maps API. 若要获取此密钥,请执行如何获取必应地图密钥中的步骤。To get this key, follow the steps for how to get a Bing Maps key.

登录到 Azure 门户Sign in to the Azure portal

使用 Azure 帐户凭据登录到 Azure 门户Sign in to the Azure portal with your Azure account credentials.

创建逻辑应用Create your logic app

  1. 在 Azure 主菜单中,依次选择“创建资源” > “集成” > “逻辑应用”。 From the main Azure menu, select Create a resource > Integration > Logic App.

    创建逻辑应用资源

  2. 在“创建逻辑应用”下,提供有关逻辑应用的信息,如下所示。 Under Create logic app, provide this information about your logic app as shown and described. 完成操作后,选择“创建” 。When you're done, select Create.

    提供有关逻辑应用的信息

    propertiesProperty Value 说明Description
    名称Name LA-TravelTimeLA-TravelTime 逻辑应用的名称,只能包含字母、数字、连字符 (-)、下划线 (_)、括号(())和句点 (.)。Your logic app's name, which can contain only letters, numbers, hyphens (-), underscores (_), parentheses ((, )), and periods (.). 此示例使用“LA-TravelTime”。This example uses "LA-TravelTime".
    订阅Subscription <your-Azure-subscription-name><your-Azure-subscription-name> Azure 订阅名称Your Azure subscription name
    资源组Resource group LA-TravelTime-RGLA-TravelTime-RG 用于组织相关资源的 Azure 资源组的名称。The name for the Azure resource group, which is used to organize related resources. 此示例使用“LA-TravelTime-RG”。This example uses "LA-TravelTime-RG".
    位置Location 美国西部West US 用于存储逻辑应用信息的区域。TThe region where to store your logic app information. 此示例使用“美国西部”。This example uses "West US".
    Log AnalyticsLog Analytics 关闭Off 对于诊断日志记录,请保留“关闭”设置。 Keep the Off setting for diagnostic logging.
  3. 在 Azure 部署你的应用后,在 Azure 工具栏上,选择“通知” > “转到资源” ,查看你部署的逻辑应用。After Azure deploys your app, on the Azure toolbar, select Notifications > Go to resource for your deployed logic app.

    转到新的逻辑应用资源

    或者,可以通过在搜索框中键入名称来查找和选择逻辑应用。Or, you can find and select your logic app by typing the name in the search box.

    逻辑应用设计器打开并显示一个包含简介视频以及常用触发器和逻辑应用模式的页面。The Logic Apps Designer opens and shows a page with an introduction video and commonly used triggers and logic app patterns. 在“模板”下选择“空白逻辑应用”。Under Templates, select Blank Logic App.

    选择空白逻辑应用模板

接下来,请添加根据指定计划激发的重复触发器Next, add the Recurrence trigger, which fires based on a specified schedule. 每个逻辑应用都必须从触发器开始,在发生特定事件或新数据符合特定条件的情况下触发。Every logic app must start with a trigger, which fires when a specific event happens or when new data meets a specific condition. 有关详细信息,请参阅创建第一个逻辑应用For more information, see Create your first logic app.

添加重复触发器Add the Recurrence trigger

  1. 在逻辑应用设计器上的搜索框中,输入“重复”作为筛选器。On the Logic App Designer, in the search box, enter "recurrence" as your filter. 在“触发器”列表中选择“重复”触发器。 From the Triggers list, select the Recurrence trigger.

    添加“重复”触发器

  2. 在“重复”形状中选择省略号 ( ... ) 按钮,然后选择“重命名”。 On the Recurrence shape, select the ellipses (...) button, and then select Rename. 重命名触发器并提供以下说明:Check travel time every weekday morningRename the trigger with this description: Check travel time every weekday morning

    重命名重复触发器说明

  3. 在触发器中更改这些属性。Inside the trigger, change these properties.

    更改重复触发器的间隔和频率

    propertiesProperty 必选Required Value 说明Description
    间隔 Interval Yes 11 在两次检查之间需等待的时间间隔数The number of intervals to wait between checks
    频率Frequency Yes WeekWeek 用于定期触发的时间单位The unit of time to use for the recurrence
  4. 在“间隔”和“频率”下,打开“添加新参数”列表,然后选择要添加到触发器的这些属性。 Under Interval and Frequency, open the Add new parameter list, and select these properties to add to the trigger.

    • 在这些日期On these days
    • 在这些小时At these hours
    • 在这些分钟At these minutes

    添加重复触发器的属性

  5. 现在,按此处所示和所述设置其他属性的值。Now set the values for the additional properties as shown and described here.

    提供计划和定期触发详细信息

    propertiesProperty Value 说明Description
    在这些日期On these days 星期一、星期二、星期三、星期四、星期五Monday,Tuesday,Wednesday,Thursday,Friday 仅当“频率”设置为“周”时可用 Available only when Frequency is set to "Week"
    在这些小时At these hours 7,8,97,8,9 仅当“频率”设置为“周”或“天”时可用 。Available only when Frequency is set to "Week" or "Day". 选择一天中的某些小时,用于运行此定期触发。Select the hours of the day to run this recurrence. 此示例在 7、8、9 点运行。This example runs at the 7, 8, and 9-hour marks.
    在这些分钟At these minutes 0,15,30,450,15,30,45 仅当“频率”设置为“周”或“天”时可用 。Available only when Frequency is set to "Week" or "Day". 选择一天中的某些分钟,用于运行此定期触发。Select the minutes of the day to run this recurrence. 此示例从零点开始,每隔 15 分钟运行一次。This example runs every 15 minutes starting at the zero-hour mark.

    此触发器在每个工作日每隔 15 分钟触发一次,开始时间为早晨 7:00,结束时间为早晨 9:45。This trigger fires every weekday, every 15 minutes, starting at 7:00 AM and ending at 9:45 AM. “预览”框显示定期触发计划。 The Preview box shows the recurrence schedule. 有关详细信息,请参阅计划任务和工作流以及工作流操作和触发器For more information, see Schedule tasks and workflows and Workflow actions and triggers.

  6. 若要立即隐藏触发器的详细信息,请单击形状的标题栏。To hide the trigger's details for now, click inside the shape's title bar.

    折叠形状即可隐藏详细信息

  7. 保存逻辑应用。Save your logic app. 在设计器工具栏上,选择“保存” 。On the designer toolbar, select Save.

逻辑应用现已生成,但除了定期触发,不能执行任何操作。Your logic app is now live but doesn't do anything other recur. 因此,请添加一项在触发器触发时进行响应的操作。So, add an action that responds when the trigger fires.

获取路线的行程时间Get the travel time for a route

有了触发器以后,即可添加操作,用于获取两个地点之间的行程时间。Now that you have a trigger, add an action that gets the travel time between two places. 逻辑应用提供适用于必应地图 API 的连接器,可以方便地获取该信息。Logic Apps provides a connector for the Bing Maps API so that you can easily get this information. 在开始此任务之前,请确保有必应地图 API 密钥,如本教程的先决条件部分所述。Before you start this task, make sure that you have a Bing Maps API key as described in this tutorial's prerequisites.

  1. 在逻辑应用设计器中的触发器下,选择“新建步骤”。 In the Logic App Designer, under your trigger, select New step.

  2. 在“选择操作”下选择“标准”。 Under Choose an action, select Standard. 在搜索框中输入“必应地图”作为筛选器,然后选择“获取路线”操作。 In the search box, enter "bing maps" as your filter, and select the Get route action.

    选择“获取路线”操作

  3. 如果没有必应地图连接,系统会提示创建一个连接。If you don't have a Bing Maps connection, you're asked to create a connection. 提供以下连接详细信息,然后选择“创建” 。Provide these connection details, and select Create.

    创建与必应地图 API 的连接

    propertiesProperty 必选Required Value 说明Description
    连接名称Connection Name Yes BingMapsConnectionBingMapsConnection 提供连接的名称。Provide a name for your connection. 此示例使用“BingMapsConnection”。This example uses "BingMapsConnection".
    API 密钥API Key Yes <your-Bing-Maps-key><your-Bing-Maps-key> 输入以前接收的必应地图密钥。Enter the Bing Maps key that you previously received. 如果没有必应地图密钥,请了解如何获取密钥If you don't have a Bing Maps key, learn how to get a key.
  4. 重命名操作并提供以下说明:Get route and travel time with trafficRename the action with this description: Get route and travel time with traffic

  5. 在该操作中打开“添加新参数”列表,然后选择要添加到该操作的这些属性。 Inside the action, open the Add new parameter list, and select these properties to add to the action.

    • 优化Optimize
    • 距离单位Distance unit
    • 旅行模式Travel mode

    将属性添加到“获取路线”操作

  6. 现在,按此处所示和所述设置操作属性的值。Now set the values for the action's properties as shown and described here.

    提供“获取路线”操作的详细信息

    propertiesProperty 必选Required Value 说明Description
    路标 1Waypoint 1 Yes <start-location><start-location> 路线起点Your route's origin
    路标 2Waypoint 2 Yes <end-location><end-location> 路由终点Your route's destination
    优化Optimize No timeWithTraffictimeWithTraffic 一个参数,用于优化路线,例如距离、当前交通状况下的行程时间,等等。A parameter to optimize your route, such as distance, travel time with current traffic, and so on. 选择“timeWithTraffic”参数。Select the "timeWithTraffic" parameter.
    距离单位Distance unit No <your-preference><your-preference> 路线的距离单位。The unit of distance for your route. 此示例使用“英里”作为单位。This example uses "Mile" as the unit.
    旅行模式Travel mode No 驾车Driving 路线的旅行模式。The travel mode for your route. 选择“驾车”模式。Select "Driving" mode.

    有关这些参数的详细信息,请参阅 Calculate a route(计算路线)。For more information about these parameters, see Calculate a route.

  7. 保存逻辑应用。Save your logic app.

接下来创建一个变量,以便将当前的行程时间转换为分钟而非秒,然后将其存储。Next, create a variable so that you can convert and store the current travel time as minutes, rather than seconds. 这样即可避免重复转换,可以在后面的步骤中更方便地使用该值。That way, you can avoid repeating the conversion and use the value more easily in later steps.

创建用于存储行程时间的变量Create a variable to store travel time

有时候,可能需要对工作流中的数据运行操作,再在以后的操作中使用相关结果。Sometimes, you might want to run operations on data in your workflow, and then use the results in later actions. 若要保存这些结果,方便以后重复使用或引用,可以创建变量来存储这些经处理的结果。To save these results so that you can easily reuse or reference them, you can create variables to store those results after processing them. 只能在逻辑应用的顶层创建变量。You can create variables only at the top level in your logic app.

默认情况下,以前的“获取路线”操作通过“旅行期间交通”属性返回特定交通状况下的当前行程时间(以秒为单位)。 By default, the previous Get route action returns the current travel time with traffic in seconds from the Travel Duration Traffic property. 将该值改为转换为分钟并进行存储以后,该值在以后就会更易于重复使用,不需再次转换。By converting and storing this value as minutes instead, you make the value easier to reuse later without converting again.

  1. 在“获取路线”操作下,选择“新建步骤”。 Under the Get route action, select New step.

  2. 在“选择操作”下,选择“内置”。 Under Choose an action, select Built-in. 在搜索框中输入“变量”,然后选择“初始化变量”操作。 In the search box, enter "variables", and select the Initialize variable action.

    选择“初始化变量”操作

  3. 重命名此操作并提供以下说明:Create variable to store travel timeRename this action with this description: Create variable to store travel time

  4. 提供变量的详细信息,如下所述:Provide the details for your variable as described here:

    propertiesProperty 必选Required Value 说明Description
    名称Name Yes travelTimetravelTime 变量的名称。The name for your variable. 此示例使用“travelTime”。This example uses "travelTime".
    类型 Type Yes IntegerInteger 变量的数据类型The data type for your variable
    Value No 一个表达式,可将当前的行程时间从秒转换为分钟(参见此表下面的步骤)。An expression that converts the current travel time from seconds to minutes (see steps under this table). 变量的初始值The initial value for your variable
    1. 若要创建“值”属性的表达式,请单击该框,使动态内容列表显示。 To create the expression for the Value property, click inside the box so that the dynamic content list appears. 必要时可扩大浏览器,直至列表显示。If necessary, widen your browser until the list appears. 在动态内容列表中,选择“表达式”。 In the dynamic content list, select Expression.

      提供“初始化变量”操作的信息

      单击某些编辑框时,动态内容列表会显示。When you click inside some edit boxes, the dynamic content list appears. 此列表显示以前的操作中任何可以在工作流中用作输入的属性。This list shows any properties from previous actions that you can use as inputs in your workflow. 动态内容列表有一个表达式编辑器,可以在其中选择用于运行操作的函数。The dynamic content list has an expression editor where you can select functions to run operations. 此表达式编辑器仅显示在动态内容列表中。This expression editor appears only in the dynamic content list.

    2. 在表达式编辑器中,输入此表达式:div(,60)In the expression editor, enter this expression: div(,60)

      输入此表达式:“div(,60)”

    3. 将光标置于表达式中左括号 ( ( ) 和逗号 ( , ) 之间。Put your cursor inside the expression between the left parenthesis (() and the comma (,). 选择“动态内容”。 select Dynamic content.

      位置光标,选择“动态内容”

    4. 在动态内容列表中,选择“旅行期间交通” 。In the dynamic content list, select Travel Duration Traffic.

      选择“旅行期间交通”属性

    5. 在表达式中解析属性值后,选择“确定”。 After the property value resolves inside the expression, select OK.

      若要完成构建表达式,请选择“确定”

      “值”属性现在如下所示: The Value property now appears as shown here:

      “值”属性与已解析表达式一起出现

  5. 保存逻辑应用。Save your logic app.

接下来添加一个条件,检查当前的行程时间是否长于特定的限制。Next, add a condition that checks whether the current travel time is greater than a specific limit.

将行程时间与限制进行比较Compare the travel time with limit

  1. 在上一个操作下,选择“新建步骤”。 Under the previous action, select New step.

  2. 在“选择操作”下,选择“内置”。 Under Choose an action, select Built-in. 在搜索框中,输入“条件”作为筛选器。In the search box, enter "condition" as your filter. 从操作列表中选择“条件”操作。 From the actions list, select the Condition action.

    选择“条件”操作

  3. 重命名条件并提供以下说明:If travel time exceeds limitRename the condition with this description: If travel time exceeds limit

  4. 生成一个条件,用于检查 travelTime 属性值是否超过指定的限制,如下所示:Build a condition that checks whether the travelTime property value exceeds your specified limit as described and shown here:

    1. 在条件中,单击条件左侧的“选择值”框。 In the condition, click inside the Choose a value box on the condition's left side.

    2. 从显示的动态内容列表中的“变量” 下,选择 travelTime 属性。From the dynamic content list that appears, under Variables, select the travelTime property.

      构建条件的左侧

    3. 在中间的比较框中,选择“大于” 运算符。In the middle comparison box, select the is greater than operator.

    4. 在条件右侧的“选择值”框中输入以下限制:15 In the Choose a value box on the condition's right side, enter this limit: 15

      完成后,条件如以下示例所示:When you're done, the condition looks like this example:

      检查行程时间的完成条件

  5. 保存逻辑应用。Save your logic app.

接下来,添加在行程时间超出限制时需运行的操作。Next, add the action to run when the travel time exceeds your limit.

在超出限制时发送电子邮件Send email when limit exceeded

现在,请添加一项操作,该操作在行程时间超出限制时向你发送电子邮件。Now, add an action that emails you when the travel time exceeds your limit. 该电子邮件包括当前的行程时间以及行完指定的路线所需的额外时间。This email includes the current travel time and the extra time necessary to travel the specified route.

  1. 在条件的 If true 分支中,选择“添加操作”。 In the condition's If true branch, select Add an action.

  2. 在“选择操作”下选择“标准”。 Under Choose an action, select Standard. 在搜索框中,输入“发送电子邮件”。In the search box, enter "send email". 此列表将返回许多结果,因此请先选择所需的电子邮件连接器,例如:The list returns many results, so first select the email connector that you want, for example:

    选择所需的电子邮件连接器

    • 对于 Azure 工作或学校帐户,请选择“Office 365 Outlook”。 For Azure work or school accounts, select Office 365 Outlook.
    • 对于个人 Microsoft 帐户,请选择“Outlook.com”。 For personal Microsoft accounts, select Outlook.com.
  3. 显示连接器的操作后,选择要使用的“发送电子邮件”操作,例如:When the connector's actions appear, select "send email action" that you want to use, for example:

    选择“发送电子邮件”操作

  4. 如果还没有连接,系统会要求你登录到电子邮件帐户。If you don't already have a connection, you're asked to sign in to your email account.

    逻辑应用创建到电子邮件帐户的连接。Logic Apps creates a connection to your email account.

  5. 重命名操作并提供以下说明:Send email with travel timeRename the action with this description: Send email with travel time

  6. 在“收件人” 框中,输入收件人的电子邮件地址。In the To box, enter the recipient's email address. 出于测试目的,请使用你的电子邮件地址。For testing purposes, use your email address.

  7. 在“主题”框中指定电子邮件的主题,并包括 travelTime 变量。 In the Subject box, specify the email's subject, and include the travelTime variable.

    1. 输入带尾随空格的文本 Current travel time (minutes):Enter the text Current travel time (minutes): with a trailing space.

    2. 在动态内容列表中的“变量”下,选择“查看更多”。 In the dynamic content list, under Variables, select See more.

      查找“travelTime”变量

    3. “travelTime”显示在“变量”下面后,选择“travelTime”。 After travelTime appears under Variables, select travelTime.

      输入主题文本以及可返回行程时间的表达式

  8. 在“正文”框中,指定电子邮件正文的内容。 In the Body box, specify the content for the email body.

    1. 输入带尾随空格的文本 Add extra travel time (minutes):Enter the text Add extra travel time (minutes): with a trailing space.

    2. 在动态内容列表中,选择“表达式”。 In the dynamic content list, select Expression.

      生成电子邮件正文的表达式

    3. 在表达式编辑器中输入以下表达式,以便计算超出限制的分钟数:sub(,15)In the expression editor, enter this expression so that you can calculate the number of minutes that exceed your limit: sub(,15)

      输入用于计算额外行程时间(单位:分钟)的表达式

    4. 将光标置于表达式中左括号 ( ( ) 和逗号 ( , ) 之间。Put your cursor inside the expression between the left parenthesis (() and the comma (,). 选择“动态内容”。 Select Dynamic content.

      继续生成用于计算额外行程时间(单位:分钟)的表达式

    5. 在“变量”下选择“travelTime”。 Under Variables, select travelTime.

      选择要在表达式中使用的“travelTime”属性

    6. 在表达式中解析属性后,选择“确定”。 After the property resolves inside the expression, select OK.

      在“正文”属性解析后,选择“确定”

      “正文”属性现在如下所示: The Body property now appears as shown here:

      解析了表达式中的“正文”属性

  9. 保存逻辑应用。Save your logic app.

接下来测试逻辑应用,该应用现在看起来类似于以下示例:Next, test your logic app, which now looks similar to this example:

完成的示例逻辑应用工作流

运行逻辑应用Run your logic app

若要手动启动逻辑应用,请在设计器工具栏中选择“运行” 。To manually start your logic app, on the designer toolbar bar, select Run.

  • 如果当前的行程时间始终没有超出限制,则逻辑应用不会执行任何其他操作,只是等待下一个时间间隔,然后重新进行检查。If the current travel time stays under your limit, your logic app does nothing else and waits or the next interval before checking again.

  • 如果当前的行程时间超出限制,则会收到一封电子邮件,其中包含当前的行程时间以及超出限制的分钟数。If the current travel time exceeds your limit, you get an email with the current travel time and the number of minutes above your limit. 下面是逻辑应用发送的一封示例电子邮件:Here is an example email that your logic app sends:

显示行程时间的已发送电子邮件示例

如果没有收到任何电子邮件,请检查电子邮件的垃圾邮件文件夹。If you don't get any emails, check your email's junk folder. 垃圾电子邮件筛选器可能会将这些类型的邮件重定向。Your email junk filter might redirect these kinds of mails. 否则,如果不确定逻辑应用是否正常运行,请参阅逻辑应用故障排除Otherwise, if you're unsure that your logic app ran correctly, see Troubleshoot your logic app.

祝贺!你现已创建并运行基于计划的重复逻辑应用。Congratulations, you've now created and run a schedule-based recurring logic app.

若要创建其他使用“重复”触发器的逻辑应用,请检查以下模板,这些模板在创建逻辑应用之后即可使用: To create other logic apps that use the Recurrence trigger, check out these templates, which available after you create a logic app:

  • 获取发送给你的每日提醒。Get daily reminders sent to you.
  • 删除较旧的 Azure Blob。Delete older Azure blobs.
  • 将消息添加到 Azure 存储队列中。Add a message to an Azure Storage queue.

清理资源Clean up resources

不再需要示例逻辑应用时,请删除包含该逻辑应用和相关资源的资源组。When you no longer need the sample logic app, delete the resource group that contains your logic app and related resources.

  1. 在 Azure 主菜单中转到“资源组”,然后选择逻辑应用的资源组。 On the main Azure menu, go to Resource groups, and select the resource group for your logic app.

  2. 在资源组菜单中,选择“概述” > “删除资源组”。 On the resource group menu, select Overview > Delete resource group.

    “概览”>“删除资源组”

  3. 输入资源组名称作为确认,然后选择“删除”。 Enter the resource group name as confirmation, and select Delete.

后续步骤Next steps

本教程介绍了如何创建一个逻辑应用,以便根据指定的计划(在工作日早晨)检查交通状况,并在行程时间超出指定限制时采取行动(发送电子邮件)。In this tutorial, you created a logic app that checks traffic based on a specified schedule (on weekday mornings), and takes action (sends email) when the travel time exceeds a specified limit. 现在,需了解如何通过集成 Azure 服务、Microsoft 服务和其他 SaaS 应用来生成一个逻辑应用,以便发送供审核的邮件列表请求。Now, learn how to build a logic app that sends mailing list requests for approval by integrating Azure services, Microsoft services, and other SaaS apps.