Dubbo中文文檔地址
https://dubbo.gitbooks.io/dubbo-user-book/content/
Dubbo背景

image.png
- 單一應用架構
當網(wǎng)站流量很小時,只需一個應用,將所有功能都部署在一起,以減少部署節(jié)點和成本。
此時,用于簡化增刪改查工作量的 數(shù)據(jù)訪問框架(ORM) 是關鍵。 - 垂直應用架構
當訪問量逐漸增大,單一應用增加機器帶來的加速度越來越小,將應用拆成互不相干的幾個應用,以提升效 率。此時,用于加速前端頁面開發(fā)的 Web框架(MVC) 是關鍵。 - 分布式應用架構
當垂直應用越來越多,應用之間交互不可避免,將核心業(yè)務抽取出來,作為獨立的服務,逐漸形成穩(wěn)定的服務 中心,使前端應用能更快速的響應多變的市場需求。此時,用于提高業(yè)務復用及整合的 分布式服務框架(RPC)是關鍵。 - 流動計算架構
當服務越來越多,容量的評估,小服務資源的浪費等問題逐漸顯現(xiàn),此時需增加一個調度中心基于訪問壓力實時管理集群容量,提高集群利用率。此時,用于提高機器利用率的 資源調度和治理中心(SOA) 是關鍵。
Dubbo
Dubbo快速搭建(結合SpringBoot):http://start.dubbo.io/,快速生成消費者和提供者項目
- <dubbo:service/> 服務配置,用于暴露一個服務,定義服務的元信息,一個服務可以用多個協(xié)議暴露,一個服務也可以注冊到多個注冊中心。
- <dubbo:reference/> 引用配置,用于創(chuàng)建一個遠程服務代理,一個引用可以指向多個注冊中心。
- <dubbo:protocol/> 協(xié)議配置,用于配置提供服務的協(xié)議信息,協(xié)議由提供方指定,消費方被動接受。
- <dubbo:application/> 應用配置,用于配置當前應用信息,不管該應用是提供者還是消費者。
- <dubbo:module/> 模塊配置,用于配置當前模塊信息,可選。
- <dubbo:registry/> 注冊中心配置,用于配置連接注冊中心相關信息。
- <dubbo:monitor/> 監(jiān)控中心配置,用于配置連接監(jiān)控中心相關信息,可選。
- <dubbo:provider/> 提供方的缺省值,當ProtocolConfig和ServiceConfig某屬性沒有配置時,采用此缺省值,可選。
- <dubbo:consumer/> 消費方缺省配置,當ReferenceConfig某屬性沒有配置時,采用此缺省值,可選。
- <dubbo:method/> 方法配置,用于ServiceConfig和ReferenceConfig指定方法級的配置信息。
- <dubbo:argument/> 用于指定方法參數(shù)配置。
Dubbo的Pom文件配置
<!-- dubbo 需要的jar start -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- dubbo 需要的jar end -->
Dubbo基本使用
服務提供者和消費者都需要引入公用接口相關的jar包
服務提供者
<!-- 提供方應用信息,用于計算依賴關系 -->
<dubbo:application name="gw-service-user"/>
<!-- 使用zookeeper注冊中心暴露服務地址
<dubbo:registry protocol="zookeeper" address="192.168.3.71:2181" />
-->
<!-- 使用multicast廣播注冊中心暴露服務地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- 用dubbo協(xié)議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 和本地bean一樣實現(xiàn)服務 -->
<bean id="pmsUserFacade" class="wusc.edu.facade.user.service.impl.PmsUserFacadeImpl"/>
<!-- 用戶服務接口 -->
<dubbo:service interface="wusc.edu.facade.user.service.PmsUserFacade" ref="pmsUserFacade"/>
服務消費者
<!-- 消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="edu-web-boss"/>
<!-- 使用zookeeper注冊中心暴露服務地址 -->
<!-- 注冊中心地址-->
<!--<dubbo:registry protocol="zookeeper" address="192.168.3.71:2181" />-->
<!-- 使用multicast廣播注冊中心暴露服務地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- 用戶服務接口 -->
<dubbo:reference interface="wusc.edu.facade.user.service.PmsUserFacade" id="pmsUserFacade" check="false"/>
配置重試次數(shù),最好只用于讀的重試,寫操作可能會引起多次寫入 下面三個任意一個配置就行 默認retries="0"
- <dubbo:service retries="2" />
- <dubbo:reference retries="2" />
- <dubbo:reference>
<dubbo:method name="findFoo" retries="2" />
</dubbo:reference>
注冊中心

image.png
流程:
1.服務提供者啟動時向/dubbo/com.foo.BarService/providers目錄下寫入URL
2.服務消費者啟動時訂閱/dubbo/com.foo.BarService/providers目錄下的URL向/dubbo/com.foo.BarService/consumers目錄下寫入自己的URL
3.監(jiān)控中心啟動時訂閱/dubbo/com.foo.BarService目錄下的所有提供者和消費者URL
Dubbo負載均衡
- Random LoadBalance
隨機,按權重設置隨機概率。
在一個截面上碰撞的概率高,但調用量越大分布越均勻,而且按概率使用權重后也比較均勻,有利于動態(tài)調整提供者權重。- RoundRobin LoadBalance
輪循,按公約后的權重設置輪循比率。
存在慢的提供者累積請求問題,比如:第二臺機器很慢,但沒掛,當請求調到第二臺時就卡在那,久而久之,所有請求都卡在調到第二臺上。- LeastActive LoadBalance
最少活躍調用數(shù),相同活躍數(shù)的隨機,活躍數(shù)指調用前后計數(shù)差。
使慢的提供者收到更少請求,因為越慢的提供者的調用前后計數(shù)差會越大。- ConsistentHash LoadBalance
一致性Hash,相同參數(shù)的請求總是發(fā)到同一提供者。
當某一臺提供者掛時,原本發(fā)往該提供者的請求,基于虛擬節(jié)點,平攤到其它提供者,不會引起劇烈變動。算法參見:http://en.wikipedia.org/wiki/Consistent_hashing。
缺省只對第一個參數(shù)Hash,如果要修改,請配置<dubbo:parameter key="hash.arguments" value="0,1" />
缺省用160份虛擬節(jié)點,如果要修改,請配置<dubbo:parameter key="hash.nodes" value="320" />
<dubbo:service interface="..." loadbalance="roundrobin" />
<dubbo:reference interface="..." loadbalance="roundrobin" />
<dubbo:service interface="...">
<dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>
<dubbo:reference interface="...">
<dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>
Dubbo多協(xié)議配置
不同服務在性能上適用不同協(xié)議進行傳輸,比如大數(shù)據(jù)用短連接協(xié)議,小數(shù)據(jù)大并發(fā)用長連接協(xié)議。
<!-- 多協(xié)議配置 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="1099" />
<!-- 使用dubbo協(xié)議暴露服務 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
<!-- 使用rmi協(xié)議暴露服務 -->
<dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />
<!-- 使用多個協(xié)議暴露服務 -->
<dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" />
Dubbo多注冊中心
<!-- 多注冊中心配置 -->
<dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
<!-- 向多個注冊中心注冊 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
一個接口多種實現(xiàn)時
<dubbo:service group="feedback" interface="com.xxx.IndexService" />
<dubbo:service group="member" interface="com.xxx.IndexService" />
<dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" />
<dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndexService" />
<!--任意組:(2.2.0以上版本支持,總是只調一個可用組的實現(xiàn))-->
<dubbo:reference id="barService" interface="com.foo.BarService" group="*" />
多版本
當一個接口實現(xiàn),出現(xiàn)不兼容升級時,可以用版本號過渡,版本號不同的服務相互間不引用。
在低壓力時間段,先升級一半提供者為新版本
再將所有消費者升級為新版本
然后將剩下的一半提供者升級為新版本
<dubbo:service interface="com.foo.BarService" version="1.0.0" />
<dubbo:service interface="com.foo.BarService" version="2.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />