![](/img/trans.png)
[英]Is there an out-of-box way to deserialize abstract classes in Json.Net?
[英]Does Azure have an out-of-box way of distributed locking with a key?
我希望能夠編寫像這樣的代碼
using (await LockAsync(x.Id))
{
// here goes code that is unsafe with respect to x.Id
// because it can be executed in multiple threads on
// the same instance or on multiple instances (e.g.
// multiple Azure Functions
}
我知道有一些開箱即用的方法可以在不使用密鑰的情況下鎖定整個執行上下文(例如函數中的[ActivityTrigger]
)並且我知道我可以使用 blob 租約滾動我自己的鍵控鎖,但是有沒有 -鑰匙鎖的盒子方式?
您可以使用Redis分布式鎖。
RedLock.net軟件包提供了您所需要的:
var resource = "the-thing-we-are-locking-on";
var expiry = TimeSpan.FromSeconds(30);
using (var redLock = await redlockFactory.CreateLockAsync(resource, expiry)) // there are also non async Create() methods
{
// make sure we got the lock
if (redLock.IsAcquired)
{
// do stuff
}
}
是的,它可以通過租用 Blob 功能在 Azure 存儲中實現。 Azure Functions 實際上使用此功能在函數的多個實例之間進行協調。
如果我想在x.Id
上創建獲取鎖,我將使用 Azure 存儲在路徑/my-app-locks/{x.Id}
處的 0 字節 blob 上獲得 60 秒的存儲租用。 如果我得到租約,我就知道我有鎖,如果沒有,那么其他人就有了,然后我可以輪詢它,直到他們釋放它。 您也可以在 60 年代租約到期之前續訂以延長時間。
這個博客有一些代碼和細節,以及其他實現方式 - https://medium.com/@fbeltrao/distributed-locking-in-azure-functions-bc4517c0306c
雖然不是“開箱即用”,但我發現了 2 個 Nuget 包,它們可以使用 Azure Blob 存儲租約(以及其他技術)來執行分布式鎖。 YMMV:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.