Java集羣優化——dubbo+zookeeper構建高可用分佈式集羣

  不久前,我們討論過Nginx+tomcat組成的集羣,這已經是非常靈活的集羣技術,但是當我們的系統遇到更大的瓶頸,全部應用的單點服務器已經不能滿足我們的需求,這時,我們要考慮另外一種,我們熟悉的內容,就是分佈式,而當下流行的Dubbo框架,不容我們忽視,這裏,咱們一起來探討一下這個框架的使用。


一,背景


    以前我們需要遠程調用他人的接口,我們是這麼做的:

                                              

我們遇到的問題:

        (1) 當服務越來越多時,服務URL配置管理變得非常困難,F5硬件負載均衡器的單點壓力也越來越大。
此時需要一個服務註冊中心,動態的註冊和發現服務,使服務的位置透明。
並通過在消費方獲取服務提供方地址列表,實現軟負載均衡和Failover,降低對F5硬件負載均衡器的依賴,也能減少部分成本。
        (2) 當進一步發展,服務間依賴關係變得錯蹤複雜,甚至分不清哪個應用要在哪個應用之前啓動,架構師都不能完整的描述應用的架構關係。
這時,需要自動畫出應用間的依賴關係圖,以幫助架構師理清理關係。
        (3) 接着,服務的調用量越來越大,服務的容量問題就暴露出來,這個服務需要多少機器支撐?什麼時候該加機器?
        爲了解決這些問題,第一步,要將服務現在每天的調用量,響應時間,都統計出來,作爲容量規劃的參考指標。
        其次,要可以動態調整權重,在線上,將某臺機器的權重一直加大,並在加大的過程中記錄響應時間的變化,直到響應時間到達閥值,記錄此時的訪問量,再以此訪問量乘以機器數反推總容量。

        爲解決這些問題,Dubbo爲我們做了什麼呢:

                               



負載均衡:

                                        


        這就是所謂的軟負載均衡!



        現在讓我們一起來接觸下這個優秀的框架:

        簡介


     架構如圖:

                                                      


節點角色說明:

        Provider: 暴露服務的服務提供方。

        Consumer: 調用遠程服務的服務消費方。

        Registry: 服務註冊與發現的註冊中心。

        Monitor: 統計服務的調用次調和調用時間的監控中心。

        Container: 服務運行容器。


調用關係說明:

        0. 服務容器負責啓動,加載,運行服務提供者。

        1. 服務提供者在啓動時,向註冊中心註冊自己提供的服務。

        2. 服務消費者在啓動時,向註冊中心訂閱自己所需的服務。

        3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連接推送變更數據給消費者。

        4. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選另一臺調用。

        5. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心。





        Dubbo提供了很多協議,Dubbo議、RMI協議Hessian協議,我們查看Dubbo源代碼,有各種協議的實現,如圖所示:

                                                                       



        我們之前沒用Dubbo之前時,大部分都使用Hessian來使用我們服務的暴露和調用,利用HessianProxyFactory調用遠程接口。


        上面是參考了Dubbo官方網介紹,接下來我們來介紹SpringMVCDubboZookeeper整合使用。


第三:Dubbo與Zookeeper、SpringMVC整合使用

  

  第一步:在Linux上安裝Zookeeper

       Zookeeper作爲Dubbo服務的註冊中心,Dubbo原先基於數據庫的註冊中心,沒采用ZookeeperZookeeper一個分佈式的服務框架,是樹型的目錄服務的數據存儲,能做到集羣管理數據 ,這裏能很好的作爲Dubbo服務的註冊中心,Dubbo能與Zookeeper做到集羣部署,當提供者出現斷電等異常停機時,Zookeeper註冊中心能自動刪除提供者信息,當提供者重啓時,能自動恢復註冊數據,以及訂閱請求。我們先在linux上安裝Zookeeper,我們安裝最簡單的單點,集羣比較麻煩。

    

先需要安裝JdK,從Oracle的Java網站下載,安裝很簡單,就不再詳述。

單機模式

        單機安裝非常簡單,只要獲取到 Zookeeper 的壓縮包並解壓到某個目錄如:C:\zookeeper-3.4.5\下,Zookeeper 的啓動腳本在 bin 目錄下,Windows 下的啓動腳本是 zkServer.cmd。

        在你執行啓動腳本之前,還有幾個基本的配置項需要配置一下,Zookeeper 的配置文件在 conf 目錄下,這個目錄下有 zoo_sample.cfg 和 log4j.properties,你需要做的就是將 zoo_sample.cfg 改名爲 zoo.cfg,因爲 Zookeeper 在啓動時會找這個文件作爲默認配置文件。下面詳細介紹一下,這個配置文件中各個配置項的意義。



[html] view plain copy

  1. <span style="font-size:18px;"># The number of milliseconds of each tick  

  2. tickTime=2000  

  3. # The number of ticks that the initial   

  4. # synchronization phase can take  

  5. initLimit=10  

  6. # The number of ticks that can pass between   

  7. # sending a request and getting an acknowledgement  

  8. syncLimit=5  

  9. # the directory where the snapshot is stored.  

  10. # do not use /tmp for storage, /tmp here is just   

  11. # example sakes.  

  12. dataDir=C:\\zookeeper-3.4.5\\data  

  13. dataLogDir=C:\\zookeeper-3.4.5\\log  

  14. # the port at which the clients will connect  

  15. clientPort=2181  

  16. #  

  17. # Be sure to read the maintenance section of the   

  18. # administrator guide before turning on autopurge.  

  19. #  

  20. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  

  21. #  

  22. # The number of snapshots to retain in dataDir  

  23. #autopurge.snapRetainCount=3  

  24. # Purge task interval in hours  

  25. # Set to "0" to disable auto purge feature  

  26. #autopurge.purgeInterval=1</span>  


  • tickTime:這個時間是作爲 Zookeeper 服務器之間或客戶端與服務器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。

  • dataDir:顧名思義就是 Zookeeper 保存數據的目錄,默認情況下,Zookeeper 將寫數據的日誌文件也保存在這個目錄裏。

  • dataLogDir:顧名思義就是 Zookeeper 保存日誌文件的目錄

  • clientPort:這個端口就是客戶端連接 Zookeeper 服務器的端口,Zookeeper 會監聽這個端口,接受客戶端的訪問請求。

        當這些配置項配置好後,你現在就可以啓動 Zookeeper 了,啓動後要檢查 Zookeeper 是否已經在服務,可以通過 netstat – ano 命令查看是否有你配置的 clientPort 端口號在監聽服務。


  第二步:配置dubbo-admin的管理頁面,方便我們管理頁面

        (1)下載dubbo-admin-2.4.1.war包,在windowstomcat部署,先把dubbo-admin-2.4.1放在tomcatwebapps/ROOT下,然後進行解壓

        (2)然後到webapps/ROOT/WEB-INF下,有一個dubbo.properties文件,裏面指向Zookeeper ,使用的是Zookeeper 的註冊中心,如圖所示:

        


[html] view plain copy

  1. <span style="font-size:18px;">dubbo.registry.address=zookeeper://127.0.0.1:2181  

  2. dubbo.admin.root.password=root  

  3. dubbo.admin.guest.password=guest</span>  



        (3)然後啓動tomcat服務,用戶名和密碼:root,並訪問服務,顯示登陸頁面,說明dubbo-admin部署成功,如圖所示:

      

  第三步:SpringMVC與Dubbo的整合,這邊使用的Maven的管理項目

        第一:我們先開發服務註冊的,就是提供服務,項目結構如圖所示:

         

        (1)test-maven-api項目加入了一個服務接口,代碼如下:

[java] view plain copy

  1. public interface TestRegistryService {  

  2.    public String hello(String name);  

  3. }  




       (2)test-maven-console在pom.xml加入Dubbo和Zookeeper的jar包、引用test-maven-apijar包,代碼如下:


  

[html] view plain copy

  1. <span style="font-size:18px;"><dependency>  

  2.         <groupId>cn.test</groupId>  

  3.         <artifactId>test-maven-api</artifactId>  

  4.         <version>0.0.1-SNAPSHOT</version>  

  5.     </dependency>  

  6.     

  7.       <dependency>  

  8.             <groupId>com.alibaba</groupId>  

  9.             <artifactId>dubbo</artifactId>  

  10.             <version>2.5.3</version>  

  11.         </dependency>  

  12.           

  13.          <dependency>  

  14.             <groupId>org.apache.zookeeper</groupId>  

  15.             <artifactId>zookeeper</artifactId>  

  16.             <version>3.4.6</version>  

  17.         </dependency>  

  18.     

  19.       <dependency>  

  20.          <groupId>com.github.sgroschupf</groupId>  

  21.          <artifactId>zkclient</artifactId>  

  22.          <version>0.1</version>  

  23. </dependency></span>  



        (3)test-maven-console實現具體的服務,代碼如下:

[java] view plain copy

  1. @Service("testRegistryService")  

  2. public class TestRegistryServiceImpl implements TestRegistryService {  

  3.     public String hello(String name) {    

  4.         return "hello"+name;  

  5.     }  

  6. }  



        (4)我們服務以及實現好了,這時要暴露服務,代碼如下:

  

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  4.     xmlns:jee="http://www.springframework.org/schema/jee"  

  5.     xmlns:tx="http://www.springframework.org/schema/tx"  

  6.     <span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>  

  7.     xmlns:context="http://www.springframework.org/schema/context"  

  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  

  9.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  

  10.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  

  11.     <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>  

  12.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"  

  13.     default-lazy-init="false" >  

  14.    <!-- 提供方應用名稱信息,這個相當於起一個名字,我們dubbo管理頁面比較清晰是哪個應用暴露出來的 -->  

  15.    <dubbo:application name="dubbo_provider"></dubbo:application>  

  16.    <!-- 使用zookeeper註冊中心暴露服務地址 -->    

  17.    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>  

  18.   <!-- 要暴露的服務接口 -->    

  19.   <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />        

  20. </beans>  




  


說明:

   dubbo:registry 標籤一些屬性的說明:

      1register是否向此註冊中心註冊服務,如果設爲false,將只訂閱,不註冊

      2check註冊中心不存在時,是否報錯。

      3subscribe是否向此註冊中心訂閱服務,如果設爲false,將只註冊,不訂閱

      4timeout註冊中心請求超時時間(毫秒)

      5address可以Zookeeper集羣配置,地址可以多個以逗號隔開等。

  dubbo:service標籤的一些屬性說明:

     1interface服務接口的路徑

     2ref引用對應的實現類的BeanID

     3registry向指定註冊中心註冊,在多個註冊中心時使用,值爲<dubbo:registry>id屬性,多個註冊中心ID用逗號分隔,如果不想將該服務註冊到任何registry,可將值設爲N/A

     4register 默認true ,該協議的服務是否註冊到註冊中心。


    (5)啓動項目,然後我們在Dubbo管理頁面上顯示,已經暴露的服務,但顯示還沒有消費者,因爲我們還沒實現消費者服務,如圖所示:

  

   第二:我們在開發服務消費者,就是調用服務,我們在新建一個新的消費者項目:

       

        (1)test-maven-server-console的pom.xml引入Dubbo和Zookeeper的jar包、test-maven-apijar包,因爲引入test-maven-apijar包,我們在項目中調用像在本地調用一樣。代碼如下:

      

[html] view plain copy

  1. <span style="font-size:18px;"><dependency>  

  2.         <groupId>cn.test</groupId>  

  3.         <artifactId>test-maven-api</artifactId>  

  4.         <version>0.0.1-SNAPSHOT</version>  

  5.     </dependency>  

  6.     

  7.       <dependency>  

  8.             <groupId>com.alibaba</groupId>  

  9.             <artifactId>dubbo</artifactId>  

  10.             <version>2.5.3</version>  

  11.         </dependency>  

  12.           

  13.          <dependency>  

  14.             <groupId>org.apache.zookeeper</groupId>  

  15.             <artifactId>zookeeper</artifactId>  

  16.             <version>3.4.6</version>  

  17.         </dependency>  

  18.     

  19.       <dependency>  

  20.          <groupId>com.github.sgroschupf</groupId>  

  21.          <artifactId>zkclient</artifactId>  

  22.          <version>0.1</version>  

  23. </dependency></span>  



        (2)test-maven-server-console項目的具體實現,代碼如下:

     

[java] view plain copy

  1. @Controller  

  2. public class IndexController {  

  3.       

  4.     @Autowired  

  5.     private TestRegistryService testRegistryService;  

  6.       

  7.     @RequestMapping("/hello")  

  8.     public String index(Model model){  

  9.          String name=testRegistryService.hello("zz");  

  10.          System.out.println("xx=="+name);  

  11.         return "";  

  12.     }  

  13.   

  14. }  




        (3)我們要引用的地址,代碼如下:

   

[java] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  4.     xmlns:jee="http://www.springframework.org/schema/jee"  

  5.     xmlns:tx="http://www.springframework.org/schema/tx"  

  6.     <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>  

  7.     xmlns:context="http://www.springframework.org/schema/context"  

  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  

  9.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  

  10.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  

  11.     <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>  

  12.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"  

  13.     default-lazy-init="false" >  

  14.   

  15.    <dubbo:application name="dubbo_consumer"></dubbo:application>  

  16.    <!-- 使用zookeeper註冊中心暴露服務地址 -->    

  17.    <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry>   

  18.      <!-- 要引用的服務 -->    

  19.    <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>  

  20. </beans>  



說明:

   dubbo:reference 的一些屬性的說明:

      1interface調用的服務接口

      2check 啓動時檢查提供者是否存在,true報錯,false忽略

      3registry 從指定註冊中心註冊獲取服務列表,在多個註冊中心時使用,值爲<dubbo:registry>id屬性,多個註冊中心ID用逗號分隔

      4loadbalance 負載均衡策略,可選值:random,roundrobin,leastactive,分別表示:隨機,輪循,最少活躍調用

   

(4)項目啓動,Dubbo管理頁面,能看到消費者,如圖所示:

 

(5)然後訪問消費者項目,Controller層能像調用本地一樣調用服務的具體實現,如圖所示:

  


總結:

        經過一系列的操作之後,我們感覺,的確很簡單,dubbo給我們封裝了很多操作,讓我們不需要過多考慮具體的實現細節,配置化生成咱們的應用,這樣的思想,現在在IT行業正在盛行!


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