此示例脚本会创建新的 Azure SignalR 服务资源,用于将实时内容更新推送到客户端。 此脚本还会添加新的 Web 应用和应用服务计划,以托管使用 SignalR 服务的 ASP.NET Core Web 应用。 使用应用设置将 Web 应用配置为连接到新的 SignalR 服务资源,并使用 GitHub 身份验证进行身份验证。 Web 应用还配置为使用本地 Git 存储库部署资源。
如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅。
使用以下脚本通过其他订阅登录,将 <Subscription ID>
替换为 Azure 订阅 ID。 如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅。
az cloud set -n AzureChinaCloud
az login
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
# Create a SignalR Service with an App Service
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="China East 2"
resourceGroup="msdocs-azure-signalr-rg-$randomIdentifier"
tag="create-signal-service-with-app-service"
signalRSvc="msdocs-signalr-svc-$randomIdentifier"
webApp="msdocs-web-app-signalr-$randomIdentifier"
appSvcPlan="msdocs-app-svc-plan-$randomIdentifier"
signalRSku="Standard_S1"
unitCount="1"
serviceMode="Default"
planSku="Free"
# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag
# Create the Azure SignalR Service resource
echo "Creating $signalRSvc"
az signalr create \
--name $signalRSvc \
--resource-group $resourceGroup \
--sku $signalRSku \
--unit-count $unitCount \
--service-mode $serviceMode
# Create an App Service plan.
echo "Creating $appSvcPlan"
az appservice plan create --name $appSvcPlan --resource-group $resourceGroup --sku $planSku
# Create the Web App
echo "Creating $webApp"
az webapp create --name $webApp --resource-group $resourceGroup --plan $appSvcPlan
# Get the SignalR primary connection string
primaryConnectionString=$(az signalr key list --name $signalRSvc \
--resource-group $resourceGroup --query primaryConnectionString -o tsv)
echo $primaryConnectionString
# Add an app setting to the web app for the SignalR connection
az webapp config appsettings set --name $webApp --resource-group $resourceGroup \
--settings "AzureSignalRConnectionString=$primaryConnectionString"
根据 GitHub OAuth 应用注册更新以下脚本中的值。
GitHubClientId=<Replace with your GitHub OAuth app Client ID> GitHubClientSecret=<Replace with your GitHub OAuth app Client Secret>
添加用于 GitHub 身份验证的应用设置
az webapp config appsettings set --name $webApp --resource-group $resourceGroup --settings "GitHubClientSecret=$GitHubClientSecret"
配置 Git 部署并返回部署 URL。
az webapp deployment source config-local-git --name $webAppName --resource-group $resourceGroupName --query [url] -o tsv
使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。
az group delete --name $resourceGroup
表中的每条命令均链接到特定于命令的文档。 此脚本使用以下命令:
命令 | 说明 |
---|---|
az group create | 创建用于存储所有资源的资源组。 |
az signalr create | 创建 Azure SignalR 服务资源。 |
az signalr key list | 列出密钥,使用 SignalR 推送实时内容更新时,应用程序将使用这些密钥。 |
az appservice plan create | 创建用于托管 Web 应用的 Azure 应用服务计划。 |
az webapp create | 使用应用服务托管计划创建 Azure Web 应用。 |
az webapp config appsettings set | 为 Web 应用添加新的应用设置。 这些应用设置用于存储 SignalR 连接字符串和 GitHub OAuth 应用密钥。 |
az webapp deployment user set | 更新部署凭据。 |
az webapp deployment source config-local-git | 获取 git 存储库终结点的 URL 用于为 web 应用部署克隆和推送。 |
有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档。
可在 Azure SignalR 服务文档中找到其他 Azure SignalR 服务 CLI 脚本示例。