azure - 二头肌 : Creating multiple topics with multiple subscriptions

标签 azure azureservicebus azure-servicebus-topics azure-bicep infrastructure-as-code

我是 bicep 新手,一直在努力实现 Bicep 脚本来部署具有许多主题和订阅的 Azure 服务总线。

我添加的每个主题都有可变数量的订阅(例如,通知主题可能有 3 个订阅,但分析主题可能有 2 个订阅)。

有没有办法可以循环遍历并创建所有主题资源,然后循环遍历所有订阅并将它们添加到正确的主题定义中?

最佳答案

这有点取决于我们在这里讨论的主题数量。 如果你只是创建一个小数字,那么我会做这样的事情;

TopicsAndSubs.bicep

@description('Name of the Service Bus namespace')
param serviceBusNamespaceName string

@description('Name of the Topic')
param serviceBusTopicName string = 'theweather'

param topicSubscriptions int = 3

resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' existing = {
  name: serviceBusNamespaceName
}

resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' = {
  parent: serviceBusNamespace
  name: serviceBusTopicName
  properties: {

  }

  resource sub 'subscriptions' = [for i in range(0,topicSubscriptions): {
    name: 'sub-${i}'
    properties: {}
  }]
}

<小时/>

但是,如果它变得更加古怪,那么您将需要使用模块(一个单独的主题文件和一个用于订阅的文件)。像这样的东西;

主题.bicep

@description('Name of the Service Bus namespace')
param serviceBusNamespaceName string

@description('Name of the Topic')
param topicsAndSubscriptions array = [
  {
    name: 'notification'
    subscriptions: [
      'none'
      'ntwo'
      'nthree'
      'nfour'
    ]
  }
  {
    name: 'analysis'
    subscriptions: [
      'aone'
      'atwo'
      'athree'
      'afour'
    ]
  }
]

resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' existing = {
  name: serviceBusNamespaceName
}

resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' = [ for topic in topicsAndSubscriptions: {
  parent: serviceBusNamespace
  name: topic.name
  properties: {

  }
}]

module subs 'sub.bicep' = [ for topic in topicsAndSubscriptions: {
  name: '${topic.name}-subs'
  params: {
    servicebusNamespaceName: serviceBusNamespaceName
    topicName: topic.name
    subscriptions: topic.subscriptions
  }
}]

订阅.bicep

param servicebusNamespaceName string
param topicName string
param subscriptions array = ['asubscription','anotherone']

resource sub 'Microsoft.ServiceBus/namespaces/topics/subscriptions@2022-10-01-preview' = [for i in subscriptions: {
  name: '${servicebusNamespaceName}/${topicName}/${i}'
  properties: {}
}]

关于azure - 二头肌 : Creating multiple topics with multiple subscriptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76516806/

相关文章:

azure - 服务总线 MessageSession 已接受,但接收返回 null

azureservicebus - 在 C# 中将 Azure 服务总线队列迁移到主题和订阅

azure - 在 Azure Functions Http 触发器上设置 UTF-8 编码

php - HTTP 请求中找到的 MAC 签名与使用 php 的任何计算签名 azure 集成不同

ajax - 通过 Javascript/AJAX 使用 Microsoft Translator

从 Azure Web 应用程序发送消息时 Azure ServiceBus 超时

c# - 如何处理对azure服务总线的多次访问

azure - 如何使用 bicep 在多个服务总线中创建多个主题/队列?

javascript - 如何使用 javascript 在 azure 函数应用程序中使用服务总线主题 session

Azure 逻辑应用程序和 http 阶跃响应