首页 > 其他分享 >【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题

【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题

时间:2022-12-10 13:04:53浏览次数:63  
标签:Function Singleton attribute Timer 实例 Azure

问题描述

在Azure Function Timer Trigger的函数中,添加了Singleton属性,当Function的实例变为3个后,发现Timer函数并没有在三个实例上同时运行,每次触发时,都只有在一个实例上运行。这时因为Singleton属性的原因吗?

【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题_触发器

 

问题解答

在调查Singleton属性中,确实有描述 Singleton 单例模式,保证Function 在多个实例中,只会在一个实例上运行。详见:​​https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#singleton-attribute​

The ​​Singleton​​​ attribute ensures that only one instance of a function runs, even when there are multiple instances of the host web app. The Singleton attribute uses ​​distributed locking​​ to ensure that one instance runs.

但是,在深入产看Function Timer 触发器的文档后,发现Timer 函数只会单实例运行。不存在并发情况!

【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题_单实例运行_02

 

通过实验测试,在没有使用Singleton属性的情况下,Timer函数只在一个实例上运行!

【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题_单实例运行_03

【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题_Singleton_04

 

 

参考资料

WebJob / Function Singleton attribute : ​​https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#singleton-attribute​​ 

Azure Functions 的计时器触发器 横向扩展 : ​​https://docs.azure.cn/zh-cn/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp#scale-out​

 

 

 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!



标签:Function,Singleton,attribute,Timer,实例,Azure
From: https://blog.51cto.com/u_13773780/5927480

相关文章