最近項目用到Dubbo框架,臨時抱佛腳分享一下共探討

1. dubbo是什麼?

dubbo是一個分佈式服務框架,致力於提供高性能和透明化的rpc遠程服務調用方案,以及soa服務治理方案。簡單的說,dubbo就是個服務框架,如果沒有分佈式的需求,其實是不需要用的,只有在分佈式的時候,纔有dubbo這樣的分佈式服務框架的需求,並且本質上是個服務調用的東東,說白了就是個遠程服務調用的分佈式框架(告別web service模式中的wsdl,以服務者與消費者的方式在dubbo上註冊
其核心部分包含:
1. 遠程通訊: 提供對多種基於長連接的nio框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
2. 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
3. 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。

2. dubbo能做什麼?

1.透明化的遠程方法調用,就像調用本地方法一樣調用遠程方法,只需簡單配置,沒有任何api侵入。      
2.軟負載均衡及容錯機制,可在內網替代f5等硬件負載均衡器,降低成本,減少單點。
3. 服務自動註冊與發現,不再需要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的ip地址,並且能夠平滑添加或刪除服務提供者。

dubbo採用全spring配置方式,透明化接入應用,對應用沒有任何api侵入,只需用spring加載dubbo的配置即可,dubbo基於spring的schema擴展進行加載。

3. dubbo的架構

dubbo架構圖如下所示:

 

 

節點角色說明:

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

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

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

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

       container: 服務運行容器。

調用關係說明:

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

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

2. 服務消費者在

此文來自: 馬開東博客 轉載請註明出處 網址: http://www.makaidong.com

啓動時,向註冊中心訂閱自己所需的服務。

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

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

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

4. dubbo使用方法。

dubbo採用全spring配置方式,透明化接入應用,對應用沒有任何api侵入,只需用spring加載dubbo的配置即可,dubbo基於spring的schema擴展進行加載。如果不想使用spring配置,而希望通過api的方式進行調用(不推薦)

下面我們就來看看spring配置方式的寫法:

服務提供者:

1. 下載zookeeper註冊中心,下載地址:http://www.apache.org/dyn/closer.cgi/zookeeper/  下載後解壓即可,進入d:\apach-zookeeper-3.4.5\bin,

雙擊zkserver.cmd啓動註冊中心服務。

2. 定義服務接口: (該接口需單獨打包,在服務提供方和消費方共享)

 

[html]
 



  1. package com.unj.dubbotest.provider;  
  2.   
  3. import java.util.list;  
  4.   
  5. public interface demoservice {  
  6.   
  7.     string sayhello(string name);  
  8.   
  9.     public list getusers();  
  10.   
  11. }  


在服務提供方實現接口:(對服務消費方隱藏實現)

 

[html] 
 



  1. package com.unj.dubbotest.provider;  
  2.   
  3. import java.util.arraylist;  
  4. import java.util.linkedlist;  
  5. import java.util.list;  
  6.   
  7.   
  8. public class demoserviceimpl implements demoservice{  
  9.       
  10.      public string sayhello(string name) {  
  11.             return “hello ” + name;  
  12.      }  
  13.      public list getusers() {  
  14.          list list = new arraylist();  
  15.          user u1 = new user();  
  16.          u1.setname(“jack”);  
  17.          u1.setage(20);  
  18.          u1.setsex(“男”);  
  19.            
  20.          user u2 = new user();  
  21.          u2.setname(“tom”);  
  22.          u2.setage(21);  
  23.          u2.setsex(“女”);  
  24.            
  25.          user u3 = new user();  
  26.          u3.setname(“rose”);  
  27.          u3.setage(19);  
  28.          u3.setsex(“女”);  
  29.            
  30.          list.add(u1);  
  31.          list.add(u2);  
  32.          list.add(u3);  
  33.          return list;  
  34.      }  
  35. }  

用spring配置聲明暴露服務:

[html] 
 



  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:dubbo=http://code.alibabatech.com/schema/dubbo  
  5.     xsi:schemalocation=”http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd &nbsp;
  7.         http://code.alibabatech.com/schema/dubbo &nbsp;
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd &nbsp;
  9.         ”>  
  10.    
  11.     <!– 具體的實現bean –>  
  12.     <bean id=“demoservice” class=“com.unj.dubbotest.provider.demoserviceimpl” />  
  13.       
  14.     <!– 提供方應用信息,用於計算依賴關係 –>  
  15.     <dubbo:application name=“xixi_provider”  />  
  16.    
  17.     <!– 使用multicast廣播註冊中心暴露服務地址   
  18.     <dubbo:registry address=“multicast://224.5.6.7:1234” />>  
  19.     
  20.     <!– 使用zookeeper註冊中心暴露服務地址 –>  
  21.     <dubbo:registry address=“zookeeper://127.0.0.1:2181” />   
  22.     
  23.     <!– 用dubbo協議在20880端口暴露服務 –>  
  24.     <dubbo:protocol name=“dubbo” port=“20880” />  
  25.    
  26.     <!– 聲明需要暴露的服務接口 –>  
  27.     <dubbo:service interface=“com.unj.dubbotest.provider.demoservice” ref=“demoservice” />  
  28.       
  29. </beans>  
[html] 
 



  1. package com.unj.dubbotest.provider;  
  2.   
  3. import org.springframework.context.support.classpathxmlapplicationcontext;  
  4.   
  5. public class provider {  
  6.    
  7.     public static void main(string[] args) throws exception {  
  8.         classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(new string[] {“applicationcontext.xml”});  
  9.         context.start();  
  10.    
  11.         system.in.read(); // 爲保證服務一直開着,利用輸入流的阻塞來模擬  
  12.     }  
  13.    
  14. }  


服務消費者:

1.通過spring配置引用遠程服務:
[html]
 



  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 xmlns:dubbo=http://code.alibabatech.com/schema/dubbo  
  4.     xsi:schemalocation=”http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd &nbsp;
  6.         http://code.alibabatech.com/schema/dubbo &nbsp;
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd &nbsp;
  8.         ”>  
  9.   
  10.     <!– 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 –>  
  11.     <dubbo:application name=“hehe_consumer” />  
  12.   
  13.     <!– 使用zookeeper註冊中心暴露服務地址 –>  
  14.     <!– <dubbo:registry address=”multicast://224.5.6.7:1234” /> –>  
  15.     <dubbo:registry address=“zookeeper://127.0.0.1:2181” />  
  16.   
  17.     <!– 生成遠程服務代理,可以像使用本地bean一樣使用demoservice –>  
  18.     <dubbo:reference id=“demoservice”  
  19.         interface=“com.unj.dubbotest.provider.demoservice” />  
  20.   
  21. </beans>  


2.加載spring配置,並調用遠程服務:
[html] 
 



  1. package com.alibaba.dubbo.demo.pp;  
  2.   
  3. import java.util.list;  
  4.   
  5. import org.springframework.context.support.classpathxmlapplicationcontext;  
  6.   
  7. import com.unj.dubbotest.provider.demoservice;  
  8.   
  9. public class consumer {  
  10.   
  11.     public static void main(string[] args) throws exception {  
  12.         classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(  
  13.                 new string[] { “applicationcontext.xml” });  
  14.         context.start();  
  15.   
  16.         demoservice demoservice = (demoservice) context.getbean(“demoservice”); //  
  17.         string hello = demoservice.sayhello(“tom”); // ִ  
  18.         system.out.println(hello); //   
  19.   
  20.         //   
  21.         list list = demoservice.getusers();  
  22.         if (list != null && list.size() > 0) {  
  23.             for (int i = 0; i < list.size(); i++) {  
  24.                 system.out.println(list.get(i));  
  25.             }  
  26.         }  
  27.         // system.out.println(demoservice.hehe());  
  28.         system.in.read();  
  29.     }  
  30.   
  31. }  

調用結果爲:


dubbo管理頁面:


 

應用頁面:

 

提供者頁面:

 

消費者頁面:

 

服務頁面:

案例代碼下載:http://download.csdn.net/detail/yiyu1/7116319

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