azure多功能成像好用吗_Azure持久功能简介:模式和最佳实践

azure多功能成像好用吗

Authored with Steef-Jan Wiggers at Microsoft Azure

由Microsoft Azure的Steef-Jan Wiggers撰写

With Durable Functions, you can program a workflow and instantiate tasks in sequential or parallel order, or you can build a watch or support a human interaction flow (approval workflow). You can chain functions to control your flow. You can use fan-in and fan-out scenarios, correlating events, flexible automation, and long-running processes, and human interaction patterns that are hard to put in place with only functions or with logic apps.

使用“耐用功能”,您可以对工作流程进行编程并按顺序或并行顺序实例化任务,或者可以构建手表或支持人机交互流程( 批准工作流程 )。 您可以链接函数来控制流程。 您可以使用扇入和扇出场景,关联事件,灵活的自动化和长期运行的流程,以及仅通过功能或逻辑应用程序难以实现的人机交互模式。

链接功能 (Chaining functions)

The most natural and straightforward use of Durable Functions is chaining functions together. You have one orchestrator function that calls many functions in the order you desire. You can do this with functions alone and using Service Bus Queues, yet you will face some challenges:

持久功能最自然,最直接的用法是将功能链接在一起。 您拥有一个编排器函数,该函数以所需的顺序调用许多函数。 您可以单独使用功能并使用服务总线队列来完成此操作,但是您将面临一些挑战:

  • no visualization to show the relationship between functions and queues

    没有可视化显示功能和队列之间的关系
  • middle queues are an implementation detail, with a conceptual overhead

    中间队列是一个实现细节,具有概念上的开销
  • error handling adds a lot more complexity

    错误处理增加了更多的复杂性

Using Durable Functions, you will not run into those challenges. With an orchestrator you:

使用持久功能,您将不会遇到这些挑战。 与协调器一起您可以:

  • can have a central place to set the order of function calls (relations)

    可以在中心位置设置函数调用(关系)的顺序
  • need no management of queues — under the hood, Durable Functions use and manage storage queues

    无需管理队列-持久功能在后台使用和管理存储队列
  • have central error handling — when an error occurs in one of the activity functions, the error propagates back to the orchestrator

    具有集中的错误处理功能—当活动功能之一发生错误时,该错误会传播回协调器
//calls functions in sequence
public static async Task<object> Run (DurableOrchestrationContext ctx)
{ 
 try
 {
 var x = await ctx.CallFunctionAsync (“F1”);
 var y = await ctx.callFunctionAsync (“F2”, x);
 var z = await ctx.callFunctionAsync (“F3”, y);
 return = await ctx.CallFunctionAsync (“F4”, z);
 }
 
catch (Exception )
 {
 //global error handling /compensation goes here
 }
}

扇出/扇入 (Fan-out/Fan-in)

Fan-out/fan-in can be used when you need to execute one or more functions in parallel and, based on the results, you run some other tasks. With functions, you cannot put in place such an approach. Moreover, you will also face the challenges mentioned in the previous section. But, with Durable Functions, you can achieve fan-out/fan-in:

当您需要并行执行一个或多个功能,并根据结果运行一些其他任务时,可以使用扇出/扇入。 使用函数,您将无法采用这种方法。 此外,您还将面临上一节中提到的挑战。 但是,通过耐用功能,您可以实现扇出/扇入:

public static async Task Run (Durableorchestrationcontext ctx)
{
var parallelTasks = new List<Task<int>>();
//get a list of N work items to process in parallel
object []workBatch = await ctx.CallFunctionAsync<object[]> (“F1”);
for (int i = 0; i < workBatch.Length; i++)
{
Task<int> task = ctx.CallFunctionAsync <int> (“F2”, workBatch [i]); 
parallelTasks.Add (task);
}
await Task.WhenAll(parallelTasks);
//aggregate all N outputs and send result to F3
int sum = parallelTasks.Sum(t=> t.Result); 
await ctx.CallFunctionAsync (“F3”, sum);
}

HTTP异步响应 (HTTP Async Response)

With functions, it is possible that when you call another API you do not know the amount of time it would take before a response is returned. For example, latency and volume can cause the time it would make the API to process the request and return a response to be unknown.

使用函数,当您调用另一个API时,您可能不知道返回响应之前所花费的时间。 例如,延迟和数量会导致使API处理请求并返回响应的时间变得未知。

A function can time-out when using a Consumption plan. The state needs to be maintained, which is undesirable for functions, as they need to be stateless. Durable Functions provide built-in APIs that simplify the code you write for interacting with long-running function executions. Furthermore, the state is managed by the Durable Functions run-time.

使用消耗计划时,功能可能会超时。 需要保持状态,这对于功能是不希望的,因为它们需要是无状态的。 耐用函数提供了内置的API,这些API简化了您编写的代码以与长时间运行的函数执行进行交互。 此外,状态由“持久功能”运行时管理。

//HTTP-triggered function to start a new orchestrator function instance.
public static async Task<HttpResponseMessage> Run (
HttpReq uestMessage req, DurableOrchestrationClient starter,
string functionName,
Ilogger log)
{
//Function name comes from the request URL.
//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);
}

演员们 (Actors)

Another use is the watcher — a recurring process in a workflow such as a clean-up process. You can put this in place with a function. But, again, you will have some challenges:

观察者的另一个用途是观察者-工作流中的重复过程,例如清理过程。 您可以使用函数将其放置到位。 但是,再次,您将面临一些挑战:

  • functions are stateless and short-lived

    功能是无状态的,且存在时间短
  • read/write access to an external state needs to be synchronized

    对外部状态的读/写访问需要同步

With Durable Functions, you can have flexible recurrence intervals, task lifetime management, and the ability to create many watch processes from a single orchestration.

使用耐用功能,您可以具有灵活的重复间隔,任务生命周期管理以及从单个业务流程创建许多监视流程的能力。

public static async Task Run(DurableOrchestrationContext ctx)
{
int counterState = ctx.Getinput<int>();
string operation = await ctx.WaitForExternalEvent<string>(“operation”);
if (operation == “incr”)
{
counterState++;
}
else if (operation == “decr”)
{
counterstate --;
}
ctx.ContinueAsNew(counterState);
}

人际交往 (Human interaction)

Within organizations, you will face processes that require some human interaction such as approvals. Interactions like approvals require the availability of the approver. Thus, the process needs to be active for some time, and needs a reliable mechanism when the process times out. For instance, when an approval doesn’t occur within 72 hours, an escalation process must start. With Durable Functions, you can support such a scenario.

在组织内部,您将面临需要人工交互(例如批准)的流程。 诸如批准之类的交互需要批准者的可用性。 因此,该过程需要在一段时间内处于活动状态,并且在过程超时时需要可靠的机制。 例如,如果72小时之内未获得批准,则必须启动升级程序。 使用持久功能,您可以支持这种情况。

public static async Task Run(DurableOrchestrationContext ctx)
{
await ctx.CallFunctionAsync<object []>(“RequestApproval”);
using (var timeoutCts = new CancellationTokenSource())
{
DateTime dueTime = ctx.CurrentUtcDateTime.AddHours(72);
Task durableTimeout = ctx.CreateTimer(dueTime, 0, cts.Token);
Task<bool > approvalEvent = ctx.WaitForExternalEvent< bool>(“ApprovalEvent”);
if (approvalEvent == await Task .WhenAny(approvalEvent, durableTimeout ))
{
timeoutCts.Cancel();
await ctx .CallFunctionAsync(“HandleApproval”, approvalEvent.Result);
}
else
{
await ctx.CallFunctionAsy nc(“Escalate” );
}
}
}

示例实现:使用持久函数进行链接 (Sample implementation: Chaining using Durable Functions)

The Orchestrator Client is a function that can be triggered when a message is sent. This Client, a function, will call the Orchestrator and pass the order message.

Orchestrator客户端是可以在发送消息时触发的功能。 该客户端是一个函数,它将调用Orchestrator并传递订单消息。

public static async Task<HttpResponseMessage> Run (
HttpReq uestMessage req, DurableOrchestrationClient starter, string functionName,
Ilogger log)
{
//Function name comes from the request URL.
//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);
}

The Orchestrator will receive the order and call the activity functions.

协调器将接收订单并调用活动功能。

public static async Task Run(DurableOrchestrationContext context, object order, ILogger log)
{
log.LogInformation($”Data = ‘{order}’.”);
var orderDetail = (OrderDetail) order;
try
{
bool x = await context.CallActivityAsync<bool>(“WriteToDatabase”, orderDetail);
log.LogInformation($”Data storage = ‘{x}’.”);
if (x == true)
{
await context.CallActivityAsync<OrderDetail>(“WriteToArchive”, orderDetail);
await context.CallActivityAsync<OrderDetail>(“SendNotification”, orderDetail);
}
}
catch (Exception)
{
//ErrorHandling
}
}

Each of the activity functions will perform a task — in this case, store the order in a document collection in a CosmosDB instance, archive the stored message, and send a message to the queue to send out a notification via a logic app.

每个活动功能都将执行一项任务-在这种情况下,将订单存储在CosmosDB实例中的文档集中,将已存储的消息存档,然后将消息发送到队列以通过逻辑应用程序发出通知。

最佳实践 (Best practices)

With Durable Functions there are a few best practices to follow:

使用耐用功能,可以遵循一些最佳做法:

  • use the Azure App Insights app to monitor running instances and health, including Azure Functions

    使用Azure App Insights应用程序监视正在运行的实例和运行状况,包括Azure功能
  • the Durable Functions app also exposes the HTTP API for management. With the API methods, you can influence the course of action for your Durable Functions.

    耐用功能应用程序还公开了HTTP API进行管理。 使用API​​方法,您可以影响耐用功能的操作过程。
  • use version control with your durable function

    与持久功能一起使用版本控制
  • you can use side-by-side deployment, updating the name of your task hub on deployment. See Durable Functions Blue Green Deployment Strategies for more information.

    您可以使用并行部署,在部署时更新任务中心的名称。 有关更多信息,请参见持久功能蓝绿色部署策略

结语 (Wrap-up)

In this blog post, we hope you have a better understanding of the use of Durable Functions, and what value they offer. Durable Functions give you the ultimate control over a workflow, not achievable with alternative technologies such as logic apps or functions alone. Together with some of the best practices we consolidated, you should now be able to build sustainable solutions with Durable Functions.

在本博客中,我们希望您对耐用功能的使用以及它们提供的价值有更好的了解。 耐用的功能为您提供了对工作流程的最终控制,这是仅使用逻辑应用程序或功能等替代技术无法实现的。 加上我们整合的一些最佳实践,您现在应该能够使用耐用功能构建可持续的解决方案。

This article was originally published at Serverless360.

本文最初在Serverless360上发布。

翻译自: https://www.freecodecamp.org/news/an-introduction-to-azure-durable-functions-patterns-and-best-practices-b1939ae6c717/

azure多功能成像好用吗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值