Durable Functions 中的任务中心 (Azure Functions)Task hubs in Durable Functions (Azure Functions)

Durable Functions 中的任务中心是用于业务流程的 Azure 存储资源的逻辑容器。A task hub in Durable Functions is a logical container for Azure Storage resources that are used for orchestrations. 只有当业务流程协调程序函数与活动函数属于同一任务中心时,它们才能彼此进行交互。Orchestrator and activity functions can only interact with each other when they belong to the same task hub.

如果多个函数应用共享存储帐户,则必须使用单独的任务中心名称配置每个函数应用。If multiple function apps share a storage account, each function app must be configured with a separate task hub name. 一个存储帐户可以包含多个任务中心。A storage account can contain multiple task hubs. 下图说明了在共享和专用存储帐户中每个函数应用有一个任务中心。The following diagram illustrates one task hub per function app in shared and dedicated storage accounts.

说明共享和专用存储帐户的关系图。

Azure 存储资源Azure Storage resources

任务中心包含以下存储资源:A task hub consists of the following storage resources:

  • 一个或多个控制队列。One or more control queues.
  • 一个工作项队列。One work-item queue.
  • 一个历史记录表。One history table.
  • 一个实例表。One instances table.
  • 一个包含一个或多个租用 blob 的存储容器。One storage container containing one or more lease blobs.

当业务流程协调程序或活动函数运行时或调度它们运行时,将自动在默认 Azure 存储帐户中创建所有这些资源。All of these resources are created automatically in the default Azure Storage account when orchestrator or activity functions run or are scheduled to run. 性能和缩放一文介绍了如何使用这些资源。The Performance and Scale article explains how these resources are used.

任务中心名称Task hub names

任务中心由 host.json 文件中声明的名称标识,如以下示例所示:Task hubs are identified by a name that is declared in the host.json file, as shown in the following example:

host.json (Functions 1.x)host.json (Functions 1.x)

{
  "durableTask": {
    "hubName": "MyTaskHub"
  }
}

host.json (Functions 2.x)host.json (Functions 2.x)

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "hubName": "MyTaskHub"
    }
  }
}

还可以使用应用设置配置任务中心,如以下 host.json 示例文件所示:Task hubs can also be configured using app settings, as shown in the following host.json example file:

host.json (Functions 1.x)host.json (Functions 1.x)

{
  "durableTask": {
    "hubName": "%MyTaskHub%"
  }
}

host.json (Functions 2.x)host.json (Functions 2.x)

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "hubName": "%MyTaskHub%"
    }
  }
}

任务中心名称将设置为 MyTaskHub 应用设置的值。The task hub name will be set to the value of the MyTaskHub app setting. 以下 local.settings.json 演示了如何将 MyTaskHub 设置定义为 samplehubnameThe following local.settings.json demonstrates how to define the MyTaskHub setting as samplehubname:

{
  "IsEncrypted": false,
  "Values": {
    "MyTaskHub" : "samplehubname"
  }
}

这是一个预编译的 C# 示例,说明如何编写这样一个函数,该函数使用 OrchestrationClientBinding 来处理配置为应用设置的任务中心:Here is a precompiled C# example of how to write a function which uses an OrchestrationClientBinding to work with a task hub that is configured as an App Setting:

[FunctionName("HttpStart")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, methods: "post", Route = "orchestrators/{functionName}")] HttpRequestMessage req,
    [OrchestrationClient(TaskHub = "%MyTaskHub%")] DurableOrchestrationClientBase starter,
    string functionName,
    ILogger log)
{
    // Function input comes from the request content.
    dynamic eventData = await req.Content.ReadAsAsync<object>();
    string instanceId = await starter.StartNewAsync(functionName, eventData);

    log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

    return starter.CreateCheckStatusResponse(req, instanceId);
}

下面是 JavaScript 所需的配置。And below is the required configuration for JavaScript. function.json 文件中的任务中心属性通过应用设置进行设置:The task hub property in the function.json file is set via App Setting:

{
    "name": "input",
    "taskHub": "%MyTaskHub%",
    "type": "orchestrationClient",
    "direction": "in"
}

任务中心名称必须以字母开头且只能包含字母和数字。Task hub names must start with a letter and consist of only letters and numbers. 如果未指定,默认名称是 DurableFunctionsHubIf not specified, the default name is DurableFunctionsHub.

Note

当共享存储帐户中有多个任务中心时,名称用于将一个任务中心与其他任务中心区分开来。The name is what differentiates one task hub from another when there are multiple task hubs in a shared storage account. 如果有多个函数应用共享一个共享存储帐户,则必须在 host.json 文件中为每个任务中心显式配置不同的名称。If you have multiple function apps sharing a shared storage account, you must explicitly configure different names for each task hub in the host.json files. 否则多个函数应用会相互竞争消息,这可能会导致未定义的行为。Otherwise the multiple function apps will compete with each other for messages, which could result in undefined behavior.

后续步骤Next steps