const sleep = t => new Promise(resolve => setTimeout(resolve, t));
while (true) {
const receivedMessages = await queueClient.receiveMessages();
if (receivedMessages.receivedMessageItems.length > 0) {
for (const msg of receivedMessages.receivedMessageItems) {
// call your logic to handle and delete the msg
// await queueClient.deleteMessage(message.messageId, message.popReceipt);
}
} else {
// queue is empty, check in 5 seconds
await sleep(5000)
}
}
# dapr-queue-component.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: ricardos-queue
spec:
type: bindings.azure.storagequeues
version: v1
metadata:
- name: accountName
value: "yourAccountName"
# you can use managed identity in Azure to not have to set this here
# the doc linked above shows how to do that.
# - name: accountKey
# value: "***********"
- name: queueName
value: "myqueue"
然后创建它:
➜ az containerapp env dapr-component set \
--name $CONTAINERAPPS_ENVIRONMENT --resource-group $RESOURCE_GROUP \
--dapr-component-name ricardos-queue \
--yaml dapr-queue-component.yaml
1条答案
按热度按时间1sbrub3j1#
您可以采取以下两种一般方法:
1.使用事件源SDK(如您所述)。这是特定SDK所要求的命令式(或声明式)。在本例中,它是Azure存储队列SDK,您需要类似于
1.使用一些其他的框架来为你抽象,并在消息上调用你的方法,例如dapr或Azure Functions。
*对于Dapr,创建一个类似https://docs.dapr.io/reference/components-reference/supported-bindings/storagequeues/的dapr组件(请注意,如果你在Azure容器应用上部署,你必须使用Azure的ARM(或bicep)或使用az cli定义该组件。此示例创建一个blob组件,但它应该与队列组件非常相似。因此,你将拥有一个类似于https://docs.dapr.io/reference/components-reference/supported-bindings/storagequeues/的dapr组件:
然后创建它:
型
最后你的代码看起来像这样(see dapr docs for more details):
***对于Azure Functions:**您可以安装Azure Functions cli,然后
在Azure容器应用上部署时,可以将缩放规则设置为如下所示 (有关定义缩放规则的详细信息,请参阅此处的文档,以便在队列上没有消息时应用将缩放为0)