說明
Dubbo官方網(wǎng)站下面的文字來自官網(wǎng)
DUBBO是一個分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠程服務(wù)調(diào)用方案,是阿里巴巴SOA服務(wù)化治理方案的核心框架,每天為2,000+個服務(wù)提供3,000,000,000+次訪問量支持,并被廣泛應用于阿里巴巴集團的各成員站點。
Dubbo是一個分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠程服務(wù)調(diào)用方案,以及SOA服務(wù)治理方案。從本質(zhì)上說是一個分布式遠程服務(wù)調(diào)用框架。核心內(nèi)容如下:
- 遠程通訊: 提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
- 集群容錯: 提供基于接口方法的透明遠程過程調(diào)用,包括多協(xié)議支持,以及軟負載均衡,失敗容錯,地址路由,動態(tài)配置等集群支持。
- 自動發(fā)現(xiàn): 基于注冊中心目錄服務(wù),使服務(wù)消費方能動態(tài)的查找服務(wù)提供方,使地址透明,使服務(wù)提供方可以平滑增加或減少機器。
Dubbo采用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴展進行加載。
架構(gòu)
dubbo的整體架構(gòu)圖:

dubbo整體架構(gòu)
節(jié)點角色說明:
Provider:暴露服務(wù)的服務(wù)提供方。
Consumer:調(diào)用遠程服務(wù)的服務(wù)消費方。
Registry:服務(wù)注冊與發(fā)現(xiàn)的注冊中心。
Monitor:統(tǒng)計服務(wù)的調(diào)用次調(diào)和調(diào)用時間的監(jiān)控中心。
Container:服務(wù)運行容器。
調(diào)用關(guān)系說明:
- 服務(wù)容器負責啟動,加載,運行服務(wù)提供者。
- 服務(wù)提供者在啟動時,向注冊中心注冊自己提供的服務(wù)。
- 服務(wù)消費者在啟動時,向注冊中心訂閱自己所需的服務(wù)。
- 注冊中心返回服務(wù)提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費者。
- 服務(wù)消費者,從提供者地址列表中,基于軟負載均衡算法,選一臺提供者進行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。
- 服務(wù)消費者和提供者,在內(nèi)存中累計調(diào)用次數(shù)和調(diào)用時間,定時每分鐘發(fā)送一次統(tǒng)計數(shù)據(jù)到監(jiān)控中心。
spring + zookeeper + dubbo整合
這里使用的是dubbo2.5.3版本
- zookeeper作為注冊中心:首先啟動zookeeper
- 使用dubbo-admin-2.5.3.war作為dubbo的管理工具:在dubbo.properties文件中設(shè)置zookeeper的地址和訪問密碼
dubbo.registry.address=zookeeper://192.168.139.42:2181
dubbo.admin.root.password=abc
dubbo.admin.guest.password=abc
- 服務(wù)端spring整合
Maven依賴
<!-- 這里是接口定義的內(nèi)容:例子中的 interface TestRegistryService -->
<dependency>
<groupId>cn.test</groupId>
<artifactId>test-maven-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
服務(wù)代碼
public interface TestRegistryService {
public String hello(String name);
}
@Service("testRegistryService")
public class TestRegistryServiceImpl implements TestRegistryService {
public String hello(String name) {
return "hello"+name;
}
}
spring配置中暴漏接口
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
<span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<!-- 提供方應用名稱信息,這個相當于起一個名字,我們dubbo管理頁面比較清晰是哪個應用暴露出來的 -->
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注冊中心暴露服務(wù)地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>
<!-- 要暴露的服務(wù)接口 -->
<dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />
</beans>
- 消費端使用
spring配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
<span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<dubbo:application name="dubbo_consumer"></dubbo:application>
<!-- 使用zookeeper注冊中心暴露服務(wù)地址 -->
<dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry>
<!-- 要引用的服務(wù) -->
<dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>
</beans>
使用服務(wù)的方法
@Controller
public class IndexController {
@Autowired
private TestRegistryService testRegistryService;
@RequestMapping("/hello")
public String index(Model model){
String name=testRegistryService.hello("zz");
System.out.println("xx=="+name);
return "";
}
}