《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

Windows Azure Cloud Service (42) 使用Azure In-Role Cache缓存(1)Co-located Role的更多相关文章

  1. Windows Azure Cloud Service (43) 使用Azure In-Role Cache缓存(2)Dedicated Role

    <Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...

  2. Windows Azure Cloud Service (36) 在Azure Cloud Service配置SSL证书

    <Windows Azure Platform 系列文章目录> 在某些时候,我们需要在Azure PaaS Cloud Service配置HTTPS连接.本章将介绍如何在本地创建证书,然后 ...

  3. Windows Azure Cloud Service (11) PaaS之Web Role, Worker Role(上)

    <Windows Azure Platform 系列文章目录> 本文是对Windows Azure Platform (六) Windows Azure应用程序运行环境内容的补充. 我们知 ...

  4. [SDK2.2]Windows Azure Cloud Service (35) 使用VS2013发布Azure Cloud Service

    <Windows Azure Platform 系列文章目录> 好久没有更新BLOG了,今天我们继续Windows Azure相关的内容. 笔者最近把Visual Studio升级到了20 ...

  5. Windows Azure Cloud Service (38) 微软IaaS与PaaS比较

    <Windows Azure Platform 系列文章目录> 最近一直想总结Azure IaaS和PaaS的区别与比较,写个博文详细说明一下.建议读者在阅读之前,先熟悉微软PaaS和Ia ...

  6. Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台

    <Windows Azure Platform 系列文章目录> 本文将简单介绍,如何将企业内现有的ASP.NET应用程序迁移到Azure PaaS平台. 因为在迁移过程中,可能需要对现有的 ...

  7. Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)

    <Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...

  8. Windows Azure Cloud Service (47) 修改Cloud Service时区

    <Windows Azure Platform 系列文章目录> 本文介绍内容适合于Azure Global和Azure China 我们在使用Cloud Service的时候,会发现默认的 ...

  9. 如何使用 OneAPM 监控微软 Azure Cloud Service ?

    不知不觉微软 Azure 已经进入中国市场近两年的时间.那么 Azure 平台的性能究竟如何?资源加载的延迟.虚拟机的稳定性等问题是否切实满足客户期许.这些都是大家对微软 Azure 这个国外的云服务 ...

随机推荐

  1. ABP框架详解(七)Caching

    在ABP框架中存在一个缓存机制,使用ICache的继承类来存储最终需要缓存的数据,可以吧ICache看成一个字典对象,使用Key作为真实数据的具有唯一性的表示.使用上与字典对象完全相同,Get方法传递 ...

  2. struts2 国际化的一个日期封装bug

    输入用户的生日:时间格式是yyyy-MM-dd,这样的.使用struts2,在action中有一个熟悉是:private Date birth;struts2在默认的情况下会将birth值自动绑定,简 ...

  3. webview使用总结及注意事项

    1 网页 调用后台java代码 ,后台处理 一 网页上click事件 <a href="javascript:;" onclick="window.JsNative ...

  4. goalng 发布的版本中自动加上 git revision

    概述 起因是这样的,在编译发布 golang 工程时,希望版本号中包含有 git revision number. 但是,没有commit之前,是没法知道 revision number 的,comm ...

  5. SVN分支管理策略个人见解

    本篇目录 前言 SVN分支管理策略 VisualSVN Server TortoiseSVN客户端 Repository的创建 Check out trunk创建新项目MyProject trunk更 ...

  6. 用JQuery仿造QQ头像裁剪功能

    最近工作真心忙碌,几乎没时间写博客.今天趁周末来仿一个QQ头像裁剪功能插件.效果如下: 所有文件都可在我的Github上下载,从头到尾从简到繁按步骤一共分了9个HTML文件,每个步骤文件里的注释都写的 ...

  7. MySQL5:性能优化

    性能优化 优化MySQL数据库是数据库管理员和数据库开发人员的必备技能.MySQL优化,一方面是找出系统的瓶颈,提高MySQL数据库的整体性能:一方面需要合理的结构设计和参数调整,以提高用户操作响应的 ...

  8. CSS Font知识整理总结

    1.什么是字体 字体是文字的外在形式,就是文字的风格,是文字的外衣.比如行书.楷书.草书,都是一种字体.同样一个字每个人写起来都会有差异,可以说每个人都有一套潜在的字体库.对于web页面来说,字体就是 ...

  9. [浅学] 1、Node.js尝试_安装&运行第一个helloworld

    官网:https://nodejs.org/ 介绍:Node.js® is a platform built on Chrome's JavaScript runtime for easily bui ...

  10. [_CN] Eclipse精要与高级开发技术 note

    一 eclipse是基于java的 ide ,但根据其体系结构,开发插件,也可拓展到其他语言———————— 尽管如此,但还是很少听说用eclipse来写php或者c的 跨os 三个项目:eclips ...