用 IIS 7、ARR 與 Velocity 建設高性能的大型網站

 http://www.cnblogs.com/WizardWu/archive/2009/05/16/1458108.html

本帖是研討會中的一些雜記,搭配一些官方的文檔,經整合歸納後,介紹 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]。

2009051603124668.jpg

圖 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章