创建 SignalR 服务Create a SignalR Service

此示例脚本在名称随机生成的新资源组中创建新的 Azure SignalR 服务资源。This sample script creates a new Azure SignalR Service resource in a new resource group with a random name.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

如果选择在本地安装并使用 CLI,本文要求运行 Azure CLI 2.0 版或更高版本。If you choose to install and use the CLI locally, this article requires that you are running the Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

此脚本创建新的 SignalR 服务资源和新的资源组。This script creates a new SignalR Service resource and a new resource group.

#!/bin/bash

# Generate a unique suffix for the service name
let randomNum=$RANDOM*$RANDOM

# Generate a unique service and group name with the suffix
SignalRName=SignalRTestSvc$randomNum
#resource name must be lowercase
mySignalRSvcName=${SignalRName,,}
myResourceGroupName=$SignalRName"Group"

# Create resource group 
az group create --name $myResourceGroupName --location chinaeast2

# Create the Azure SignalR Service resource
az signalr create \
  --name $mySignalRSvcName \
  --resource-group $myResourceGroupName \
  --sku Standard_S1 \
  --unit-count 1 \
  --service-mode Default

# Get the SignalR primary connection string 
primaryConnectionString=$(az signalr key list --name $mySignalRSvcName \
  --resource-group $myResourceGroupName --query primaryConnectionString -o tsv)

echo "$primaryConnectionString"

记下为新资源组生成的实际名称。Make a note of the actual name generated for the new resource group. 如果要删除所有组资源,将使用该资源组名称。You will use that resource group name when you want to delete all group resources.

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation. 此脚本使用以下命令:This script uses the following commands:

CommandCommand 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az signalr createaz signalr create 创建 Azure SignalR 服务资源。Creates an Azure SignalR Service resource.
az signalr key listaz signalr key list 列出密钥,使用 SignalR 推送实时内容更新时,应用程序将使用这些密钥。List the keys, which will be used by your application when pushing real-time content updates with SignalR.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可在 Azure SignalR 服务文档中找到其他 Azure SignalR 服务 CLI 脚本示例。Additional Azure SignalR Service CLI script samples can be found in the Azure SignalR Service documentation.