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與Zookeeper、SpringMVC整合使用

  

  第一步:在Linux上安裝Zookeeper

 

       Zookeeper作為Dubbo服務的注冊中心,Dubbo原先基於數據庫的注冊中心,沒采用Zookeeper,Zookeeper一個分布式的服務框架,是樹型的目錄服務的數據存儲,能做到集群管理數據 ,這里能很好的作為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 在啟動時會找這個文件作為默認配置文件。下面詳細介紹一下,這個配置文件中各個配置項的意義。

 

<span style="font-size:18px;"># The number of milliseconds of each tick  
tickTime=2000  
# The number of ticks that the initial   
# synchronization phase can take  
initLimit=10  
# The number of ticks that can pass between   
# sending a request and getting an acknowledgement  
syncLimit=5  
# the directory where the snapshot is stored.  
# do not use /tmp for storage, /tmp here is just   
# example sakes.  
dataDir=C:\\zookeeper-3.4.5\\data  
dataLogDir=C:\\zookeeper-3.4.5\\log  
# the port at which the clients will connect  
clientPort=2181  
#  
# Be sure to read the maintenance section of the   
# administrator guide before turning on autopurge.  
#  
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
#  
# The number of snapshots to retain in dataDir  
#autopurge.snapRetainCount=3  
# Purge task interval in hours  
# Set to "0" to disable auto purge feature  
#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包,在windows的tomcat部署,先把dubbo-admin-2.4.1放在tomcat的webapps/ROOT下,然后進行解壓

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

       

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

 

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

      

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

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

         

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

public interface TestRegistryService {  
   public String hello(String name);  
}  

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

<span style="font-size:18px;"><dependency>  
        <groupId>cn.test</groupId>  
        <artifactId>test-maven-api</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </dependency>  
    
      <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
        </dependency>  
          
         <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.4.6</version>  
        </dependency>  
    
      <dependency>  
         <groupId>com.github.sgroschupf</groupId>  
         <artifactId>zkclient</artifactId>  
         <version>0.1</version>  
</dependency></span>  

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

@Service("testRegistryService")  
public class TestRegistryServiceImpl implements TestRegistryService {  
    public String hello(String name) {    
        return "hello"+name;  
    }  
}  

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

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    <span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
    <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
    default-lazy-init="false" >  
   <!-- 提供方應用名稱信息,這個相當於起一個名字,我們dubbo管理頁面比較清晰是哪個應用暴露出來的 -->  
   <dubbo:application name="dubbo_provider"></dubbo:application>  
   <!-- 使用zookeeper注冊中心暴露服務地址 -->    
   <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>  
  <!-- 要暴露的服務接口 -->    
  <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />        
</beans>  

說明:

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

      1)register是否向此注冊中心注冊服務,如果設為false,將只訂閱,不注冊。

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

      3)subscribe是否向此注冊中心訂閱服務,如果設為false,將只注冊,不訂閱。

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

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

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

     1)interface服務接口的路徑

     2)ref引用對應的實現類的Bean的ID

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

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

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

  

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

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

<span style="font-size:18px;"><dependency>  
        <groupId>cn.test</groupId>  
        <artifactId>test-maven-api</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </dependency>  
    
      <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
        </dependency>  
          
         <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.4.6</version>  
        </dependency>  
    
      <dependency>  
         <groupId>com.github.sgroschupf</groupId>  
         <artifactId>zkclient</artifactId>  
         <version>0.1</version>  
</dependency></span>  

 

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

@Controller  
public class IndexController {  
      
    @Autowired  
    private TestRegistryService testRegistryService;  
      
    @RequestMapping("/hello")  
    public String index(Model model){  
         String name=testRegistryService.hello("zz");  
         System.out.println("xx=="+name);  
        return "";  
    }  
  
}  

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

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
    <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
    default-lazy-init="false" >  
  
   <dubbo:application name="dubbo_consumer"></dubbo:application>  
   <!-- 使用zookeeper注冊中心暴露服務地址 -->    
   <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry>   
     <!-- 要引用的服務 -->    
   <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>  
</beans>  

 

說明:

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

      1)interface調用的服務接口

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

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

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

   

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

 

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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM