雲中間層服務 - 區域感知負載均衡器 Ribbon

Ribbon 簡介
        Ribbon 是 Netflix 發佈的雲中間層服務開源項目,其主要功能是提供客戶側軟件負載均衡算法,將 Netflix 的中間層服務連接在一起。
        Why Ribbon?
        Ribbon 的區域感知負載均衡器的功能久經考驗。區域感知負載均衡器內置電路跳閘邏輯,可被配置基於區域同源關係(Zone Affinity,也就是更傾向於選擇發出調用的服務所在的託管區域內,這樣可用降低延遲,節省成本)選擇目標服務實例。它監控每個區域中運行的實例的運維行爲,而且能夠實時快速丟棄一整個區域。在面對整個區域的故障時,這幫我們提升了彈性。簡單的來說,在配置文件中列出LB後面所有的機器,ribbon 會自動幫你去連這些機器(基於某種規則,如隨機連接、輪流連接,基於響應時間等等)。我們很方便地使用 Ribbon 實現自定義負載均衡算法。
        Ribbon 的其他一些特性
  1. 發現服務,比如需要增加一些機器,可以實現 Ribbon 的一個接口將這個機器加到列表裏(請參考《Ribbon 和 wowza 的集成開發》的動態修改負載節點案例);
  2. 對 Cloud 平臺的支持:比如對 AWS 來說,有參數支持只返回同一個 zone 的服務器地址,這樣避免跨 zone 調用所引起的網絡延時;
  3. 故障恢復能力:可以將一些不可用的服務器剔除。
        Ribbon 架構圖
ribbon-architecture-dirgram
        Ribbon 時序圖
Ribbon-wowza-sequence
        如何使用 Ribbon?
        Ribbon 提供了框架來支持通用的 LB 機制,同時也提供了對 Netflix eureka 框架的支持。接下來我們來看一下如何在 Java 項目裏使用 Ribbon。
        配置文件(sample-client.properties)
[plain] view plain copy
 print?
  1. # Max number of retries on the same server (excluding the first try)  
  2. sample-client.ribbon.MaxAutoRetries=1  
  3.   
  4.   
  5. # Max number of next servers to retry (excluding the first server)  
  6. sample-client.ribbon.MaxAutoRetriesNextServer=1  
  7.   
  8.   
  9. # Whether all operations can be retried for this client  
  10. sample-client.ribbon.OkToRetryOnAllOperations=true  
  11.   
  12.   
  13. # Interval to refresh the server list from the source  
  14. sample-client.ribbon.ServerListRefreshInterval=2000  
  15.   
  16.   
  17. # Connect timeout used by Apache HttpClient  
  18. sample-client.ribbon.ConnectTimeout=3000  
  19.   
  20.   
  21. # Read timeout used by Apache HttpClient  
  22. sample-client.ribbon.ReadTimeout=3000  
  23.   
  24.   
  25. # Initial list of servers, can be changed via Archaius dynamic property at runtime  
  26. sample-client.ribbon.listOfServers=www.microsoft.com:80,www.yahoo.com:80,www.google.com:80  

        每個條目的格式是爲
<clientName>.<nameSpace>.<propertyName>=<value>
        clientName 將被接下來的工廠用於創建客戶端。nameSpace 是可配置的,默認是 "ribbon"。常用屬性名可以從 CommonClientConfigKey 獲取。
        這些屬性也可以被定義作系統屬性進行獲取,也可以以 Archaius 加載的任何屬性資源進行獲取。
        Java 代碼測試
[java] view plain copy
 print?
  1. public static void main(String[] args) throws Exception {  
  2.       ConfigurationManager.loadPropertiesFromResources("sample-client.properties");  // 1  
  3.       System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));  
  4.       RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");  // 2  
  5.       HttpClientRequest request = HttpClientRequest.newBuilder().setUri(new URI("/")).build(); // 3  
  6.       for (int i = 0; i < 20; i++)  {  
  7.         HttpClientResponse response = client.executeWithLoadBalancer(request); // 4  
  8.         System.out.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());  
  9.       }  
  10.       ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();  
  11.       System.out.println(lb.getLoadBalancerStats());  
  12.       ConfigurationManager.getConfigInstance().setProperty(  
  13.             "sample-client.ribbon.listOfServers""www.linkedin.com:80,www.google.com:80"); // 5  
  14.       System.out.println("changing servers ...");  
  15.       Thread.sleep(3000); // 6  
  16.       for (int i = 0; i < 20; i++)  {  
  17.         HttpClientResponse response = client.executeWithLoadBalancer(request);  
  18.         System.out.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());  
  19.         response.releaseResources();  
  20.       }  
  21.       System.out.println(lb.getLoadBalancerStats()); // 7  
  22. }  

        代碼解釋:
  1. 使用 Archaius ConfigurationManager 加載屬性;
  2. 使用 ClientFactory 創建客戶端和負載均衡器;
  3. 使用 builder 構建 http 請求。注意我們只支持 URI 的 "/" 部分的路徑,一旦服務器被負載均衡器選中,會由客戶端計算出完整的 URI;
  4. 調用 API client.executeWithLoadBalancer(),不是 exeucte() API;
  5. 動態修正配置中的服務器池;
  6. 等待服務器列表刷新(配置文件中定義的刷新間隔是爲 2 秒鐘);
  7. 打印出負載均衡器記錄的服務器統計信息。
        負載均衡器統計包含了很多用於監控的信息,可以用於負載均衡算法的輸入。
        關於 Ribbon 客戶端使用的小 demo(包括 LB 調用、動態調整負載均衡節點)請參考博客《Ribbon 和 wowza 的集成開發》。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章