1、基本環(huán)境搭建
1.1饼拍、軟件及源碼
zookeeper安裝配置:http://www.reibang.com/p/de90172ea680
node.js及vue安裝配置:http://www.reibang.com/p/7834fc55e9e8
dubbo源碼:https://github.com/apache/incubator-dubbo
dubbo管理后臺(tái)源碼:https://github.com/apache/incubator-dubbo-ops
1.2首装、環(huán)境配置
1.2.1厉膀、zookeeper配置
解壓zookeeper壓縮包颤殴,會(huì)有如下目錄:
雙擊運(yùn)行bin目錄下的zkServer.cmd啟動(dòng)腳本觅廓,啟動(dòng)成功會(huì)顯示如下信息:
若執(zhí)行腳本出現(xiàn)閃退,則啟動(dòng)異常涵但,請(qǐng)打開windows的cmd窗口哪亿,在此窗口中執(zhí)行zkServer.cmd命令,并查看異常原因贤笆。
若2181端口被占用,請(qǐng)先關(guān)閉占用此端口的應(yīng)用讨阻。
zookeeper服務(wù)端啟動(dòng)完成后芥永,啟動(dòng)bin目錄下的zkCli.cmd客戶端腳本。啟動(dòng)成功并輸入:【ls /】命令會(huì)有以下輸出:
1.2.2钝吮、dubbo源碼編譯
dubbo源碼目錄結(jié)構(gòu)如下:
利用idea等IDE工具集成的maven進(jìn)行編譯打包埋涧,并本地install;
1.2.3奇瘦、dubbo管理后臺(tái)
(1)管理后臺(tái)結(jié)構(gòu)介紹
dubbo后臺(tái)管理的目錄結(jié)構(gòu)如下:
dubbo最新版使用了前后端分離:
dubbo-admin-backend:基于spring-boot的后端業(yè)務(wù)邏輯代碼棘催;
dubbo-admin-frontend:基于vue的前臺(tái)頁面資源代碼;
(1)后端編譯配置啟動(dòng)
后端代碼目錄如下:
后端主要配置zookeeper耳标,即配置注冊(cè)中心醇坝,application.properties中配置如下:
運(yùn)行DubboAdminApplication的main函數(shù),啟動(dòng)后端服務(wù):
啟動(dòng)成功將有如下輸出:
(1)前端配置啟動(dòng)
前端代碼目錄如下:
config/index.js中有如下配置:
前臺(tái)服務(wù)器啟動(dòng)之后有如下輸出:
打開瀏覽器次坡,輸入:http://localhost:8081
2呼猪、服務(wù)提供者及調(diào)用者
項(xiàng)目目錄結(jié)構(gòu)如下:
maven中dubbo版本依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--此版本為本地編譯版本画畅,若demo無法運(yùn)行請(qǐng)?zhí)鎿Q為已發(fā)布版本-->
<!--經(jīng)測(cè)試,最新版本無法識(shí)別注冊(cè)中心-->
<!--<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0-SNAPSHOT</version>
</dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
2.1宋距、服務(wù)提供者
2.1.1轴踱、dubbo-rpc-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 -->
<dubbo:application name="privider-api-test" />
<!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 -->
<dubbo:registry id="zookeeperRegistry" protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:service id="dubboTestApi" interface="dubbo.demo.api.DubboTestApi" ref="dubboTestApiImpl" />
<!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) -->
<bean id="dubboTestApiImpl" class="dubbo.demo.api.DubboTestApiImpl" />
</beans>
使用zookeeper作為注冊(cè)中心谚赎,協(xié)議dubbo協(xié)議淫僻,暴露端口為20880;
2.1.2壶唤、服務(wù)啟動(dòng)實(shí)現(xiàn)
public class ProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/rpc/dubbo-rpc-provider.xml");
context.start();
System.in.read(); // 按任意鍵退出
}
}
2.1.3雳灵、啟動(dòng)服務(wù)
運(yùn)行main函數(shù),啟動(dòng)服務(wù)视粮,輸出如下:
管理后臺(tái)服務(wù)提供者有如下信息:
2.2细办、服務(wù)調(diào)用者
2.2.1、dubbo-rpc-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消費(fèi)方應(yīng)用名蕾殴,用于計(jì)算依賴關(guān)系笑撞,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="consumer-api-test" />
<!-- 使用zookeeper注冊(cè)中心暴露發(fā)現(xiàn)服務(wù)地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成遠(yuǎn)程服務(wù)代理钓觉,可以和本地bean一樣使用demoService -->
<dubbo:reference id="dubboTestApi" interface="dubbo.demo.api.DubboTestApi" />
</beans>
2.2.2茴肥、服務(wù)啟動(dòng)實(shí)現(xiàn)
public class ConsumerMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config/rpc/dubbo-rpc-consumer.xml");
context.start();
DubboTestApi testApi = (DubboTestApi) context.getBean("dubboTestApi"); // 獲取遠(yuǎn)程服務(wù)代理
String ret = testApi.echoTest("this is my test!");
System.out.println(ret); // 顯示調(diào)用結(jié)果
System.in.read();
}
}
2.2.3、調(diào)用服務(wù)
服務(wù)輸出:
管理后臺(tái)服務(wù)消費(fèi)者有如下信息:
demo源碼:https://github.com/zhaozhou11/dubbo-demo.git
參考文檔:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html