Dubbo[]是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。java
其核心部分包含:git
----------------------------------------------------------------------------------------------------------------------------------------------------github
****************************************************************************************************************************spring
----------------------------------------------------------------------------------------------------------------------------------------------------apache
以上內容來源Dubbo官網,另外深刻學習Dubbo必須訪問的站點就是它的官網.我這裏只是寫一個簡單的使用教程,有一個快速入門的基礎.session
Dubbo官方資源(由於'牆'緣由,部分資源無響應.圖鑑使用github下載源碼):app
我這裏講解的dubbo2.4.11版本,其餘各版本大致相同,具體差別可能也有.須要去查看相關的document:負載均衡
生產者配置:框架
<description>dubbo配置示例之生產者配置</description> <!-- 生產者的項目配置 --> <dubbo:application id="demoApplication" name="demoApplication" owner="pan" organization="cn" logger="slf4j" compiler="javassist"/> <!--協議,能夠配置多個--> <dubbo:protocol id="demoServerProtocol" name="dubbo" port="-1" dispatcher="message" threadpool="limited" threads="20"> </dubbo:protocol> <!--註冊中心--> <dubbo:registry id="demoServerRegistry" protocol="zookeeper" address="zk地址" client="zkclient" group="族的位置" session="60000" register="false" subscribe="false" check="false" file="demo_erver-registry-cache.properties"> </dubbo:registry> <!--生產者配置--> <dubbo:provider id="demoServerProvider" application="demoServerApplication" registry="demoServerRegistry" protocol="demoServerProtocol" cluster="failover" loadbalance="leastactive" serialization="hessian2" retries="0" timeout="15000" proxy="javassist" delay="-1"> </dubbo:provider> <!--生產者的接口提供遠程調用的bean ref是對應的bean名--> <dubbo:service interface="com.pan.demo.service.Service1" ref="demoService1"/> <dubbo:service interface="com.pan.demo.service.Service2" ref="demoService2"/> <dubbo:service interface="com.pan.demo.service.Service3" ref="demoService3"/>
(1)同一IP運行起來幾個Provider,listen 端口號從默認端口號向大綁定:將port配置爲-1maven
而後,消費者依賴生產者.
消費者的配置:
<description>dubbo示例之消費者配置</description> <!--註冊中心--> <dubbo:registry id="demoClientRegistry" protocol="zookeeper" client="zkclient" address="zk註冊中心地址" group="組地址" subscribe="true" check="false" file="demo_client_dubbo_registry_cache.properties"> </dubbo:registry> <!--消費者配置--> <dubbo:consumer id="demoServiceConsumer" registry="demoClientRegistry" init="false" check="false" timeout="15000" retries="0"> </dubbo:consumer> <!-- 消費者獲取遠程對象注入本地接口配置的bean --> <dubbo:reference id="demoClientService1" interface="com.pan.demo.Service1" consumer="demoServiceConsumer"/> <dubbo:reference id="demoClientService2" interface="com.pan.demo.Service2" consumer="demoServiceConsumer"/> <dubbo:reference id="demoClientService3" interface="com.pan.demo.Service3" consumer="demoServiceConsumer"/>
總結,使用dubbo遠程調用的前提:
1.安裝配置好zookeeper或者其餘服務監聽框架.
2.引入jar包.
3.生產者與消費者都須要引入zookeeper,zkclient,dubbojar,spring,log4j包.
4.消費者依賴生產者或者他們共同依賴同一個接口.
4.其餘一些異常處理...例如check文件共用了之類的.
好了,如今能夠遠程調用了.
@Resource("demoClientService1")//能夠不指定名稱,前提是bean配好. private DemoService1 service ; public static void main(String[] args){ sysoout(service.foo("word")) ; }
OK,使用已經能夠了.
具體配置的含義,請參照官方文檔.這裏寫出來太多了.
以後會寫一些閱讀源碼的心得,以及metaq的使用.
maven的基本依賴一份:
<!-- 基本配置 --> <properties> <spring.version>4.3.3.RELEASE</spring.version> <zookeeper.version>3.4.9</zookeeper.version> <zkclient.version>0.9</zkclient.version> <dubbo.version>2.4.11</dubbo.version> <junit.version>4.12</junit.version> </properties> <!-- 基本依賴 --> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies>
項目依賴根據本身須要調用的接口加入.