WizardWu 編程網

一位台灣的工程師,接觸 .NET 逾十年,近年研究 SQL Server、Performance Tuning、手機應用

博客园 首页 新随笔 联系 订阅 管理

本帖是研讨会中的一些杂记,搭配一些官方的文档,经整合归纳后,介绍 IIS 7 如何搭配新一代的 ARR (Application Request Routing),建置 Server Farm 并达到比过去 NLB 更优的 Load Balancing 功能,此外还介绍微软新一代的分布式缓存技术 Velocity。

-------------------------------------------------------------------------------------------------------

过去及目前 Windows Server 中的 NLB (Network Load Balance, 负载均衡) 其缺点:

  • 用软件做的 NLB 功能较少。
  • 网络流量全挡在 NAT,而 NLB 都在内部网络的 Windows Server / IIS 上。
  • 只做到网络协议的 Layer 4。
  • 没有 Health Monitoring,无法知道 Web Server / AP 的目前情况;即使网站当机了,NLB 仍会将流量持续导引至此。
  • 较适合 Client 架构,较不适合 Web AP。

此外,若改用硬件做 NLB,虽然性能较佳,但价格较贵,且弹性较低。

------------------------------------------

因此 ARR (Application Request Routing) for IIS 7 出现了,其功能包括 [1], [2]:

Microsoft Application Request Routing Version 1 for IIS 7 is a proxy-based routing module that forwards HTTP requests to content servers based on HTTP headers and server variables, and load balance algorithms.  Application Request Routing (ARR) can be used to:

  • Increase application availability and scalability.
  • Better utilize content server resources.
  • Facilitate application deployment including pilot management and A/B testing.
  • Lower management costs and create opportunities for shared hosters.

Application Request Routing relies on the URL rewrite module to inspect incoming HTTP requests to make the routing decisions, and therefore, the URL rewrite module is required to enable Application Request Routing features.


ARR 的特性:

  • HTTP-based routing decisions:
    Unlike hardware load balancers that make the routing decisions at the IP level, Application Request Routing makes the routing decisions at the application level.  Working with the URL rewrite module, powerful routing rules can be written based on HTTP headers and server variables.
    达到网络协议的 Layer 7。
  • Load balance algorithms:
    A user-selected load balance algorithm is applied to determine which content server is most appropriate to service HTTP requests.  Six algorithms are provided.
    提供六种负载均衡的算法,可供网管人员选用。
  • Health monitoring:
    Both live traffic and specific URL tests are used to determine the health of content servers.  A set of configuration parameters are provided to define the meaning of server health.  It is also possible to disable both live traffic and URL tests and use a custom health monitoring provider by setting the health of the servers by calling RSCA APIs.
    可监控和检查 Server 是否仍在线提供服务,或已当机。可替代一些像是 WhatsUp 等网络监控及管理工具。
  • Client affinity:
    Using a cookie, Application Request Routing can affinitize all requests from a client to a content server.  It differentiates the clients behind NAT, so each client is treated independently.  This feature requires that the clients accept cookies.
  • Host name affinity:
    “Host name affinity” is a specific feature for shared hosters.  This feature changes the deployment topology to minimize and streamline administration and to create additional business opportunities.  For more information about this scenario, refer to Shared Hosting Deployment using Application Request Routing.  There are two providers, round robin and memory, to determine which server the host should be affinitized to.
  • Multiple server groups:
    Application Request Routing can manage multiple server groups, which are logical groupings of content servers in an environment.  This feature allows Application Request Routing to be used in pilot management and A/B testing scenarios.
    在几百、几千台服务器的环境中,可先试着导引 (pilot) 少数流量至某一台机器,若没问题再加大流量。
  • Management and monitoring via UI:
    All configuration settings and aggregated runtime statistics of Application Request Routing are managed and viewable using IIS Manager.
    所有的管理和配置,只要透过 IIS 图形操作界面即可。
  • Failed request tracing rules:
    Specific traces have been added to quickly troubleshoot and diagnose Application Request Routing.
    若服务器或 IIS 当机或无法提供服务,可选择是否要将所有的 error log 完整记录下来。

------------------------------------------

ARR (Application Request Routing) for IIS 7:

它是可免费下载的 Module (模组)。目前已可下载 version 1 的版本,下载网址:
http://www.iis.net/extensions

如先前所述,有: Health Monitoring、多种类的 Loading Balance Algorithms,如: Round Robin。


其可应用的情况,举个例,若您公司有个大型网站,有几百台或几千台机器,即可用 ARR 做成 Web Farm (Server Farm)。Server Farm 是一种让多台 Application Server 可基于「负载均衡」的逻辑性的集合 [4]。

图 1 在 IIS 7 可透过 ARR 定义和配置 Server Farm (服务器农场)


举个例子,ARR 的 pilot 功能,可先对某些机器,先试着导引 1 MB 的网络流量做测试,若没问题,则再加大流量。

此外,即使 IIS 当机或停止服务了,ARR 有 tracing 功能,可选择把所有的 error 都做 log 记录 (Failed Request Tracing)。

------------------------------------------

若要下载安装 ARR,可用 Microsoft Web Platform Installer 这套免费的工具作为辅助。它是一种 Web 的安装辅助工具,可简化微软各种 Web 平台上的组件,其下载和安装工作。例如若用它来下载、安装 ARR 时,就会先自动侦测,列出并告知您应该先安装哪些软件或组件。

Microsoft Web Platform Installer 的下载网址,目前有 1.0 版,以及 2.0 Beta 版:
http://www.microsoft.com/web/
http://www.microsoft.com/web/downloads/platform.aspx

------------------------------------------

安装 ARR 之前,必须要有 IIS 7;否则在安装 ARR 时,IIS 会自动被安装。

ARR 安装好后,在 IIS 7 左侧的窗格,下方会多出一个 Server Farm 的节点,此种 Server Farm 即为 ARR 的管理单位。

090516_02
图 2 可在 IIS 7 中创建一或多个 Server Farm

 

------------------------------------------

应用实例:

在一个购物商城,或电子商务网站里,网站的网管人员或程序开发人员,可能会想把「浏览」、「购买及结帐」的人分开 (避免一堆人只看不买,占用过多带宽),亦即让这两种 Web Page 及其网络流量能分开,以确保信用卡的交易,及其需要的流量,能用另外一台独立的 Server 来处理。若是用传统的做法,可能需要配置 Domain Name (DNS),如此虽然也可行但较无弹性。若能改用 ARR,则会更易解决,且不必更改配置即能办到 (这是研讨会的讲师说的)。

例如在 ARR 里,我们可以:

先创建两个 Server Farm:
Server1 -- 纯粹浏览网页用
Server2 -- 交易及付费专用

当用户单击「购买」按钮,或 URL 被导引至交易付费页面时,如: 购物网站网址/purchase.aspx

我们可先用 ARR 配置此 URL 的 Routing Rule (Edit Rule)。配置方式: 在下图 3 的「Test Pattern」窗体里,在「Pattern」文本框,我们可输入: */Purchase.aspx,如此即可将其 route 到特定的 Server Farm 上,例如我们上方提到的「交易及付费专用」的 Server2。

 

090516_03
图 3 Test Pattern 窗体里的 ARR Routing Rule 配置画面

 

-----------------------------------------------

ARR 的其他应用实例,像是还可以在大型网站里,弹性调动某几台 Server,将其系统资源,都专门用来处理网站中浏览人数最多的「首页」(例如: Yahoo 等入口网站);或某个大型购物网站里,某些特别热门、或同时在线人数很多的产品型录,或某个红火的目录 (网站的文件夹),能集中系统资源,去处理这些特别重要的网站服务。

-----------------------------------------------

ARR 目前有 v1 和 v2 的版本,v2 尚为 Beta 1 版,但 v2 的 Cache (缓存) 功能较丰富,可 Cache 各地不同机器中的信息,例如:
CARP (Cache Hierarchy Management) 这项功能,可让 Cache 要查找某项信息时,先锁定在同一层里的 Cache 去搜寻,避免一开始就向所有位置的 Cache 搜寻,影响了运作效率和性能。

此外,ARR 的 HA (High Availability) 也比 Windows Server 内附的 NLB 要好 (研讨会上讲师说的)。


-------------------------------------------------------------------------------------------------------
 

Cache (缓存) 和微软新一代的分布式缓存技术 Velocity

Velocity 是微软推出的分布式缓存解决方案 (Distributed In-Memory Cache),为开发高扩展性 (scalability)、高性能的应用程提供支持,可大幅节省应用程序访问数据库的次数,以大幅提升程序的性能。Velocity 可缓存各种 CLR object 以及可被序列化的 .NET 对象,它也将集成在 .NET Framework 4.0 中,并可透过 LINQ 语法读取和查询。

以下是 msdn 在去年六月的一篇文章中 [9],对 Velocity 的简介:

  “Velocity” is a distributed in-memory application cache platform for developing scalable, available, and high-performance applications. “Velocity” fuses (融合) memory across multiple computers to give a single unified cache view to applications. Applications can store any serializable CLR object without worrying about where the object gets stored. Scalability can be achieved by simply adding more computers on demand. “Velocity” also allows for copies of data to be stored across the cluster, thus protecting data against failures. “Velocity” can be configured to run as a service accessed over the network or can be run embedded with the distributed application. “Velocity” includes an ASP.NET session provider object that enables ASP.NET session objects to be stored in the distributed cache without having to write to databases.

-----------------------------------------------

微软和 .NET 的 Cache,可概分为三种:

(1) ASP.NET Cache (从 ASP.NET 1.x 时代即内建)
(2) IIS 7 Output Cache
(3) Project Velocity (即本帖介绍的新一代分布式缓存技术)

(1)、(2) 二者皆为 Local Cache,只能在本机使用,因此传统 ASP.NET 中的 Cache 在 Server Farm / Web Farm 无法让多台机器共用。而且若 Web Farm 中的每台机器都要维护自己的 Cache,就要把 Cache 的内容在每台机器上都拷贝一份,这样不仅浪费内存,而且可能会有数据不同步的问题,Velocity 就是为了要解决这些问题。

(3) Velocity 为微软的分布式缓存解决方案
  (3.1) Distributed memory cache
  (3.2) ASP.NET Session Provider: 可把 ASP.NET 的 Session 存入分布式环境中的共用缓存,在整个 Web Farm 中共用此 Session。


目前 Velocity 只是代号,非正式的名称。等到 6 月推出正式版时,微软会公布正式名称。Velocity 目前的版本为 CTP3 (Community Technology Preview 3)。

-----------------------------------------------

 

应用实例:


您公司有个大型网站,用到五台服务器,每台各有 2 GB 的内存。若在这五台机器上,都安装此 Velocity 的组件,就有 10 GB 的 Cache 可共用。如同 Grid 的概念,亦即将所有的服务器,全部集合成一个大型的 Cluster,让 Web Farm 里所有的机器,都能共享 In Memory Cache。

以后当客户端透过网络连至此网站时,只有第一次来时需要访问数据库,此时会把数据放到 Velocity 里,此后这些数据就可给整个 Web Farm 共用。以后当客户端连到 Web Farm 中、五台机器中的任何一台,都可取得所要的数据。

-----------------------------------------------


Velocity 其他特性:

* 微软官方有提供测试数据,证实 Velocity 的运作性能极优。
* Velocity 比传统的 Session State Server 更易分享在分布式环境中。
* 传统的做法,Session State Server 若只建置一台机器的话,若机器当机就无法提供服务了。
* 传统的做法,Session State Server 无扩充性,无法一直加入机器做扩充。
* Velocity 扩充性佳,只要加机器,就能一直把 Cache 加入 Web Farm,整个网站系统的性能就可望一直提升。
* Velocity 还支持客户端缓存,如果启用了客户端缓存后,在从缓存集群中取回数据时,将会放在客户端缓存中,这样下次取数据时将会直接从客户端缓存中取出,能够极大的提高效率,有点像是缓存的缓存 [8]。
* Velocity 是设计用来实作 HA (High Availability)。
* Velocity 是免费的。

此外,我们可把 ASP.NET 的 Session、Cache 放入 Velocity 中 (Distributed In-Memory Cache),然后再用 LINQ 去做查询。


Velocity 预计今年 6 月释出正式版,目前已有 CTP3 版本可下载 (2009/04/17)。您可从本帖下方,参考文檔的 [14] 网址下载,内附 .chm 说明文档:

Velocity 安装前,至少要有:
(1) .NET 3.5 SP1 
(2) PowerShell 1.0 (Windows Server 2008 已内建)
后者为 Velocity 的主要管理工具。


Velocity 支持架构设计上的 HA (High Availability) [9], [19]。若在 Web Farm 中,Primary Cache 无法提供服务了 (比方服务器当机),则 Secondary Cache 对自动升級成 Primary Cache。且在这个过程中,管理人员不用改写代码;而用户在浏览网站的期间,存储有重要信息的 Session 也不会因此而遗失 (这点研讨会中讲师有亲自示范,证实了这一点),尤其像电子商务等重要的网站系统,此点尤为重要,否则会让用户有不好的体验,甚至造成订单或生意被取消。
090516_04
图 4 若其中一台机器或缓存服务故障,缓存的数据及客户端的 Session 不会遗失


在 .NET Framework 4.0 中,将提供支持 Velocity 的相关 APIs,包括 System.Caching 这个新的 namespace [18],但目前 .NET 3.5 不直接支持 Velocity。而 Velocity 将来也可能成为 ASP.NET MVC、云端服务、SQL Server Data Services 的数据 Routing 及缓存的重要技术。


最后,在下方转贴 msdn 中 [9],所列出 Velocity 的重要特性:

  • Caches any serializable CLR object and provides access through simple cache APIs.
  • Supports enterprise scale: tens to hundreds of computers.
  • Configurable to run as a service accessed over the network or run embedded with the application.
  • Supports common cache configurations.
  • Supports dynamic scaling by adding new nodes.
  • Configurable number of backup copies to provide high availability.
  • Automatic load balancing.
  • Integration with administration and monitoring tools such as ETW, System Center, etc.
  • Provides tight integration with ASP.NET to be able to cache ASP.NET session data in the cache without having to write it to source databases. It can also be used as a cache for application data to be able to cache application data across the entire Web farm.
  • Follows the cache-aside architecture (also known as Explicit Caching) for V1. That is, you must decide explicitly which objects to put/remove in your applications and “Velocity” does not synchronize with any source database automatically.
  • Support for multiple client languages (for example, PHP, C#, C++, etc.).

 

总结:

在中国及美国等人口众多的国家,大型或超大型网站的建设势必与日俱增,而高性能、高弹性、可扩充性和稳定性也变得更为重要。微软在新一代的 Web 平台上,正持续推出更多功能强大的工具,试图扭转大家对 ASP.NET 只适合中小型网站的迷思,且计划性地为将来的云计算、各种 Data Services 和框架的推出,先做好铺路的工作。

 

-------------------------------------------------------------------------------------------------------

 

ARR 相关文档:

[1] Application Request Routing
http://www.iis.net/extensions/ApplicationRequestRouting

[2] Using the Application Request Routing Module
http://learn.iis.net/page.aspx/489/using-the-application-request-routing-module/

[3] download Microsoft Application Request Routing Version 1 for IIS 7 (x86 & x64)
http://iis.net/downloads/default.aspx?tabid=34&g=6&i=1709
http://iis.net/downloads/default.aspx?tabid=34&g=6&i=1712

[4] Define and Configure an Application Request Routing Server Farm
http://learn.iis.net/page.aspx/485/define-and-configure-an-application-request-routing-server-farm/

[5] IIS7.x, Server Farms and Ruby
http://blogs.msdn.com/nickhodge/archive/2009/02/24/iis7-5-server-farms-and-ruby.aspx

[6] IIS 7.0 - Application Request Routing (ARR) 论坛
http://forums.iis.net/1154.aspx

[7] 如何用 IIS 7 的 ARR 模组,实做 Reverse Proxy 机制 (台湾网页)
http://blog.miniasp.com/post/2009/04/Using-ARR-to-implement-Reverse-Proxy.aspx

-------------------------------------------

Velocity 相关文档:

[8] 使用微软分布式缓存服务 Velocity Part 1 ~ 3 (Terry Lee)
http://www.cnblogs.com/Terrylee/archive/2008/11/20/Microsoft-Distributed-Cache-Velocity-Part1.html
http://www.cnblogs.com/Terrylee/archive/2008/11/21/Microsoft-Distributed-Cache-Velocity-Part2.html
http://www.cnblogs.com/Terrylee/archive/2008/11/21/Microsoft-Distributed-Cache-Velocity-Part3.html

[9] Microsoft Project Code Named “Velocity”(msdn)
http://msdn.microsoft.com/en-us/library/cc645013.aspx

[10] Microsoft Project Code Named “Velocity”(msdn)
http://msdn.microsoft.com/en-us/data/cc655792.aspx

[11] Velocity: Microsoft's Distributed In-Memory Cache
http://www.infoq.com/news/2008/06/Velocity

[12] Microsoft project code named Velocity (msdn blog)
http://blogs.msdn.com/velocity/default.aspx

[13] Velocity CTP3 Released
http://adtmag.com/articles/2009/04/14/velocity-ctp3-released.aspx
http://netrsc.blogspot.com/2009/04/velocity-ctp3-released.html

[14] Velocity CTP3 (Community Technology Preview 3) download:
http://www.microsoft.com/downloads/details.aspx?FamilyId=B24C3708-EEFF-4055-A867-19B5851E7CD2&displaylang=en

[15] 使用微软分布式缓存服务 Velocity 第一课
http://www.xueit.com/html/2009-01/21_312_00.html

[16] Velocity:微软的分布式内存缓存
http://www.kuqin.com/dotnet/20080613/9457.html

[17] IIS 7: Extending Our Extensions Into Your Platforms
http://blogs.iis.net/wincat/archive/2008/12/05/iis-7-extending-our-extensions-into-your-platforms.aspx

[18] System.Caching in .Net 4.0 - Part 1
http://www.iamcodemonkey.com/blog/post/SystemCaching-in-Net-40-Part-1.aspx

[19] Project Velocity – Answer to Memcached?
http://geeks.netindonesia.net/blogs/risman/archive/2009/04/04/project-velocity-answer-to-memcached.aspx

-------------------------------------------

NLB 相关文档:

[20] Network LoadBalance 里 ASP.NET Session State 怎么处理 (台湾网页)
http://www.dotblogs.com.tw/dotjason/archive/2009/04/28/8221.aspx

[21] Windows Server 2008 怎么配置 Network LoadBalance (台湾网页)
http://www.dotblogs.com.tw/dotjason/archive/2009/04/27/8209.aspx

[22] Windows 2008 以 NLB 及复写功能达成 HA (台湾网页)
http://www.dotblogs.com.tw/dotjason/archive/2009/04/29/8241.aspx

-------------------------------------------

posted on 2009-05-16 04:11  WizardWu  阅读(15385)  评论(24编辑  收藏  举报