如何在Azure Service Bus的Bicep模板中配置自动扩缩容?
Azure Service Bus的自动扩缩容仅针对Premium层实例(只有该层支持Message Units),需要通过Microsoft.Insights/autoscaleSettings资源配合Service Bus命名空间来实现。以下是完整的Bicep配置示例及关键说明:
1. 定义Service Bus Premium命名空间
首先创建基础的Premium层命名空间,设置初始Message Unit数量:
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' = { name: 'your-sb-namespace' // 替换为你的命名空间名称 location: resourceGroup().location sku: { name: 'Premium' tier: 'Premium' capacity: 1 // 初始MU数量 } properties: { zoneRedundant: false // 根据需求启用区域冗余 } }
2. 添加自动扩缩容配置
创建关联到Service Bus的自动扩缩规则,可基于CPU使用率、消息吞吐量等指标调整MU数量:
resource sbAutoscale 'Microsoft.Insights/autoscaleSettings@2022-10-01' = { name: '${serviceBusNamespace.name}-autoscale' location: resourceGroup().location properties: { enabled: true targetResourceUri: serviceBusNamespace.id profiles: [ { name: 'Default' capacity: { minimum: '1' // 最小MU数量 maximum: '10' // 最大MU数量(根据区域配额调整) default: '1' // 异常时 fallback 的MU数量 } rules: [ // 扩容规则:CPU使用率超过70%时增加1个MU { metricTrigger: { metricName: 'CPU Usage' metricNamespace: 'Microsoft.ServiceBus/namespaces' resourceUri: serviceBusNamespace.id timeGrain: 'PT1M' statistic: 'Average' timeWindow: 'PT5M' timeAggregation: 'Average' operator: 'GreaterThan' threshold: 70 dividePerInstance: false } scaleAction: { direction: 'Increase' type: 'ChangeCount' value: '1' cooldown: 'PT5M' // 扩容冷却时间,避免频繁调整 } } // 扩容规则:入站消息量超过阈值时增加1个MU { metricTrigger: { metricName: 'Incoming Messages' metricNamespace: 'Microsoft.ServiceBus/namespaces' resourceUri: serviceBusNamespace.id timeGrain: 'PT1M' statistic: 'Total' timeWindow: 'PT5M' timeAggregation: 'Total' operator: 'GreaterThan' threshold: 10000 // 根据业务流量调整阈值 dividePerInstance: false } scaleAction: { direction: 'Increase' type: 'ChangeCount' value: '1' cooldown: 'PT5M' } } // 缩容规则:CPU使用率低于30%时减少1个MU { metricTrigger: { metricName: 'CPU Usage' metricNamespace: 'Microsoft.ServiceBus/namespaces' resourceUri: serviceBusNamespace.id timeGrain: 'PT1M' statistic: 'Average' timeWindow: 'PT10M' timeAggregation: 'Average' operator: 'LessThan' threshold: 30 dividePerInstance: false } scaleAction: { direction: 'Decrease' type: 'ChangeCount' value: '1' cooldown: 'PT10M' // 缩容冷却时间更长,避免误操作 } } ] } ] } }
关键配置说明
- SKU要求:必须使用Premium层,Standard层不支持Message Units和自动扩缩容。
- MU范围:
capacity.maximum不能超过当前Azure区域允许的Premium MU配额(多数区域默认上限为10,可提交配额申请提升)。 - 指标选择:除示例中的CPU和入站消息量,还可使用
Outgoing Messages、Queue Length、Topic Length等指标适配不同业务场景。 - 冷却时间:扩容冷却时间建议设置为5分钟,缩容设置为10分钟以上,避免短时间内频繁扩缩容导致资源波动。
内容的提问来源于stack exchange,提问作者cscsilviu89
相关产品推荐
相关产品推荐

