1、工做原理圖html
這是dubbo官網的關於dubbo工做原理,其中最核心的應該是Registry註冊中心,Monitor,Consumer消費者和Provider服務提供者四個部分,註冊中心關係這消費者和提供者的在zookeeper上的註冊狀態,Monitor控制着消費者,提供者啓用和禁用。java
1、服務提供者git
dubbo的git源碼有提供一個provider案例工程,dubbo-demo-provider。上面有一段spring配置文件,dubbo-demo-provider.xml,其內容以下:spring
<?xml version="1.0" encoding="UTF-8"?> <!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="demo-provider"/> <!-- 使用multicast廣播註冊中心暴露服務地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> --> <dubbo:registry address="zookeeper://192.168.43.33:2181"/> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 和本地bean同樣實現服務 --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> </beans>
其中,1)dubbo:application標籤做用是給提供者備註一個名稱,這個名稱一旦被取了,其餘提供者就不能再用這個名稱。express
2)dubbo:registry標籤的做用,就是講應用註冊到註冊中心,原例子是在用多廣播方式進行註冊,能夠改成註冊到咱們安裝的zookeeper上,個人zookeeper服務地址是192.168.43.33,端口默認2181。apache
3)dubbo:protocol標籤是標註這個本次服務的協議,暴露的端口,如今是採用dubbo協議,20880端口。api
4)dubbo:service標籤是聲明提供者服務用的。app
具體實現服務代碼比較簡單,是一個helloworld程序,再也不作介紹。less
2、服務消費者eclipse
dubbo的git源碼有提供一個consumer案例工程,dubbo-demo-consumer。上面有一段spring配置文件,dubbo-demo-consumer.xml,其內容以下:
<?xml version="1.0" encoding="UTF-8"?> <!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 --> <dubbo:application name="demo-consumer"/> <!-- 使用multicast廣播註冊中心暴露發現服務地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> --> <dubbo:registry address="zookeeper://192.168.43.33:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService --> <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/> </beans>
上面的配置文件跟provider類似,比較特別就是他是消費者,沒有服務提供一說,而是用dubbo:reference來標明本身要調用提供者的那個服務,響應名稱又跟提供者對應。
3、運行結果
程序的調用順序是要先把provider啓動起來,而後運行consumer工程的,會發現eclipse的console中有以下日誌:
說明服務已經成功調用
4、注意環節
消費者要跑起來,還要在該工程的pom.xml中引入一個
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.parent.version}</version> </dependency>
這段引用是對應dubbo開源項目中的dubbo-demo-api這個工程,主要是實現一個與provide對應服務相同的一個接口,給消費者調用,否則程序會報錯,當你在eclipse用鼠標,點擊
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
中的interface的內容,就會跳到相應的代碼,相應代碼以下:
package com.alibaba.dubbo.demo; public interface DemoService { String sayHello(String name); }