Dubbo是什麼?
Dubbo[]是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。
其核心部分包含:
- 遠程通訊: 提供對多種基於長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
- 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
- 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。
Dubbo能做什麼?
- 透明化的遠程方法調用,就像調用本地方法一樣調用遠程方法,只需簡單配置,沒有任何API侵入。
- 軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,降低成本,減少單點。
- 服務自動註冊與發現,不再需要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的IP地址,並且能夠平滑添加或刪除服務提供者。
----------------------------------------------------------------------------------------------------------------------------------------------------
****************************************************************************************************************************
----------------------------------------------------------------------------------------------------------------------------------------------------
以上內容來源Dubbo官網,另外深入學習Dubbo必須訪問的站點就是它的官網.我這裏只是寫一個簡單的使用教程,有一個快速入門的基礎.
Dubbo官方資源(因爲'牆'原因,部分資源無響應.圖鑑使用github下載源碼):
- Duboo的下載:https://github.com/alibaba/dubbo
- Dubbo的使用:http://dubbo.io/User+Guide-zh.htm
- Dubbo的擴展以及內部原理:http://dubbo.io/Developer+Guide-zh.htm
我這裏講解的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配置爲-1
然後,消費者依賴生產者.
消費者的配置:
<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>
項目依賴根據自己需要調用的接口加入.