教程:使用流量管理器改善网站响应Tutorial: Improve website response using Traffic Manager

本教程介绍如何使用流量管理器创建具有高响应能力的网站,并以较低的延迟将用户流量定向到该网站。This tutorial describes how to use Traffic Manager to create a highly responsive website by directing user traffic to the website with the lowest latency. 通常,地理距离最短的数据中心的延迟最低。Typically, the datacenter with the lowest latency is the one that is closest in geographic distance.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 在 IIS 上创建两个运行基本网站的 VMCreate two VMs running a basic website on IIS
  • 创建两个测试 VM 以查看流量管理器的运作方式Create two test VMs to view Traffic Manager in action
  • 为运行 IIS 的 VM 配置 DNS 名称Configure DNS name for the VMs running IIS
  • 创建流量管理器配置文件以改善网站性能Create a Traffic Manager profile for improved website performance
  • 将 VM 终结点添加到流量管理器配置文件Add VM endpoints to the Traffic Manager profile
  • 查看正在运行的流量管理器View Traffic Manager in action

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

先决条件Prerequisites

为了查看流量管理器的运作方式,本教程要求部署以下各项:In order to see the Traffic Manager in action, this tutorial requires that you deploy the following:

  • 在不同 Azure 区域(“中国东部”和“中国北部”)运行的基本网站的两个实例。 Two instances of basic websites running in different Azure regions - China East and China North.
  • 用于测试流量管理器的两个测试 VM - 一个 VM 位于“中国东部”,另一个 VM 位于“中国北部”。 Two test VMs for testing the Traffic Manager - one VM in China East and the second VM in China North. 测试 VM 用于演示流量管理器如何将用户流量路由到同一区域中运行的网站,并提供最低的延迟。The test VMs are used to illustrate how Traffic Manager routes user traffic to the website that is running in the same region as it provides the lowest latency.

登录 AzureSign in to Azure

通过 https://portal.azure.cn 登录到 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn.

创建网站Create websites

在本部分,我们将创建两个网站实例,以便为 Azure 区域中的流量管理器配置文件提供两个服务终结点。In this section, you create two website instances that provide the two service endpoints for the Traffic Manager profile in two Azure regions. 通过以下步骤创建两个网站:Creating the two websites includes the following steps:

  1. 创建两个用于运行基本网站的 VM - 一个 VM 位于“中国东部”,另一个位于“中国北部”。 Create two VMs for running a basic website - one in China East, and the other in China North.
  2. 在每个 VM 上安装 IIS 服务器,并更新默认网站页面,该页面描述当用户访问网站时要连接到的 VM 名称。Install IIS server on each VM and update the default website page that describes the VM name that a user is connected to when visiting the website.

创建用于运行网站的 VMCreate VMs for running websites

本部分介绍如何创建两个分别位于 Azure 区域“中国东部”和“中国北部”的 VM:myIISVMChinaEastmyIISVMChinaNorthIn this section, you create two VMs myIISVMChinaEast and myIISVMChinaNorth in the China East and China North Azure regions.

  1. 在 Azure 门户的左上角选择“创建资源”, 在"新建"页中的搜索框中输入 Windows Server 2019 Datacenter, 选择回车键 。On the upper, left corner of the Azure portal, select Create a resource , Enter Windows Server 2019 Datacenter in search filter of New page, then press the Enter key.

  2. 在“创建虚拟机”中,在“基本信息”选项卡中键入或选择以下值: In Create a virtual machine, type or select the following values in the Basics tab:

    • 订阅 > 资源组:选择“新建”,然后键入 myResourceGroupTM1Subscription > Resource Group: Select Create new and then type myResourceGroupTM1.
    • 实例详细信息 > 虚拟机名称:键入“myIISVMChinaEast” 。Instance Details > Virtual machine name: Type myIISVMChinaEast.
    • “实例详细信息” > “区域”: 选择“中国东部”。 Instance Details > Region: Select China East.
    • “管理员帐户” > “用户名”:输入所选用户名。Administrator Account > Username: Enter a user name of your choosing.
    • “管理员帐户” > “密码”:输入所选密码。Administrator Account > Password: Enter a password of your choosing. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    • “入站端口规则” > “公共入站端口”:选择“允许所选端口” 。Inbound Port Rules > Public inbound ports: Select Allow selected ports.
    • “入站端口规则” > “选择入站端口”:在下拉框中选择“RDP”和“HTTP”。 Inbound Port Rules > Select inbound ports: Select RDP and HTTP in the pull down box.
  3. 选择“管理”选项卡,或者选择“下一步: 磁盘”,然后选择“下一步: 网络”,然后选择“下一步: 管理”。Select the Management tab, or select Next: Disks, then Next: Networking, then Next: Management. 在“监视” 下,将“启动诊断” 设置为“关闭”。 Under Monitoring, set Boot diagnostics to Off.

  4. 选择“查看 + 创建” 。Select Review + create.

  5. 查看设置,并单击“创建”。 Review the settings, and then click Create.

  6. 按步骤创建另一个 VM,其名称为“myIISVMChinaNorth” ,其“资源组”名称为“myResourceGroupTM2” ,其“位置”为“中国北部”,所有其他设置与 myIISVMChinaEast 相同。 Follow the steps to create a second VM named myIISVMChinaNorth, with a Resource group name of myResourceGroupTM2, a location of China North, and all the other settings the same as myIISVMChinaEast.

  7. 创建 VM 可能需要数分钟的时间。The VMs take a few minutes to create. 在两个 VM 完成创建之前,不要继续执行剩余的步骤。Do not continue with the remaining steps until both VMs are created.

    创建 VM

安装 IIS 并自定义默认网页Install IIS and customize the default web page

在本部分中,将在 myIISVMChinaEast 和 myIISVMChinaNorth 这两个 VM 上安装 IIS 服务器,然后更新默认网站页面 。In this section, you install the IIS server on the two VMs myIISVMChinaEast and myIISVMChinaNorth, and then update the default website page. 自定义网站页面显示从 Web 浏览器访问网站时要连接到的 VM 的名称。The customized website page shows the name of the VM that you are connecting to when you visit the website from a web browser.

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中,单击位于 myResourceGroupTM1 资源组中的“myIISVMChinaEast”。 Select All resources in the left-hand menu, and then from the resources list click myIISVMChinaEast that is located in the myResourceGroupTM1 resource group.

  2. 在“概述”页上单击“连接”,然后在“连接到虚拟机”中选择“下载 RDP 文件”。 On the Overview page, click Connect, and then in Connect to virtual machine, select Download RDP file.

  3. 打开下载的 rdp 文件。Open the downloaded rdp file. 出现提示时,选择“连接” 。If prompted, select Connect. 输入在创建 VM 时指定的用户名和密码。Enter the user name and password you specified when creating the VM. 可能需要选择“更多选择” ,然后选择“使用其他帐户” ,以指定在创建 VM 时输入的凭据。You may need to select More choices, then Use a different account, to specify the credentials you entered when you created the VM.

  4. 选择“确定” 。Select OK.

  5. 你可能会在登录过程中收到证书警告。You may receive a certificate warning during the sign-in process. 如果收到警告,请选择“是”或“继续”以继续连接。 If you receive the warning, select Yes or Continue, to proceed with the connection.

  6. 在服务器桌面上导航到“Windows 管理工具” > “服务器管理器”。On the server desktop, navigate to Windows Administrative Tools>Server Manager.

  7. 在 VM1 上启动 Windows PowerShell 并使用以下命令安装 IIS 服务器并更新默认的 htm 文件。Launch Windows PowerShell on VM1 and using the following commands to install IIS server and update the default htm file.

    # Install IIS
    Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
    # Remove default htm file
    remove-item C:\inetpub\wwwroot\iisstart.htm
    
    #Add custom htm file
    Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from " + $env:computername)
    

    安装 IIS 并自定义网页

  8. 关闭与 myIISVMChinaEast 之间的 RDP 连接。Close the RDP connection with myIISVMChinaEast.

  9. 重复步骤 1-8,与 myResourceGroupTM2 资源组中的 VM myIISVMChinaNorth 建立 RDP 连接,以安装 IIS 并自定义其默认网页。Repeat steps 1-8 with by creating an RDP connection with the VM myIISVMChinaNorth within the myResourceGroupTM2 resource group to install IIS and customize its default web page.

为运行 IIS 的 VM 配置 DNS 名称Configure DNS names for the VMs running IIS

流量管理器基于服务终结点的 DNS 名称路由用户流量。Traffic Manager routes user traffic based on DNS name of the service endpoints. 本部分介绍如何为 IIS 服务器 myIISVMChinaEastmyIISVMChinaNorth 配置 DNS 名称。In this section, you configure the DNS names for the IIS servers - myIISVMChinaEast and myIISVMChinaNorth.

  1. 在左侧菜单中单击“所有资源”,然后在资源列表中,选择位于 myResourceGroupTM1 资源组中的“myIISVMChinaEast”。 Click All resources in the left-hand menu, and then from the resources list, select myIISVMChinaEast that is located in the myResourceGroupTM1 resource group.
  2. 在“概述”页上的“DNS 名称”下,选择“配置”。 On the Overview page, under DNS name, select Configure.
  3. 在“配置”页上的 DNS 名称标签下添加唯一的名称,然后选择“保存”。 On the Configuration page, under DNS name label, add a unique name, and then select Save.
  4. 针对位于 myResourceGroupTM2 资源组中名为 myIISVMChinaNorth 的 VM 重复步骤 1-3。Repeat steps 1-3, for the VM named myIISVMChinaNorth that is located in the myResourceGroupTM2 resource group.

创建测试 VMCreate test VMs

在本部分中,将在每个 Azure 区域(“中国东部”和“中国北部”)创建一个 VM(myVMChinaEast 和 myVMChinaNorth ) 。In this section, you create a VM (myVMChinaEast and myVMChinaNorth) in each Azure region (China East and China North). 稍后将使用这些 VM 来测试当你浏览到该网站时,流量管理器如何将流量路由到最近的 IIS 服务器。You will use these VMs to test how Traffic Manager routes traffic to the nearest IIS server when you browse to the website.

  1. 在 Azure 门户的左上角选择“创建资源”, 在"新建"页中的搜索框中输入 Windows Server 2019 Datacenter, 选择回车键 。On the upper, left corner of the Azure portal, select Create a resource , Enter Windows Server 2019 Datacenter in search filter of New page, then press the Enter key.

  2. 在“创建虚拟机”中,在“基本信息”选项卡中键入或选择以下值: In Create a virtual machine, type or select the following values in the Basics tab:

    • 订阅 > 资源组:选择“myResourceGroupTM1” 。Subscription > Resource Group: Select myResourceGroupTM1.
    • 实例详细信息 > 虚拟机名称:键入“myVMChinaEast” 。Instance Details > Virtual machine name: Type myVMChinaEast.
    • “实例详细信息” > “区域”: 选择“中国东部”。 Instance Details > Region: Select China East.
    • “管理员帐户” > “用户名”:输入所选用户名。Administrator Account > Username: Enter a user name of your choosing.
    • “管理员帐户” > “密码”:输入所选密码。Administrator Account > Password: Enter a password of your choosing. 密码必须至少 12 个字符长,且符合定义的复杂性要求The password must be at least 12 characters long and meet the defined complexity requirements.
    • “入站端口规则” > “公共入站端口”:选择“允许所选端口” 。Inbound Port Rules > Public inbound ports: Select Allow selected ports.
    • “入站端口规则” > “选择入站端口”:在下拉框中选择“RDP”。 Inbound Port Rules > Select inbound ports: Select RDP in the pull down box.
  3. 选择“管理”选项卡,或者选择“下一步: 磁盘”,然后选择“下一步: 网络”,然后选择“下一步: 管理”。Select the Management tab, or select Next: Disks, then Next: Networking, then Next: Management. 在“监视” 下,将“启动诊断” 设置为“关闭”。 Under Monitoring, set Boot diagnostics to Off.

  4. 选择“查看 + 创建” 。Select Review + create.

  5. 查看设置,并单击“创建”。 Review the settings, and then click Create.

  6. 按步骤创建另一个 VM,其名称为“myVMChinaNorth” ,其“资源组”名称为“myResourceGroupTM2” ,其“位置”为“中国北部”,所有其他设置与 myVMChinaEast 相同。 Follow the steps to create a second VM named myVMChinaNorth, with a Resource group name of myResourceGroupTM2, a location of China North, and all the other settings the same as myVMChinaEast.

  7. 创建 VM 可能需要数分钟的时间。The VMs take a few minutes to create. 在两个 VM 完成创建之前,不要继续执行剩余的步骤。Do not continue with the remaining steps until both VMs are created.

创建流量管理器配置文件Create a Traffic Manager profile

创建流量管理器配置文件,以便将用户流量定向到延迟最低的终结点。Create a Traffic Manager profile that directs user traffic by sending them to the endpoint with lowest latency.

  1. 在屏幕左上方,选择“创建资源” > “网络” > “查看全部” > “流量管理器配置文件” > “创建”。On the top left-hand side of the screen, select Create a resource > Networking > See All > Traffic Manager profile > Create.

  2. 在“创建流量管理器配置文件”中输入或选择以下信息,接受剩下的默认设置,然后选择“创建” :In the Create Traffic Manager profile, enter or select, the following information, accept the defaults for the remaining settings, and then select Create:

    设置Setting Value
    NameName 此名称必须在 trafficmanager.cn 区域中唯一,并会生成用于访问流量管理器配置文件的 DNS 名称 trafficmanager.cn。This name needs to be unique within the trafficmanager.cn zone and results in the DNS name, trafficmanager.cn that is used to access your Traffic Manager profile.
    路由方法Routing method 选择“性能” 路由方法。Select the Performance routing method.
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择资源组“myResourceGroupTM1” 。Select the Resource group myResourceGroupTM1.
    位置Location 选择“中国东部”。 Select China East. 此设置指的是资源组的位置,对将全局部署的流量管理器配置文件没有影响。This setting refers to the location of the resource group, and has no impact on the Traffic Manager profile that will be deployed globally.

    创建流量管理器配置文件

添加流量管理器终结点Add Traffic Manager endpoints

添加两个运行 IIS 服务器的 VM(myIISVMChinaEastmyIISVMChinaNorth),以便将用户流量路由到最靠近用户的终结点。Add the two VMs running the IIS servers - myIISVMChinaEast & myIISVMChinaNorth to route user traffic to the closest endpoint to the user.

  1. 在门户的搜索栏中,搜索在前面部分创建的流量管理器配置文件名称,并在显示的结果中选择该配置文件。In the portal's search bar, search for the Traffic Manager profile name that you created in the preceding section and select the profile in the results that the displayed.

  2. 在“流量管理器配置文件” 的“设置” 部分单击“终结点” ,然后单击“添加”。 In Traffic Manager profile, in the Settings section, click Endpoints, and then click Add.

  3. 输入或选择以下信息,保留剩下的默认设置,然后选择“确定” :Enter, or select, the following information, accept the defaults for the remaining settings, and then select OK:

    设置Setting Value
    类型Type Azure 终结点Azure endpoint
    NameName myChinaEastEndpointmyChinaEastEndpoint
    目标资源类型Target resource type 公共 IP 地址Public IP Address
    目标资源Target resource 选择公共 IP 地址以显示同一订阅下具有公共 IP 地址的资源列表。Choose a Public IP address to show the listing of resources with Public IP addresses under the same subscription. 在“资源”中,选择名为 myIISVMChinaEast-ip 的公共 IP 地址。 In Resource, select the public IP address named myIISVMChinaEast-ip. 这是中国东部的 IIS 服务器 VM 的公共 IP 地址。This is the public IP address of the IIS server VM in China East.
  4. 重复步骤 2 和 3,为名为 myIISVMChinaNorth 的 IIS 服务器 VM 所关联的公共 IP 地址 myIISVMChinaNorth-ip 添加名为 myChinaNorthEndpoint 的另一个终结点。Repeat steps 2 and 3 to add another endpoint named myChinaNorthEndpoint for the public IP address myIISVMChinaNorth-ip that is associated with the IIS server VM named myIISVMChinaNorth.

  5. 添加完这两个终结点后,这两个终结点会显示在“流量管理器配置文件” 中,并且其监视状态为“联机” 。When the addition of both endpoints is complete, they are displayed in Traffic Manager profile along with their monitoring status as Online.

    添加流量管理器终结点

测试流量管理器配置文件Test Traffic Manager profile

在本部分,我们将测试流量管理器如何将用户流量路由到运行网站的最近 VM,以提供最低的延迟。In this section, you test how the Traffic Manager routes user traffic to the nearest VMs running the website to provide minimum latency. 若要查看流量管理器的运作方式,请完成以下步骤:To view the Traffic Manager in action, complete the following steps:

  1. 确定流量管理器配置文件的 DNS 名称。Determine the DNS name of your Traffic Manager profile.
  2. 按如下所述查看流量管理器的运作方式:View Traffic Manager in action as follows:
    • 在位于“中国东部”区域的测试 VM (myVMChinaEast) 上,通过 Web 浏览器浏览到流量管理器配置文件的 DNS 名称。 From the test VM (myVMChinaEast) that is located in the China East region, in a web browser, browse to the DNS name of your Traffic Manager profile.
    • 在位于“中国北部”区域的测试 VM (myVMChinaNorth) 上,通过 Web 浏览器浏览到流量管理器配置文件的 DNS 名称。 From the test VM (myVMChinaNorth) that is located in the China North region, in a web browser, browse to the DNS name of your Traffic Manager profile.

确定流量管理器配置文件的 DNS 名称Determine DNS name of Traffic Manager profile

在本教程中,为简单起见,我们将使用流量管理器配置文件的 DNS 名称来访问网站。In this tutorial, for simplicity, you use the DNS name of the Traffic Manager profile to visit the websites.

可按如下所述确定流量管理器配置文件的 DNS 名称:You can determine the DNS name of the Traffic Manager profile as follows:

  1. 在门户的搜索栏中,搜索在前面部分中创建的流量管理器配置文件名称。In the portal's search bar, search for the Traffic Manager profile name that you created in the preceding section. 在显示的结果中,单击流量管理器配置文件。In the results that are displayed, click the traffic manager profile.

  2. 单击“概览”。 Click Overview.

  3. “流量管理器配置文件” 会显示新建的流量管理器配置文件的 DNS 名称。The Traffic Manager profile displays the DNS name of your newly created Traffic Manager profile. 在生产部署中,可以使用 DNS CNAME 记录配置一个指向流量管理器域名的虚构域名。In production deployments, you configure a vanity domain name to point to the Traffic Manager domain name, using a DNS CNAME record.

    流量管理器 DNS 名称

查看正在运行的流量管理器View Traffic Manager in action

在本部分,我们可以查看流量管理器的运作方式。In this section, you can see the Traffic Manager is action.

  1. 在左侧菜单中选择“所有资源”,然后在资源列表中,单击位于 myResourceGroupTM1 资源组中的“myVMChinaEast”。 Select All resources in the left-hand menu, and then from the resources list click myVMChinaEast that is located in the myResourceGroupTM1 resource group.

  2. 在“概述”页上单击“连接”,然后在“连接到虚拟机”中选择“下载 RDP 文件”。 On the Overview page, click Connect, and then in Connect to virtual machine, select Download RDP file.

  3. 打开下载的 rdp 文件。Open the downloaded rdp file. 出现提示时,选择“连接” 。If prompted, select Connect. 输入在创建 VM 时指定的用户名和密码。Enter the user name and password you specified when creating the VM. 可能需要选择“更多选择” ,然后选择“使用其他帐户” ,以指定在创建 VM 时输入的凭据。You may need to select More choices, then Use a different account, to specify the credentials you entered when you created the VM.

  4. 选择“确定” 。Select OK.

  5. 你可能会在登录过程中收到证书警告。You may receive a certificate warning during the sign-in process. 如果收到警告,请选择“是”或“继续”以继续连接。 If you receive the warning, select Yes or Continue, to proceed with the connection.

  6. 在 VM myVMChinaEast 上的 Web 浏览器中,键入流量管理器配置文件的 DNS 名称,以查看网站。In a web browser on the VM myVMChinaEast, type the DNS name of your Traffic Manager profile to view your website. 由于 VM 位于“中国东部”,因此你将路由到位于“中国东部”的最近 IIS 服务器 myIISVMChinaEast 上托管的最近网站。 Since the VM located in China East, you are routed to the nearest website hosted on the nearest IIS server myIISVMChinaEast that is located in China East.

    测试流量管理器配置文件

  7. 接下来,使用步骤 1-5 连接到位于“中国北部”的 VM myVMChinaNorth,然后从此 VM 浏览到流量管理器配置文件域名。 Next, connect to the VM myVMChinaNorth located in China North using steps 1-5 and browse to the Traffic Manager profile domain name from this VM. 由于 VM 位于“中国北部”,因此,现在你会路由到位于“中国北部”的最近 IIS 服务器 myIISVMChinaNorth 上托管的网站。 Since the VM located in China North, you are now routed to the website hosted on nearest the IIS server myIISVMChinaNorth that is located in China North.

    测试流量管理器配置文件

删除流量管理器配置文件Delete the Traffic Manager profile

不再需要时,请删除资源组(ResourceGroupTM1ResourceGroupTM2)。When no longer needed, delete the resource groups (ResourceGroupTM1 and ResourceGroupTM2). 为此,请选择资源组(ResourceGroupTM1ResourceGroupTM2),然后选择“删除”。 To do so, select the resource group (ResourceGroupTM1 or ResourceGroupTM2), and then select Delete.

后续步骤Next steps