设为首页 收藏本站
查看: 1478|回复: 0

[经验分享] SignalR Scaleout with Redis

[复制链接]

尚未签到

发表于 2015-7-21 07:43:17 | 显示全部楼层 |阅读模式
  In this tutorial, you will use Redis to distribute messages across a SignalR application that is deployed on two separate IIS instances.
  Redis is an in-memory key-value store. It also supports a messaging system with a publish/subscribe model. The SignalR Redis backplane uses the pub/sub feature to forward messages to other servers.
DSC0000.jpg
  For this tutorial, you will use three servers:


  • Two servers running Windows, which you will use to deploy a SignalR application.
  • One server running Linux, which you will use to run Redis. For the screenshots in this tutorial, I used Ubuntu 12.04 TLS.
  If you don’t have three physical servers to use, you can create VMs on Hyper-V. Another option is to create VMs on Windows Azure.
  Although 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.

Overview
  Before we get to the detailed tutorial, here is a quick overview of what you will do.


  • Install Redis and start the Redis server.
  • Add these NuGet packages to your application:

    • Microsoft.AspNet.SignalR
    • Microsoft.AspNet.SignalR.Redis


  • Create a SignalR application.
  • Add the following code to Startup.cs to configure the backplane:

publicclassStartup{publicvoidConfiguration(IAppBuilder app){// Any connection or hub wire up and configuration should go hereGlobalHost.DependencyResolver.UseRedis("server", port,"password","AppName");
app.MapSignalR();}}
Ubuntu on Hyper-V
  Using Windows Hyper-V, you can easily create an Ubuntu VM on Windows Server.
  Download the Ubuntu ISO from http://www.ubuntu.com.
  In Hyper-V, add a new VM. In the Connect Virtual Hard Disk step, select Create a virtual hard disk.
DSC0001.jpg
  In the Installation Options step, select Image file (.iso), click Browse, and browse to the Ubuntu installation ISO.
DSC0002.jpg

Install 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
  This builds the Redis binaries in the src directory.
  By default, Redis does not require a password. To set a password, edit the redis.conf file, which is located in the root directory of the source code. (Make a backup copy of the file before you edit it!) Add the following directive to redis.conf:

requirepass YourStrongPassword1234
  Now start the Redis server:

src/redis-server redis.conf
DSC0003.jpg
  Open port 6379, which is the default port that Redis listens on. (You can change the port number in the configuration file.)

Create the SignalR Application
  Create a SignalR application by following either of these tutorials:


  • Getting Started with SignalR 2.0
  • Getting Started with SignalR 2.0 and MVC 5
  Next, we’ll modify the chat application to support scaleout with Redis. First, add the SignalR.Redis NuGet package to your project. In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, enter the following command:

Install-PackageMicrosoft.AspNet.SignalR.Redis
  Next, open the Startup.cs file. Add the following code to the Configuration method:

publicclassStartup{publicvoidConfiguration(IAppBuilder app){// Any connection or hub wire up and configuration should go hereGlobalHost.DependencyResolver.UseRedis("server", port,"password","AppName");
app.MapSignalR();}}

  • “server” is the name of the server that is running Redis.
  • port is the port number
  • “password” is the password that you defined in the redis.conf file.
  • “AppName” is any string. SignalR creates a Redis pub/sub channel with this name.
  For example:

GlobalHost.DependencyResolver.UseRedis("redis-server.cloudapp.net",6379,"MyStrongPassword1234","ChatApp");
Deploy and Run the Application
  Prepare your Windows Server instances to deploy the SignalR application.
  Add the IIS role. Include “Application Development” features, including the WebSocket Protocol.
DSC0004.jpg
  Also include the Management Service (listed under “Management Tools”).
DSC0005.jpg
  Install Web Deploy 3.0. When you run IIS Manager, it will prompt you to install Microsoft Web Platform, or you can download the intstaller. In the Platform Installer, search for Web Deploy and install Web Deploy 3.0
DSC0006.jpg
  Check that the Web Management Service is running. If not, start the service. (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.)
  By default, the Web Management Service listens on TCP port 8172. In Windows Firewall, create a new inbound rule to allow TCP traffic on port 8172. For more information, see Configuring Firewall Rules. (If you are hosting the VMs on Windows Azure, you can do this directly in the Windows Azure portal. See How to Set Up Communication with a Virtual Machine.)
  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.
  For more detailed documentation about web deployment, see Web Deployment Content Map for Visual Studio and ASP.NET.
  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.)
DSC0007.jpg
  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
DSC0008.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88821-1-1.html 上篇帖子: Redis系列(一)---启动流程分析 下篇帖子: 基于redis分布式缓存实现(新浪微博案例)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表