Windows Azure Cloud Service (42) 使用Azure In-Role Cache缓存(1)Co-located Role

简介:

 《Windows Azure Platform 系列文章目录

 

  Update 2016-01-12

  https://azure.microsoft.com/zh-cn/documentation/articles/cache-dotnet-how-to-use-in-role/

  微软宣布Azure Managed Cache Service和Azure In-Role Cache会在2016年11月30日下线,建议所有的用户采用Redis Cache。

  有关Redis Cache的内容,请参考:

  http://www.cnblogs.com/threestone/category/741409.html

 

 

  笔者很早以前就想写这篇文章了,昨天晚上项目正好遇到,今天记录下来。

 

  为什么要使用In-Role Cache?

  我们在开发PaaS Cloud Service的时候,PaaS VM都是无状态的。而且默认的Load Balancer是5要素(source IP, source port, destination IP, destination port, protocol type)

  http://azure.microsoft.com/blog/2014/04/08/microsoft-azure-load-balancing-services/

  如果遇到需要保留Session,使用Cache的时候,就会遇到一些问题。这时候,我们就可以使用In-Role Cache了。

 

  In-Role Cache介绍

  In-Role Cache分为两种方式:

  -  Co-located Role(共享模式)。在共享模式中,Caching是保存在多个Web Role Instance的内存中。

  -  Dedicated Role(专用模式)。在专用模式中,用户需要在工程文件中新建额外的Cache Worker Role,Caching不保存在Web Role VM的内存中,而是保存在新建的Cache Worker Role中。

    因为使用Dedicated Role,需要增加额外的Cache Worker Role,会增加计算成本

 

  如何使用In-Role Cache?  

  1.In-Role Cache不能在Virtual Machine虚拟机中使用,只能在PaaS Cloud Service使用。

  2.In-Role Cache不是通过Azure Management Portal创建的,而是安装完Azure SDK后,在Cloud Project中配置的。

 

  注意:本章介绍的是Co-located Role(共享模式)。

  Co-located Role的架构图,请参考http://weblogs.asp.net/scottgu/meet-the-new-windows-azure 

  

  

  1.使用管理员身份,运行Visual Studio 2013,创建一个新的Cloud Project,命名为LeiColocatedRole。图略。

  2.在New Windows Azure Cloud Service中,只需要添加ASP.NET Web Role。如下图:

  

  3.我们在项目文件中,选择WebRole1,右键,属性

  

  4.在Configuration栏目中,将Instance Count设置为2,设置2个Web Role Instance做高可用,如下图:

  

  

  5.在Caching栏目,修改以下红色部分

  

  (1)勾选Enable Caching,启用In-Role Caching  

  (2)点击Co-located Role,并且在Cache Size中,设置内存比,即PaaS Web Role Instance的30%内存用来保存Caching信息

  (3)设置Storage Account,因为Cache Cluster(PaaS Web Role Instance)会将Runtime的信息保存在Storage,所以这里一定要设置正确

  (4)Named Cache默认是default,我们可以通过Add Named Cache,增加新的Cache Name。

  另外,笔者在步骤4设置了Instance Count=2,所以我们可以勾选High Availability,保证高可用。这样不会因为某台Web Role Instance因为故障宕机导致Caching丢失

  (如果Instance Count=1的话,是无法勾选High Availability)

  

 

  6.然后我们点击WebRole1,右键,Manage NuGet Packages

  

  

  7.查找关键字Windows Azure Cache,查询到结果后,点击下图的Install进行安装

  

 

  8.安装完NuGet之后,会发现WebRole1项目下,增加了以下的引用:

  

  如果你使用ASP.NET,还会增加引用:Microsoft.Web.DistributedCache.dll

 

  9.我们修改WebRole1下的Web.config,找到autoDiscover节点,如下图:

  

  将上图中的[Cache role name or Service Endpoint]中的节点,修改为项目文件的RoleName,在笔者的Sample Code中,我们设置为WebRole1

  设置完毕后的结果如下图:

  

 

  10.将ASP.NET的Session指向Cache

  最后我们在Web.Config节点中,将sessionState节点的注释去掉。如下图:

  

  注意:如果在步骤5中,修改了默认的Named Cache "default",我们需要在上面的XML节点中,将dataCacheClientName的节点,修改为我们自定义的Cache Name。如上图红色部分:

 

 

  11.使用In-Role Cache

  在C#代码中,增加using语句:

using Microsoft.ApplicationServer.Caching;

  声明DataCache

 static DataCache myCache;

  实例化

 myCache = new DataCache("default");

  写Cache

myCache.Put("MyKey", TextBox1.Text);

  读Cache

string outputFromCache = myCache.Get("MyKey") as string;

 

 

  参考资料:http://blog.sanc.idv.tw/2012/06/windows-azure-windows-azure-co-located.html


本文转自Lei Zhang博客园博客,原文链接:http://www.cnblogs.com/threestone/p/4367859.html,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
106 32
|
3月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
67 11
|
6月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
Windows C#
Windows Service的安装卸载 和 Service控制
原文 Windows Service的安装卸载 和 Service控制 本文内容包括如何通过C#代码安装Windows Service(exe文件,并非打包后的安装文件)、判断Service是否存在、获得Service状态及启动停止Service。
912 0
|
6天前
|
安全 Unix 虚拟化
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 2 月更新)
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 2 月更新)
32 11
Windows 7 & Windows Server 2008 R2 简体中文版下载 (2025 年 2 月更新)
|
6天前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 2 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 2 月更新)
28 7
Windows Server 2025 中文版、英文版下载 (2025 年 2 月更新)
|
6天前
|
安全 虚拟化 Windows
Windows Server 2019 中文版、英文版下载 (2025 年 2 月更新)
Windows Server 2019 中文版、英文版下载 (2025 年 2 月更新)
41 22
|
13天前
|
安全 网络安全 数据安全/隐私保护
Windows Server 2025 Active Directory 重置用户密码
密码重置是管理员日常任务之一,用户因忘记或多次输错密码导致账户锁定时需进行重置。本文介绍在Active Directory服务器上重置密码的三种方法。
|
2月前
|
安全 关系型数据库 MySQL
Windows Server 安装 MySQL 8.0 详细指南
安装 MySQL 需要谨慎,特别注意安全配置和权限管理。根据实际业务需求调整配置,确保数据库的性能和安全。
302 9
|
3月前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
239 4