从另一个 Azure Function 启动 Azure Function 的最佳实践

Best practice for initiating Azure Function from another Azure Function(从另一个 Azure Function 启动 Azure Function 的最佳实践)

本文介绍了从另一个 Azure Function 启动 Azure Function 的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我有 2 个函数,比如说函数 A 和函数 B.

I have a scenario where I have 2 functions, let's say Function A and Function B.

目前,函数 A 和函数 B 都具有相同的逻辑来记录失败的活动,其中元数据记录到表存储和 JSON 记录到 Blob 存储.

Currently, Function A and Function B both have the same logic for logging failed activity where metadata is logged to Table Storage and JSON to Blob Storage.

编辑 --> 函数 A 和函数 B 位于两个不同的函数应用程序中(有意).功能 A 属于消费计划,功能 B 属于应用服务计划.

第一个问题 - 创建一个函数 C 并从 A 和 B 获取失败的活动日志记录逻辑并将其放入 C 中是否有意义?

First question - would it make sense to create a Function C and take the failed activity logging logic from both A and B and put it in C?

这消除了代码重复,逻辑集中在一个更易于管理的地方.

This removes code duplication and the logic is in one place which is easier to manage.

第二个问题 - 从 A 和 B 调用函数 C 的最佳方法是什么?

Second question - what would be the best way to call Function C from A and B?

我已阅读 这里 最好使用 Storage Queue 或 Service Bus 来实现 Functions 之间的交叉通信.我遇到的问题是 - 在大多数情况下,我需要存储的 JSON 将超过 256KB,因此我无法将其放入队列中以触发函数.

I have read here that it is best to use a Storage Queue or Service Bus for cross communication between Functions. The problem I have is - the JSON I need to store will in most cases exceed 256KB so I can't put it on a queue to trigger a Function.

那么,函数 C 是否可以作为 HTTP 触发器,并且我发送一个请求,其中包含通过 HTTP 从函数 A 和 B 记录所需的所有相关信息?

So, could Function C be a HTTP trigger and I send a request containing all of my relevant information needed to log via HTTP from Function A and B?

有什么理由不这样做吗?

Is there any reason not to do this?

非常感谢.

推荐答案

为了将来参考(以及那些可能通过网络搜索找到这个问题的人),我们构建了一个新的 Durable Functions 对 Azure Functions 的扩展,允许从另一个函数调用一个函数.您的具体问题可能会编码如下:

For future reference (and for those who may find this question via web searching), we've built a new Durable Functions extension to Azure Functions which allows calling one function from another. Your specific problem could potentially be coded up as follows:

public static async Task<object> Run(DurableOrchestrationContext ctx)
{
    try
    {
        var result = await ctx.CallActivityAsync<object>("FunctionA");
        var y = await ctx.CallActivityAsync<object>("FunctionB", result);
        return y;
    }
    catch (Exception e)
    {
        // error handling/compensation goes here
        await ctx.CallActivityAsync<object>("FunctionC", e);
        return null;
    }
}

FunctionAFunctionB 是 活动函数 可以存在于同一个函数应用中.上面安排它们的函数是 协调器功能.支持消费和应用服务计划.

FunctionA and FunctionB are activity functions that can live in the same function app. The function above which schedules them is an orchestrator function. Both Consumption and App Service plans are supported.

在幕后,CallActivityAsync 方法通过 Azure 存储队列发送消息以按名称触发指定的函数.通过自动切换到队列和 Blob 的组合来支持大型消息,以确保它们能够可靠地传递.

Under the covers the CallActivityAsync method sends a message via a Azure Storage queue to trigger the specified function by name. Large messages are supported by automatically switching to a combination of queues and blobs to ensure they can be delivered reliably.

然而,一个重要的限制是 FunctionA 和 FunctionB 必须在同一个函数应用中定义.因此,这与您的问题不完全匹配.但是,对于那些可以将其功能保留在同一个函数应用中的人来说,这是一个很好的解决方案.

One important constraint, however, is that FunctionA and FunctionB must be defined in the same function app. For that reason, it's not an exact match for your question. It's a great solution for those who can keep their functions in the same function app, however.

官方文档:https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-overview

GitHub:https://github.com/Azure/azure-functions-durable-extension

这篇关于从另一个 Azure Function 启动 Azure Function 的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从另一个 Azure Function 启动 Azure Function 的最佳实践

基础教程推荐