dubbo的第一個入門的例子
最近剛剛接觸 Dubbo這個 框架,遇到了挺多的麻煩,網上搜索來的入門demo也是有挺多問題,百般周折終於弄出來了一個可以使用的小demo,與大家分享。
Zookeeper的介紹和安裝:
本Demo中的Dubbo註冊中心採用的是Zookeeper,為什麼採用Zookeeper呢?
Zookeeper是一個分散式的服務框架,是樹形的目錄服務的資料儲存,能做到叢集管理資料,這裡能很好的作為Dubbo服務的
註冊中心。
Dubbo能與Zookeeper做到叢集部署,當提供者出現斷電等異常停機的時候。Zookeeper註冊中心能自動的刪除提供者的資訊,
當提供者重啟時,能自動恢復註冊資料,以及訂閱請求。
安裝完成後,進入到 bin 目錄中,並且啟動,zkServer.cmd,這個指令碼中會啟動一個java程序:
(注:需要先啟動zookeeper之後,後續dubbo demo程式碼執行才能使用 zookeeper註冊中心的功能)
2.開始建立我們的maven專案:
先建立一個dubbo-server的專案,專案的目錄結構如下圖所示:
3.pom.xml檔案如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xbz</groupId> <artifactId>dubbo-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- spring begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- spring end --> <!-- dubbo begin --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- dubbo end --> <!-- 註冊中心zookeeper begin --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.6</version> </dependency> <!-- 註冊中心zookeeper end --> <!-- log begin --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <artifactId>jms</artifactId> <groupId>javax.jms</groupId> </exclusion> <exclusion> <artifactId>mail</artifactId> <groupId>javax.mail</groupId> </exclusion> </exclusions> </dependency> <!-- log end --> <!-- other begin --> <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <version>3.2.0.Final</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.8</version> </dependency> <!-- other end --> </dependencies> </project>
4.測試介面DemoService和實現類DemoServiceImpl
package com.xbz.service.impl; import com.xbz.service.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { System.out.println("init : " + name); return "hello " + name; } }
package com.xbz.service;
public interface DemoService {
String sayHello(String name);
}
5.applicationProvider.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" />
<!-- zookeeper註冊中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<!-- 和本地bean一樣實現服務 -->
<bean id="demoService" class="com.xbz.service.impl.DemoServiceImpl" />
<!-- 向註冊中心註冊暴漏服務地址,註冊服務 -->
<dubbo:service interface="com.xbz.service.DemoService"
ref="demoService" executes="10" />
</beans>
6.服務方主方法ServerMain
package main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
context.start();
System.out.println("輸入任意按鍵退出 ~ ");
System.in.read();
context.close();
}
}
至此,服務方就已經寫成功了,接下來開始寫客戶端那一方,
在寫客戶端的那一方之前,首先,我們應該先把當前的這個專案放置到maven本地倉庫中去,避免後面客戶端在寫的時候找不到介面從而報錯。
方法是這樣的:
右鍵 dubbo-server這個專案,run as --> run configuration —>Maven Build
二、接下來開始寫dubbo-client這個專案:
pom.xml檔案如下(注意這個pom.xml檔案在後面引入了 dubbo-server ):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xbz</groupId>
<artifactId>dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end -->
<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->
<!-- 註冊中心zookeeper begin -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 註冊中心zookeeper end -->
<!-- log begin -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->
<!-- other begin -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>com.xbz</groupId>
<artifactId>dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- other end -->
</dependencies>
</project>
2.消費端applicationConsumer.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" />
<!-- zookeeper註冊中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<!--提供方資訊-->
<dubbo:application name="dubbo-client" owner="mic"/>
<!-- 引用zookeeper上註冊的遠端服務 -->
<dubbo:reference id="demoService"
interface="com.xbz.service.DemoService"
protocol="dubbo"/>
</beans>
3.客戶端消費主方法clientMain
package main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xbz.service.DemoService;
public class ClientMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
DemoService service = (DemoService) context.getBean("demoService");
System.out.println(service.sayHello("world"));
context.close();
}
}
client的專案結構如下:
於是開始執行兩邊的主程式,啟動服務方
執行客戶端:
這時客戶端輸出的結果:
伺服器端輸出的結果:
進入dubbo-admin介面進行服務的管理。(首先我們應該先下載 dubbo-admin的包,可以在guthub上下載)
,然後將dubbo-admin這個包放入到tomcat中的 web-app目錄下:隨後啟動Tomcat。
並且訪問dubbo的管理選單:(登入時候的使用者名稱和密碼初始化都是 root)