dubbo學習(zz)

dubbo學習

博客分類:  
 

Dubbo是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點:javascript

 

那麼,Dubbo是什麼?

Dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。java

其核心部分包含:web

  • 遠程通信: 提供對多種基於長鏈接的NIO框架抽象封裝,包括多種線程模型,序列化,以及「請求-響應」模式的信息交換方式。
  • 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
  • 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方能夠平滑增長或減小機器。

Dubbo能作什麼?

  • 透明化的遠程方法調用,就像調用本地方法同樣調用遠程方法,只需簡單配置,沒有任何API侵入。
  • 軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,下降成本,減小單點。
  • 服務自動註冊與發現,再也不須要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的IP地址,而且可以平滑添加或刪除服務提供者。

Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置便可,Dubbo基於Spring的Schema擴展進行加載。spring

 

實例服務器

 

服務提供方:HuoDongapp

 

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

 

Java代碼     收藏代碼
  1. public interface DemoService {  
  2.     public void sayHello();  
  3. }  

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

 

 

Java代碼     收藏代碼
  1. public class DemoServiceImpl implements DemoService{  
  2.     @Override  
  3.     public void sayHello() {  
  4.         System.out.println("hello zy!");  
  5.     }  
  6. }  

 

 

用Spring配置聲明暴露服務:分佈式

 

applicationProvider.xml:ide

 

 

Xml代碼     收藏代碼
  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  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         ">       
  10.     <dubbo:application name="hello-world-app" />       
  11.     <dubbo:registry  protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" />  
  12.     <dubbo:protocol name="dubbo" port="20880" />        
  13.     <dubbo:service interface="org.huodong.service.DemoService"  
  14.         ref="demoService" />       <!-- 和本地bean同樣實現服務 -->  
  15.     <bean id="demoService" class="org.huodong.service.DemoServiceImpl" />  
  16. </beans>  

 

   ps:這裏是把服務器方和提供方都註冊到了zookeeper上統一管理。上面的IP爲安裝了zookeeper的服務器地址。若是不想用zookeeper管理的話,能夠改成

    <dubbo:registry address="multicast://224.5.6.7:1234" />

 

  服務消費者方 Fashion:

  applicationConsumer.xml:

 

Xml代碼     收藏代碼
  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  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         ">       
  10.     <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->  
  11.     <dubbo:application name="consumer-of-helloworld-app" />       <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->  
  12.     <dubbo:registry  protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" />         <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->  
  13.     <dubbo:reference id="demoService" interface="org.huodong.service.DemoService" />  
  14. </beans>  

 

調用遠程服務(ChatAction.java):

Java代碼     收藏代碼
  1. public class ChatAction implements Controller {  
  2.     private DemoService demoService;  
  3.     public ModelAndView handleRequest(HttpServletRequest req,  
  4.             HttpServletResponse res) throws Exception {  
  5.         demoService.sayHello(); //調用提供方的方法  
  6.         return null;  
  7.     }  
  8.     public DemoService getDemoService() {  
  9.         return demoService;  
  10.     }  
  11.     public void setDemoService(DemoService demoService) {  
  12.         this.demoService = demoService;  
  13.     }  
  14. }  

 

    注:須要的jar包,消費方和服務方都須要

           而後把服務方的DemoService打成一個jar包,這裏只定義了一個方法,若是定義了好多個service或者service的實現中須要用到輔助類或javabean的話,那麼都要打包進去。

 

ps:在公司的zookeeper中經過頁面http://172.17.0.121:8080/governance/services能夠找到全部註冊的服務,而後能夠在這裏設置每一個服務所使用的提供者(IP地址等信息),一個服務能夠設置好多提供者(好比一個是服務器的,一個是本地的),可是隻能一個是啓用狀態,其他都是禁用狀態。

相關文章
相關標籤/搜索