使用 Redis 的 SignalR 向外延展SignalR Scaleout with Redis

Mike Wassonby Mike Wasson

Warning

本檔不適用於最新版本的 SignalR。This documentation isn't for the latest version of SignalR. 請參閱ASP.NET Core SignalRTake a look at ASP.NET Core SignalR.

本主題中使用的軟體版本Software versions used in this topic

本主題的先前版本Previous versions of this topic

如需舊版 SignalR 的詳細資訊,請參閱SignalR 較舊的版本For information about earlier versions of SignalR, see SignalR Older Versions.

問題與意見Questions and comments

請留下有關您喜歡本教學課程的意見反應,以及我們可以在頁面底部的批註中改進的內容。Please leave feedback on how you liked this tutorial and what we could improve in the comments at the bottom of the page. 如果您有與本教學課程不直接相關的問題,您可以將其張貼至ASP.NET SignalR 論壇StackOverflow.comIf you have questions that are not directly related to the tutorial, you can post them to the ASP.NET SignalR forum or StackOverflow.com.

在本教學課程中,您將使用Redis ,將訊息散發至部署在兩個不同 IIS 實例上的 SignalR 應用程式。In this tutorial, you will use Redis to distribute messages across a SignalR application that is deployed on two separate IIS instances.

Redis 是記憶體中的索引鍵/值存放區。Redis is an in-memory key-value store. 它也支援具有發行/訂閱模型的訊息系統。It also supports a messaging system with a publish/subscribe model. SignalR Redis 背板會使用 pub/sub 功能將訊息轉送至其他伺服器。The SignalR Redis backplane uses the pub/sub feature to forward messages to other servers.

在本教學課程中,您將使用三部伺服器:For this tutorial, you will use three servers:

  • 兩部執行 Windows 的伺服器,您將用它來部署 SignalR 應用程式。Two servers running Windows, which you will use to deploy a SignalR application.
  • 一部執行 Linux 的伺服器,您將使用它來執行 Redis。One server running Linux, which you will use to run Redis. 在本教學課程的螢幕擷取畫面中,我使用了 Ubuntu 12.04 TLS。For the screenshots in this tutorial, I used Ubuntu 12.04 TLS.

如果您沒有三個要使用的實體伺服器,您可以在 Hyper-v 上建立 Vm。If you don't have three physical servers to use, you can create VMs on Hyper-V. 另一個選項是在 Azure 上建立 Vm。Another option is to create VMs on Azure.

雖然本教學課程使用官方 Redis 實,但也有 MSOpenTech 的Windows 埠 RedisAlthough this tutorial uses the official Redis implementation, there is also a Windows port of Redis from MSOpenTech. 安裝程式和設定不同,但步驟相同。Setup and configuration are different, but otherwise the steps are the same.

Note

具有 Redis 的 SignalR 向外延展不支援 Redis 叢集。SignalR scaleout with Redis does not support Redis clusters.

概觀Overview

在我們開始進行詳細的教學課程之前,請先快速瞭解您將執行的動作。Before we get to the detailed tutorial, here is a quick overview of what you will do.

  1. 安裝 Redis 並啟動 Redis 伺服器。Install Redis and start the Redis server.

  2. 將下列 NuGet 套件新增至您的應用程式:Add these NuGet packages to your application:

  3. 建立 SignalR 應用程式。Create a SignalR application.

  4. 將下列程式碼新增至 Startup.cs 以設定背板:Add the following code to Startup.cs to configure the backplane:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            GlobalHost.DependencyResolver.UseStackExchangeRedis("server", port, "password", "AppName");
            app.MapSignalR();
        }
    }
    

Hyper-v 上的 UbuntuUbuntu on Hyper-V

您可以使用 Windows Hyper-v,輕鬆地在 Windows Server 上建立 Ubuntu VM。Using Windows Hyper-V, you can easily create an Ubuntu VM on Windows Server.

http://www.ubuntu.com下載 Ubuntu ISO。Download the Ubuntu ISO from http://www.ubuntu.com.

在 Hyper-v 中,新增 VM。In Hyper-V, add a new VM. 在 [連接虛擬硬碟] 步驟中,選取 [建立虛擬硬碟]。In the Connect Virtual Hard Disk step, select Create a virtual hard disk.

在 [安裝選項] 步驟中,選取 [影像檔案(.iso) ],按一下 [流覽] ,然後流覽至 Ubuntu 安裝 iso。In the Installation Options step, select Image file (.iso), click Browse, and browse to the Ubuntu installation ISO.

安裝 RedisInstall Redis

遵循http://redis.io/download的步驟,下載並建立 Redis。Follow the steps at http://redis.io/download to download and build Redis.

wget http://redis.googlecode.com/files/redis-2.6.12.tar.gz
tar xzf redis-2.6.12.tar.gz
cd redis-2.6.12
make

這會在 src 目錄中建立 Redis 二進位檔。This builds the Redis binaries in the src directory.

根據預設,Redis 不需要密碼。By default, Redis does not require a password. 若要設定密碼,請編輯位於原始程式碼根目錄中的 redis.conf 檔案。To set a password, edit the redis.conf file, which is located in the root directory of the source code. (您必須先製作檔案的備份副本,然後再進行編輯!)將下列指示詞新增至 redis.conf(Make a backup copy of the file before you edit it!) Add the following directive to redis.conf:

requirepass YourStrongPassword1234

現在啟動 Redis 伺服器:Now start the Redis server:

src/redis-server redis.conf

開啟埠6379,這是 Redis 接聽的預設埠。Open port 6379, which is the default port that Redis listens on. (您可以變更設定檔案中的埠號碼)。(You can change the port number in the configuration file.)

建立 SignalR 應用程式Create the SignalR Application

遵循下列其中一個教學課程來建立 SignalR 應用程式:Create a SignalR application by following either of these tutorials:

接下來,我們將修改聊天應用程式,以支援使用 Redis 的向外延展。Next, we'll modify the chat application to support scaleout with Redis. 首先,將 Microsoft.AspNet.SignalR.StackExchangeRedis NuGet 封裝新增至您的專案。First, add the Microsoft.AspNet.SignalR.StackExchangeRedis NuGet package to your project. 在 Visual Studio 中,從 [工具] 功能表選取 [ NuGet 套件管理員],然後選取 [套件管理員主控台]。In Visual Studio, from the Tools menu, select NuGet Package Manager, then select Package Manager Console. 在 [Package Manager Console] 視窗中,輸入下列命令:In the Package Manager Console window, enter the following command:

Install-Package Microsoft.AspNet.SignalR.StackExchangeRedis

接下來,開啟 Startup.cs 檔案。Next, open the Startup.cs file. 將下列程式碼新增至Configuration方法:Add the following code to the Configuration method:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // Any connection or hub wire up and configuration should go here
        GlobalHost.DependencyResolver.UseStackExchangeRedis("server", port, "password", "AppName");
        app.MapSignalR();
    }
}
  • 「伺服器」是正在執行 Redis 的伺服器名稱。"server" is the name of the server that is running Redis.
  • port是埠號碼port is the port number
  • 「密碼」是您在 redis 檔案中定義的密碼。"password" is the password that you defined in the redis.conf file.
  • "AppName" 為任何字串。"AppName" is any string. SignalR 會使用此名稱建立 Redis pub/sub 通道。SignalR creates a Redis pub/sub channel with this name.

例如:For example:

GlobalHost.DependencyResolver.UseStackExchangeRedis("redis-server.cloudapp.net", 6379,
    "MyStrongPassword1234", "ChatApp");

部署和執行應用程式Deploy and Run the Application

準備您的 Windows Server 實例以部署 SignalR 應用程式。Prepare your Windows Server instances to deploy the SignalR application.

新增 IIS 角色。Add the IIS role. 包含「應用程式開發」功能,包括 WebSocket 通訊協定。Include "Application Development" features, including the WebSocket Protocol.

也請包含管理服務(列在 [管理工具] 底下)。Also include the Management Service (listed under "Management Tools").

安裝 Web Deploy 3.0。Install Web Deploy 3.0. 當您執行 IIS 管理員時,它會提示您安裝 Microsoft Web Platform,或者您可以下載安裝程式When you run IIS Manager, it will prompt you to install Microsoft Web Platform, or you can download the installer. 在平臺安裝程式中,搜尋 Web Deploy 並安裝 Web Deploy 3。0In the Platform Installer, search for Web Deploy and install Web Deploy 3.0

檢查 Web 管理服務是否正在執行。Check that the Web Management Service is running. 如果沒有,請啟動服務。If not, start the service. (如果您在 Windows 服務清單中看不到 [Web 管理服務],請確定您已在新增 IIS 角色時安裝管理服務)。(If you don't see Web Management Service in the list of Windows services, make sure that you installed the Management Service when you added the IIS role.)

根據預設,Web 管理服務會接聽 TCP 通訊埠8172。By default, the Web Management Service listens on TCP port 8172. 在 Windows 防火牆中,建立新的輸入規則,以允許埠8172上的 TCP 流量。In Windows Firewall, create a new inbound rule to allow TCP traffic on port 8172. 如需詳細資訊,請參閱設定防火牆規則For more information, see Configuring Firewall Rules. (如果您要在 Azure 上裝載 Vm,您可以直接在 Azure 入口網站中執行此動作。(If you are hosting the VMs on Azure, you can do this directly in the Azure portal. 請參閱如何設定虛擬機器的端點。)See How to Set Up Endpoints to a Virtual Machine.)

現在您已準備好從開發電腦將 Visual Studio 專案部署到伺服器。Now you are ready to deploy the Visual Studio project from your development machine to the server. 在方案總管中,以滑鼠右鍵按一下方案,然後按一下 [發佈]。In Solution Explorer, right-click the solution and click Publish.

如需 web 部署的詳細檔,請參閱Visual Studio 和 ASP.NET 的 Web 部署內容對應For more detailed documentation about web deployment, see Web Deployment Content Map for Visual Studio and ASP.NET.

如果您將應用程式部署至兩部伺服器,您可以在另一個瀏覽器視窗中開啟每個實例,並查看每個實例都會收到來自另一個的 SignalR 訊息。If you deploy the application to two servers, you can open each instance in a separate browser window and see that they each receive SignalR messages from the other. (當然,在生產環境中,這兩部伺服器會位於負載平衡器後方)。(Of course, in a production environment, the two servers would sit behind a load balancer.)

如果您想要查看傳送至 Redis 的訊息,可以使用Redis cli用戶端,它會與 Redis 一起安裝。If you're curious to see the messages that are sent to Redis, you can use the redis-cli client, which installs with Redis.

redis-cli -a password
SUBSCRIBE ChatApp