分散式學習系列【dubbo入門實踐】 dubbo架構 組成部分:provider,consumer,registry,monitor; provider,consumer註冊,訂閱類似於消息隊列的註冊訂閱 一、環境安裝 1、dubbo admin 管理控制台安裝(Windows環境) #下載dubb ...
分散式學習系列【dubbo入門實踐】
dubbo架構
組成部分:provider,consumer,registry,monitor; provider,consumer註冊,訂閱類似於消息隊列的註冊訂閱
一、環境安裝
1、dubbo admin 管理控制台安裝(Windows環境)
#下載dubbo-admin-2.5.3.war,部署到tomcat下,根據需要編輯WEB-INF/dubbo.properties文件:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
#dubbo admin 部署問題:版本2.5.3啟動報錯:Bean property 'URIType' is not writable or has an invalid
原因:使用了JDK1.8;
解決方法:將環境變數JAVA_HOME設置為JDK1.7;
2、zookeeper 安裝
dubbo對註冊中心實現了抽象,實現的註冊中心有:ZooKeeper,Redis,Multicast;這裡採用zookeeper進行演示。
下載zookeeper解壓,在zookeeper->conf目錄下複製一份zoo_sample.cfg為zoo.cfg,修改配置
添加配置:(預設不會自動創建,需要手動添加)
dataDir=D:\\zookeeper\\data
dataLogDir=D:\\zookeeper\\log
下載後windows下直接進入doc,進入zookeeper->bin目錄運行zkServer.cmd命令;
可以用netstat -ano|findstr "2181"檢查是否啟動成功
TCP 0.0.0.0:2181 0.0.0.0:0 LISTENING 5716 TCP 127.0.0.1:2181 127.0.0.1:53785 ESTABLISHED 5716 TCP 127.0.0.1:53785 127.0.0.1:2181 ESTABLISHED 9752 TCP [::]:2181 [::]:0 LISTENING 5716
也可用JPS查看:
8608 SmartGit 8992 Jps 5716 QuorumPeerMain 9752 Bootstrap
3、啟動tomcat訪問:localhost:8080
二、實踐步驟
#創建服務介面
public interface DemoService { String sendMessage(String message); }
#創建介面實現
public class DemoServiceImpl implements DemoService { @Override public String sendMessage(String message){ System.out.println("this demoService get message:"+message); return "provirder return message success"; } }
#創建提供方spring配置文件provider.xml
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 --> <dubbo:application name="dubbo-demo-test" /> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --> <dubbo:reference id="demoService" interface="com.demo.DemoService" /> </beans>
#創建提供方啟動程式:DemoProvider
public class DemoProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.out.println("provider started ..."); System.in.read(); } }
#創建消費方spring配置文件consumer.xml
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 --> <dubbo:application name="dubbo-demo-test" /> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --> <dubbo:reference id="demoService" interface="com.demo.DemoService" /> </beans>
#創建消費方方啟動程式:DemoConsumer
public class DemoConsumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); String returnMessage = demoService.sendMessage("consumer send message: hello my first dubbo service"); System.out.println("provider return message:"+returnMessage); } }
#添加項目maven依賴:
分別加入spring IOC,zkClient,dubbo依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
#啟動提供方服務,消費方進行調用
運行啟動提供方DemoProvider,啟動後進入待服務狀態
觀察dubbo admin發現已經剛纔的啟動服務在zookeepr成功註冊
這時消費方可以進行調用,運行DemoConsumer
此時服務端收到請求:
調用方收到響應:
附上示例代碼: http://files.cnblogs.com/files/yuxuan/dubbo-demo.rar