从另一个Azure函数启动Azure函数的最佳实践 [英] Best practice for initiating Azure Function from another Azure Function

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

问题描述

我有一个场景,其中我有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?

我已阅读在这里,最好使用存储队列或服务总线在功能之间进行交叉通信.我的问题是-我需要存储的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触发器,我可以从功能A和功能B发送包含HTTP登录所需的所有相关信息的请求?

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?

非常感谢.

推荐答案

为将来参考(以及那些可能通过网络搜索找到此问题的人),我们建立了一个新的

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;
    }
}

FunctionA FunctionB 活动功能.计划它们的功能是

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/zh-CN/azure/azure-functions/durable-functions-overview

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆