You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Azure App Service部署槽关联至APIM(非手动添加HTTP端点)

关联App Service部署槽到APIM的替代方法

方法1:Azure CLI命令直接创建关联

通过CLI可以绕过门户的限制,直接指定部署槽端点创建API:

  • 登录并切换到目标订阅:
    az login
    az account set --subscription <你的订阅ID>
    
  • 创建指向部署槽的API:
    az apim api create --resource-group <资源组名称> --service-name <APIM实例名称> \
      --name <自定义API名称> --display-name <API显示名称> \
      --service-url https://<部署槽名称>-<App Service名称>.azurewebsites.net \
      --protocols https
    

如果部署槽有OpenAPI/Swagger定义,可额外添加--specification-url参数导入接口文档。

方法2:Azure PowerShell脚本部署

用PowerShell实现同样的效果,适合自动化场景:

$apimCtx = New-AzApiManagementContext -ResourceGroupName <资源组名称> -ServiceName <APIM实例名称>
New-AzApiManagementApi -Context $apimCtx -Name <自定义API名称> -DisplayName <API显示名称> `
  -ServiceUrl "https://<部署槽名称>-<App Service名称>.azurewebsites.net" -Protocol Https

若部署槽使用自定义域名,直接替换ServiceUrl为自定义域名即可。

方法3:ARM模板批量部署

编写ARM模板定义API资源,指定部署槽作为后端,示例片段:

{
  "type": "Microsoft.ApiManagement/service/apis",
  "apiVersion": "2023-03-01-preview",
  "name": "[concat('<APIM实例名称>', '/<自定义API名称>')]",
  "properties": {
    "displayName": "<API显示名称>",
    "serviceUrl": "https://<部署槽名称>-<App Service名称>.azurewebsites.net",
    "protocols": ["https"],
    "path": "<可选路径前缀>"
  }
}

通过门户、CLI或PowerShell部署模板即可完成关联。

方法4:APIM策略+命名值动态切换后端

如果需要在同一API下灵活切换部署槽,可采用此方案:

  1. 在APIM的命名值中添加一个条目,比如AppServiceDevSlotUrl,值设为部署槽的端点URL。
  2. 编辑API的入站策略,替换后端服务地址:
    <set-backend-service base-url="{{AppServiceDevSlotUrl}}" />
    

后续只需修改命名值,就能快速切换到不同部署槽,适合CI/CD流水线集成。


内容的提问来源于stack exchange,提问作者ScriptKiddieOnAComputer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 19:22:40