如何在 azure 事件网格中推迟事件触发或事件接收?
Azure 事件网格 (AEG) 没有内置此功能,但是很容易使用 Azure 服务总线 (ASB) 实体来扩展它以处理延迟(计划)消息,就像 Sean 在回答中提到的那样。
以下屏幕 sn-p 显示了 Push-and-Pull with delay 订阅者的概念:
事件消息被推送到 ASB 主题中,并根据其订阅规则,将事件消息作为预定消息转发到队列实体。
主题订阅需要设置以下属性:
-
转发到
name of the queue/topic entity
-
$默认规则
过滤器:
1=1
动作(例如 10 分钟):
SET sys.TimeToLive = '00:10:00';
SET EnqueuedTimeUtc = sys.EnqueuedTimeUtc;
SET ScheduledEnqueueTimeUtc = sys.ExpiresAtUtc;
SET sys.ScheduledEnqueueTimeUtc = sys.ExpiresAtUtc;
SET sys.Label = 'Delay';
SET sys.TimeToLive = '01:00:00';
目标队列:
EnableDeadLetteringOnMessageExpiration = true
根据上面的设置,队列中的调度消息必须在'01:00:00'等TTL内消费,否则将消息发送到DLQ。肖恩评论中的更多细节。
使用ServiceBusTrigger函数,可以像AEG订阅者一样,以透明的方式从队列中拉出延迟的事件消息。
在这种情况下,当延迟事件被发送回 AEG 以进行 Fan-Out 分发并使用 Push-and-Push 模式时,以下示例显示ServiceBusTrigger 的这个实现,输出绑定到 AEG 自定义主题:
运行.csx:
#r "Newtonsoft.Json"
#r "Microsoft.Azure.EventGrid"
#r "Microsoft.Azure.ServiceBus"
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.ServiceBus;
public static async Task Run(Message queueItem, IAsyncCollector<EventGridEvent> outputEvents, ILogger log)
{
string jsontext = JToken.Parse(Encoding.UTF8.GetString(queueItem.Body)).ToString(Formatting.Indented);
log.LogInformation(jsontext);
EventGridEvent eventGridEvent = JsonConvert.DeserializeObject<EventGridEvent>(jsontext);
eventGridEvent.Topic = null;
eventGridEvent.Subject += "/delayed";
await outputEvents.AddAsync(eventGridEvent);
await Task.CompletedTask;
}
function.json:
{
"bindings": [
{
"name": "queueItem",
"type": "serviceBusTrigger",
"direction": "in",
"queueName": "aeg",
"connection": "rk2016_SERVICEBUS"
},
{
"type": "eventGrid",
"direction": "out",
"name": "outputEvents",
"topicEndpointUri": "AEG_TOPIC_XX_ENDPOINT",
"topicKeySetting": "AEG_TOPIC_XX_KEY"
}
]
}
如您所见,主题属性已修改为后缀/delayed,用于过滤目的,例如避免循环等。