使用 Azure 门户创建 Durable FunctionsCreate Durable Functions using the Azure portal

Azure Functions 的 Durable Functions 扩展是在 NuGet 包 Microsoft.Azure.WebJobs.Extensions.DurableTask 中提供的。The Durable Functions extension for Azure Functions is provided in the NuGet package Microsoft.Azure.WebJobs.Extensions.DurableTask. 此扩展必须安装在函数应用中。This extension must be installed in your function app. 本文介绍如何安装此包,以便在 Azure 门户中开发 Durable Functions。This article shows how to install this package so that you can develop durable functions in the Azure portal.

备注

创建函数应用Create a function app

必须使用函数应用托管任何函数的执行。You must have a function app to host the execution of any function. 函数应用可将函数分组为逻辑单元,以便更轻松地管理、部署、缩放和共享资源。A function app lets you group your functions as a logical unit for easier management, deployment, scaling, and sharing of resources. 你可以创建 .NET 或 JavaScript 应用。You can create a .NET or JavaScript app.

  1. 在 Azure 门户菜单或“主页”页中,选择“创建资源”**** ****。From the Azure portal menu or the Home page, select Create a resource.

  2. “新建” 页面,选择 “计算” > “函数应用”In the New page, select Compute > Function App.

  3. 在“基本信息”页上,使用下表中指定的函数应用设置。****On the Basics page, use the function app settings as specified in the following table.

    设置Setting 建议的值Suggested value 说明Description
    订阅Subscription 你的订阅Your subscription 要在其下创建此新函数应用的订阅。The subscription under which this new function app is created.
    资源组Resource Group myResourceGroupmyResourceGroup 要在其中创建 Function App 的新资源组的名称。Name for the new resource group in which to create your function app.
    函数应用名称Function App name 全局唯一名称Globally unique name 用于标识新 Function App 的名称。Name that identifies your new function app. 有效字符为 a-z(不区分大小写)、0-9-Valid characters are a-z (case insensitive), 0-9, and -.
    ****Publish 代码Code 用于发布代码文件或 Docker 容器的选项。Option to publish code files or a Docker container.
    运行时堆栈Runtime stack 首选语言Preferred language 选择支持你喜欢的函数编程语言的运行时。Choose a runtime that supports your favorite function programming language. 对于 C# 和 F# 函数,选择 .NET CoreChoose .NET Core for C# and F# functions.
    版本Version 版本号Version number 选择已安装的运行时的版本。Choose the version of your installed runtime.
    区域Region 首选区域Preferred region 选择离你近或离函数访问的其他服务近的区域Choose a region near you or near other services your functions access.

    基础知识

  4. 选择“下一步: 托管”。Select Next : Hosting. 在“托管”**** 页上,输入以下设置。On the Hosting page, enter the following settings.

    设置Setting 建议的值Suggested value 说明Description
    ****存储帐户Storage account 全局唯一名称Globally unique name 创建函数应用使用的存储帐户。Create a storage account used by your function app. 存储帐户名称必须为 3 到 24 个字符,并且只能包含数字和小写字母。Storage account names must be between 3 and 24 characters in length and can contain numbers and lowercase letters only. 也可使用现有帐户,但该帐户必须符合存储帐户要求You can also use an existing account, which must meet the storage account requirements.
    操作系统Operating system 首选操作系统Preferred operating system 系统会根据你的运行时堆栈选择为你预先选择一个操作系统,但你可以根据需要更改该设置。An operating system is pre-selected for you based on your runtime stack selection, but you can change the setting if necessary.
    计划Plan 消耗(无服务器)Consumption (Serverless) 定义如何将资源分配给 Function App 的托管计划。Hosting plan that defines how resources are allocated to your function app. 在默认的消耗计划中,根据函数需求动态添加资源。In the default Consumption plan, resources are added dynamically as required by your functions. 在此无服务器托管中,只需为函数运行时间付费。In this serverless hosting, you pay only for the time your functions run. 按应用服务计划运行时,必须管理函数应用的缩放When you run in an App Service plan, you must manage the scaling of your function app.

    Hosting

  5. 选择“下一步: 监视”。Select Next : Monitoring. 在“监视”**** 页上,输入以下设置。On the Monitoring page, enter the following settings.

    设置Setting 建议的值Suggested value 说明Description
    Application InsightsApplication Insights 默认Default 当前不受支持Not supported currently

    监视

  6. 选择“查看 + 创建”****,以便查看应用配置选择。Select Review + create to review the app configuration selections.

  7. 在“查看 + 创建”页上查看设置,然后选择“创建”来预配并部署函数应用**** ****。On the Review + create page, review your settings, and then select Create to provision and deploy the function app.

  8. 选择门户右上角的“通知”图标,留意是否显示了“部署成功”消息。**** ****Select the Notifications icon in the upper-right corner of the portal and watch for the Deployment succeeded message.

  9. 选择“转到资源”****,查看新的函数应用。Select Go to resource to view your new function app. 还可选择“固定到仪表板”****。You can also select Pin to dashboard. 固定可以更轻松地从仪表板返回此函数应用资源。Pinning makes it easier to return to this function app resource from your dashboard.

    部署通知

默认情况下,创建的函数应用使用 2.x 版 Azure Functions 运行时。By default, the function app created uses version 2.x of the Azure Functions runtime. Durable Functions 扩展在 C# 中能够在 1.x 和 2.x 版 Azure Functions 运行时上工作,在 JavaScript 中能够在 2.x 版上工作。The Durable Functions extension works on both versions 1.x and 2.x of the Azure Functions runtime in C#, and version 2.x in JavaScript. 但是,无论选择哪种语言,只有在以 2.x 版运行时为目标时,才能使用模板。However, templates are only available when targeting version 2.x of the runtime regardless of the chosen language.

安装 Durable Functions npm 程序包(仅限 JavaScript)Install the durable-functions npm package (JavaScript only)

如果要创建 JavaScript Durable Functions,需要安装 durable-functions npm 程序包If you are creating JavaScript Durable Functions, you'll need to install the durable-functions npm package:

  1. 从函数应用的页面中,选择左侧窗格中“开发工具”下的“高级工具”。From your function app's page, select Advanced Tools under Development Tools in the left pane.

    Functions 平台功能选择 Kudu

  2. 在“高级工具”页中,选择“Go”。In the Advanced Tools page, select Go.

  3. 在 Kudu 控制台中,选择“调试控制台”,然后选择“CMD”。Inside the Kudu console, select Debug console, and then CMD.

    Kudu 调试控制台

  4. 此时应当会显示你的函数应用的文件目录结构。Your function app's file directory structure should display. 导航到 site/wwwroot 文件夹。Navigate to the site/wwwroot folder. 在这里,可以通过将 package.json 文件拖放到文件目录窗口中来将其上传。From there, you can upload a package.json file by dragging and dropping it into the file directory window. 下面是一个示例 package.jsonA sample package.json is below:

    {
      "dependencies": {
        "durable-functions": "^1.3.1"
      }
    }
    

    Kudu 上传 package.json

  5. 上传你的 package.json 后,从 Kudu 远程执行控制台中运行 npm install 命令。Once your package.json is uploaded, run the npm install command from the Kudu Remote Execution Console.

    Kudu 运行 npm install

创建一个业务流程协调程序函数Create an orchestrator function

  1. 在函数应用中,从左窗格中选择“函数”,然后从顶部菜单中选择“添加”。In your function app, select Functions from the left pane, and then select Add from the top menu.

  2. 在“新建函数”页的搜索字段中,输入 durable,然后选择“Durable Functions HTTP 初学者”模板。In the search field of the New Function page, enter durable, and then choose the Durable Functions HTTP starter template.

    选择“Durable Functions HTTP 初学者”

  3. 对于新函数名称,请输入 HttpStart,然后选择“创建函数”。For the New Function name, enter HttpStart, and then select Create Function.

    创建的函数用于启动业务流程。The function created is used to start the orchestration.

  4. 在函数应用中创建另一函数,这次使用“Durable Functions 业务流程协调程序”模板。Create another function in the function app, this time by using the Durable Functions orchestrator template. 将新的业务流程函数命名为 HelloSequenceName your new orchestration function HelloSequence.

  5. 使用“Durable Functions 活动”模板创建第三个函数(名为 Hello)。Create a third function named Hello by using the Durable Functions activity template.

测试持久函数业务流程Test the durable function orchestration

  1. 返回到 HttpStart 函数,选择“获取函数 URL”,然后选择“复制到剪贴板”图标以复制该 URL。Go back to the HttpStart function, choose Get function Url, and select the Copy to clipboard icon to copy the URL. 请使用此 URL 启动 HelloSequence 函数。You use this URL to start the HelloSequence function.

  2. 使用 HTTP 工具(例如 Postman 或 cURL)将 POST 请求发送到已复制的 URL。Use an HTTP tool like Postman or cURL to send a POST request to the URL that you copied. 以下示例是一个 cURL 命令,该命令将 POST 请求发送到持久函数:The following example is a cURL command that sends a POST request to the durable function:

    curl -X POST https://{your-function-app-name}.chinacloudsites.cn/api/orchestrators/HelloSequence --header "Content-Length: 0"
    

    在此示例中,{your-function-app-name} 是域,该域是函数应用的名称。In this example, {your-function-app-name} is the domain that is the name of your function app. 响应消息包含一组 URI 终结点,这些终结点可以用来监视并管理执行,该执行如以下示例所示:The response message contains a set of URI endpoints that you can use to monitor and manage the execution, which looks like the following example:

    {  
       "id":"10585834a930427195479de25e0b952d",
       "statusQueryGetUri":"https://...",
       "sendEventPostUri":"https://...",
       "terminatePostUri":"https://...",
       "rewindPostUri":"https://..."
    }
    
  3. 调用 statusQueryGetUri 终结点 URI,此时会看到持久函数的当前状态,如以下示例所示:Call the statusQueryGetUri endpoint URI and you see the current status of the durable function, which might look like this example:

        {
            "runtimeStatus": "Running",
            "input": null,
            "output": null,
            "createdTime": "2017-12-01T05:37:33Z",
            "lastUpdatedTime": "2017-12-01T05:37:36Z"
        }
    
  4. 继续调用 statusQueryGetUri 终结点,直到状态更改为“Completed”,此时会看到一个响应,如以下示例所示:Continue calling the statusQueryGetUri endpoint until the status changes to Completed, and you see a response like the following example:

    {
            "runtimeStatus": "Completed",
            "input": null,
            "output": [
                "Hello Tokyo!",
                "Hello Seattle!",
                "Hello London!"
            ],
            "createdTime": "2017-12-01T05:38:22Z",
            "lastUpdatedTime": "2017-12-01T05:38:28Z"
        }
    

现已在 Azure 中启动并运行第一个持久函数。Your first durable function is now up and running in Azure.

后续步骤Next steps