搭建簡單的Dubbo例項
簡單的Dubbo框架搭建過程,包括Dubbo所依賴的zookeeper安裝。
一.安裝zookeeper
1.下載zookeeper的安裝包
下載地址為:https://mirrors.cnnic.cn/apache/zookeeper/
可從連結內選擇一個合適的版本下載,我所使用的版本是zookeeper-3.4.10
2.解壓下載的壓縮包,修改配置檔案
修改目錄下zookeeper-3.4.10\conf\zoo_sample.cfg檔案內容,並將檔名修改為zoo.cfg。
實際上只需要修改dataDir和dataLogDir兩個地方即可,dataDir為Zookeeper 儲存資料的目錄,dataLogDir為Zookeeper 儲存日誌檔案的目錄
以下為我的配置檔案,可以參考。
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. # 需要更改的地方 dataDir=W:/Dubbo/zookeeper/data dataLogDir=W:/Dubbo/zookeeper/log # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
3.啟動zookeeper
找到目錄\zookeeper-3.4.10\bin,雙擊zkServer.cmd,直接執行;這種方法遇到問題會閃退。
開啟cmd,進入到\zookeeper-3.4.10\bin目錄下,執行zkServer.cmd。
4.啟動zookeeper時的一些問題。
若雙擊zkServer.cmd出現閃退,可以使用第二種啟動方法,或者在編輯zkServer.cmd在文末新增pause,來查詢問題。
1.報錯,缺失zoo.cfg
這個錯是因為沒有將conf/zoo_sample.cfg改名為zoo.cfg,改名之後即可解決。
2.JAVA_HOME沒有設定
需要在環境變數的使用者變數裡新增JAVA_HOME,新增方法就不贅述了。
二.建立maven專案
1.一共分三個模組,每個模組都有獨立的pom檔案。
稍微說明一下三個模組的作用。api內為公用介面,consumer呼叫遠端服務,provider提供遠端服務。
2.在consumer和provider的pom中新增依賴
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
</dependencies>
3.在dubbo-demo-api內新增介面
public interface DemoService {
String sayHello(String name);
}
4..在dubbo-demo-provider內實現介面
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
修改配置檔案
配置檔案的內容為:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service
<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
</beans>
啟動服務,注意要先啟動zookeeper
public class Provider {
public static void main(String[] args) throws Exception {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
// System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.out.println("----------------服務已啟動,按任意鍵結束···········--------------------");
System.in.read(); // press any key to exit
}
}
5.在dubbo-demo-consumer內呼叫服務
public class Consumer {
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("world"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
修改配置檔案
下面是配置檔案內容,注意registry的地址,按照需求修改。
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>
<!-- use multicast registry center to discover service
<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
三.執行專案
先啟動zookeeper,啟動成功後啟動provider,最後啟動consumer
執行結果部分截圖如下:
我只是寫了一下我的搭建過程,如果想要更詳細的瞭解請去看大神寫的博文。
參考:http://blog.csdn.net/noaman_wgs/article/details/70214612/