教程:模拟在访问读取访问冗余存储时出现的故障Tutorial: Simulate a failure in accessing read-access redundant storage

本教程是一个系列中的第二部分。This tutorial is part two of a series. 本教程通过模拟一个故障,介绍读取访问异地冗余 (RA-GRS) 的优点。In it, you learn about the benefits of a read-access geo-redundant (RA-GRS) by simulating a failure.

若要模拟故障,可以使用静态路由FiddlerIn order to simulate a failure, you can use either Static Routing or Fiddler. 使用这两种方法都可以模拟这样一个故障:向读取访问异地冗余 (RA-GRS) 存储帐户的主终结点发送请求,导致应用程序改从辅助终结点读取内容。Both methods will allow you to simulate failure for requests to the primary endpoint of your read-access geo-redundant (RA-GRS) storage account, causing the application read from the secondary endpoint instead.

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

此系列的第二部分介绍如何:In part two of the series, you learn how to:

先决条件Prerequisites

开始学习本教程之前,请完成前一教程:使用 Azure 存储实现应用程序数据的高可用性Before you begin this tutorial, complete the previous tutorial: Make your application data highly available with Azure storage.

若要使用静态路由模拟故障,需要使用权限提升的命令提示符。To simulate a failure with static routing, you will use an elevated command prompt.

若要使用 Fiddler 模拟故障,请下载并安装 FiddlerTo simulate a failure using Fiddler, download and install Fiddler

使用无效的静态路由模拟失败Simulate a failure with an invalid static route

对于向读取访问异地冗余 (RA-GRS) 存储帐户的主终结点发出的所有请求,可以创建一个无效的静态路由。You can create an invalid static route for all requests to the primary endpoint of your read-access geo-redundant (RA-GRS) storage account. 本教程使用本地主机作为网关来路由向存储帐户发出的请求。In this tutorial, the local host is used as the gateway for routing requests to the storage account. 使用本地主机作为网关会导致向存储帐户主终结点发出的所有请求都以循环方式返回到主机内,随后导致请求失败。Using the local host as the gateway causes all requests to your storage account primary endpoint to loop back inside the host, which subsequently leads to failure. 执行以下步骤,使用无效的静态路由模拟失败和主终结点还原。Follow the following steps to simulate a failure, and primary endpoint restoration with an invalid static route.

启动和暂停应用程序Start and pause the application

使用前一篇教程中的说明启动示例并下载测试文件(确认该文件来自主存储)。Use the instructions in the previous tutorial to launch the sample and download the test file, confirming that it comes from primary storage. 然后,可以根据目标平台手动暂停示例或等待出现提示。Depending on your target platform, you can then manually pause the sample or wait at a prompt.

模拟故障Simulate failure

当应用程序处于暂停状态时,在 Windows 上以管理员身份打开命令提示符,或者在 Linux 上以 root 身份运行终端。While the application is paused, open a command prompt on Windows as an administrator or run terminal as root on Linux.

获取有关存储帐户主终结点域的信息,方法是在命令提示符下或终端中输入以下命令(请将 STORAGEACCOUNTNAME 替换为存储帐户的名称)。Get information about the storage account primary endpoint domain by entering the following command on a command prompt or terminal, replacing STORAGEACCOUNTNAME with the name of your storage account.

nslookup STORAGEACCOUNTNAME.blob.core.chinacloudapi.cn

将存储帐户的 IP 地址复制到文本编辑器中,供以后使用。Copy to the IP address of your storage account to a text editor for later use.

若要获取本地主机的 IP 地址,请在 Windows 命令提示符处键入 ipconfig,或者在 Linux 终端上键入 ifconfigTo get the IP address of your local host, type ipconfig on the Windows command prompt, or ifconfig on the Linux terminal.

若要添加目标主机的静态路由,请在 Windows 命令提示符下或 Linux 终端中键入以下命令(请将 <destination_ip> 替换为存储帐户 IP 地址,将 <gateway_ip> 替换为本地主机 IP 地址)。To add a static route for a destination host, type the following command on a Windows command prompt or Linux terminal, replacing <destination_ip> with your storage account IP address and <gateway_ip> with your local host IP address.

LinuxLinux

route add <destination_ip> gw <gateway_ip>

WindowsWindows

route add <destination_ip> <gateway_ip>

在运行示例的窗口中恢复应用程序,或者按下相应的键下载示例文件,并确认该文件来自辅助存储。In the window with the running sample, resume the application or press the appropriate key to download the sample file and confirm that it comes from secondary storage. 然后可以再次暂停示例或等待出现提示。You can then pause the sample again or wait at the prompt.

模拟主终结点还原Simulate primary endpoint restoration

若要模拟主终结点恢复正常,请从路由表中删除无效的静态路由。To simulate the primary endpoint becoming functional again, delete the invalid static route from the routing table. 这样就可以让发送到主终结点的所有请求通过默认网关来路由。This allows all requests to the primary endpoint to be routed through the default gateway. 在 Windows 命令提示符下或 Linux 终端中键入以下命令。Type the following command on a Windows command prompt or Linux terminal.

LinuxLinux

route del <destination_ip> gw <gateway_ip>

WindowsWindows

route delete <destination_ip>

然后可以恢复应用程序,或者按下相应的键再次下载示例文件,这一次请确认该文件来自主存储。You can then resume the application or press the appropriate key to download the sample file again, this time confirming that it once again comes from primary storage.

使用 Fiddler 模拟失败Simulate a failure with Fiddler

若要使用 Fiddler 来模拟失败,请针对向 RA-GRS 存储帐户的主终结点发出的请求注入一个失败的响应。To simulate failure with Fiddler, you inject a failed response for requests to the primary endpoint of your RA-GRS storage account.

以下部分介绍如何使用 fiddler 模拟失败和主终结点还原。The following sections depict how to simulate a failure and primary endpoint restoration with fiddler.

启动 FiddlerLaunch fiddler

打开 Fiddler,选择“规则”和“自定义规则”。Open Fiddler, select Rules and Customize Rules.

自定义 Fiddler 规则

此时,Fiddler ScriptEditor 会启动,并显示 SampleRules.js 文件。The Fiddler ScriptEditor launches and displays the SampleRules.js file. 此文件用于自定义 Fiddler。This file is used to customize Fiddler.

OnBeforeResponse 函数中粘贴以下代码示例(请将 STORAGEACCOUNTNAME 替换为存储帐户的名称)。Paste the following code sample in the OnBeforeResponse function, replacing STORAGEACCOUNTNAME with the name of your storage account. 根据示例,可能还需要将 HelloWorld 替换为所要下载的测试文件的名称(或类似于 sampleFile 的前缀)。Depending on the sample, you may also need to replace HelloWorld with the name of the test file (or a prefix such as sampleFile) being downloaded. 新代码已注释掉,以确保它不会立即运行。The new code is commented out to ensure that it doesn't run immediately.

完成后,依次选择“文件”和“保存”,保存所做的更改。Once complete, select File and Save to save your changes. 将 ScriptEditor 窗口保持打开,以执行后续步骤。Leave the ScriptEditor window open for use in the following steps.

    /*
        // Simulate data center failure
        // After it is successfully downloading the blob, pause the code in the sample,
        // uncomment these lines of script, and save the script.
        // It will intercept the (probably successful) responses and send back a 503 error. 
        // When you're ready to stop sending back errors, comment these lines of script out again 
        //     and save the changes.

        if ((oSession.hostname == "STORAGEACCOUNTNAME.blob.core.chinacloudapi.cn") 
            && (oSession.PathAndQuery.Contains("HelloWorld"))) {
            oSession.responseCode = 503;  
        }
    */

粘贴自定义的规则

启动和暂停应用程序Start and pause the application

使用前一篇教程中的说明启动示例并下载测试文件(确认该文件来自主存储)。Use the instructions in the previous tutorial to launch the sample and download the test file, confirming that it comes from primary storage. 然后,可以根据目标平台手动暂停示例或等待出现提示。Depending on your target platform, you can then manually pause the sample or wait at a prompt.

模拟故障Simulate failure

当应用程序处于暂停状态时,切换回到 Fiddler,并取消注释在 OnBeforeResponse 函数中保存的自定义规则。While the application is paused, switch back to Fiddler and uncomment the custom rule you saved in the OnBeforeResponse function. 请务必选择“文件”、“保存”来保存更改,使规则生效。Be sure to select File and Save to save your changes so the rule will take effect. 此代码查找对 RA-GRS 存储帐户发出的请求,如果路径包含示例文件的名称,则返回响应代码 503 - Service UnavailableThis code looks for requests to the RA-GRS storage account and, if the path contains the name of the sample file, returns a response code of 503 - Service Unavailable.

在运行示例的窗口中恢复应用程序,或者按下相应的键下载示例文件,并确认该文件来自辅助存储。In the window with the running sample, resume the application or press the appropriate key to download the sample file and confirm that it comes from secondary storage. 然后可以再次暂停示例或等待出现提示。You can then pause the sample again or wait at the prompt.

模拟主终结点还原Simulate primary endpoint restoration

在 Fiddler 中删除或再次注释掉自定义规则。In Fiddler, remove or comment out the custom rule again. 选择“文件”、“保存”,确保该规则不再生效。Select File and Save to ensure the rule will no longer be in effect.

在运行示例的窗口中恢复应用程序,或者按下相应的键下载示例文件,并再次确认该文件来自主存储。In the window with the running sample, resume the application or press the appropriate key to download the sample file and confirm that it comes from primary storage once again. 然后可以退出示例。You can then exit the sample.

后续步骤Next steps

本系列教程的第二部分介绍了如何模拟故障来测试读取访问异地冗余存储。In part two of the series, you learned about simulating a failure to test read access geo-redundant storage.

若要详细了解 RA-GRS 存储的工作原理以及相关风险,请阅读以下文章:To learn more about how RA-GRS storage works, as well as its associated risks, read the following article: