因爲DUBBO是一個高性能的RPC框架,用於進行遠程服務的註冊消費。以一個簡單案例,來闡述DUBBO框架具體應該如何使用。
一、進行DUBBO框架的安裝與部署,請參閱:https://segmentfault.com/a/11...
二、項目的依賴配置
由於使用的是Maven項目管理工具,因此要構建Maven的依賴配置。開發工具我這裏使用的是Ideal,固然也能夠使用Eclipse;
具體配置以下:web
<dependencies> <!--Spring框架的核心依賴包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <!--Apache Commons的通用工具包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!--複雜對象的映射的工具包--> <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> </dependency> <!--spring mvc依賴包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!--Dubbo工具包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.3</version> <!--<exclusions>--> <!--<exclusion>--> <!--<groupId>org.jboss.netty</groupId>--> <!--<artifactId>netty</artifactId>--> <!--</exclusion>--> <!--</exclusions>--> </dependency> <!--Zookeeper工具包--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.12</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <!--zookeeper客戶端的工具包--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!--curator框架包,主要是爲了讓Java更爲簡單方便的操做Zookeeper服務器--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> </dependency> <!--日誌處理工具包--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </dependency> <!--Json處理工具包--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <!--單元測試包-> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies>
三、編寫服務開放註冊接口
四、編寫服務提供者spring
public class ProducerMain { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ctx = null; try { ctx = new ClassPathXmlApplicationContext("spring-dubbo-provider.xml"); ctx.start(); System.in.read(); }catch (Exception ex){ System.out.println(ex.getCause().getMessage()); }finally { if(ctx != null){ ctx.close(); } } } }
編輯配置文件:spring-dubbo-provider.xmlapache
<?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="hello-world-app-provider"></dubbo:application> <dubbo:registry address="zookeeper://192.168.237.131:2181?backup=192.168.237.131:2182,192.168.237.131:2183"/> <dubbo:monitor protocol="registry"/> <dubbo:protocol name="dubbo" port="20881"/> <dubbo:service interface="接口的包路徑.SayHello" ref="sayHelloImpl"/> <bean id="sayHelloImpl" class="服務提供者的包路徑.SayHelloImpl"/> </beans>
五、編寫服務消費者segmentfault
配置spring-dubbo-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="hello-world-app-consumer"></dubbo:application> <!--<dubbo:registry address="multicast://224.5.6.7:2181"/>--> <dubbo:registry address="zookeeper://192.168.237.131:2181?backup=192.168.237.131:2182,192.168.237.131:2183"/> <dubbo:protocol name="dubbo" port="20881"/> <dubbo:reference id="sayHello" interface="cn.com.dlut.lgzs.dubbo.api.SayHello"/> </beans>
六、服務的啓動與調試:
(1)服務提供者進行服務註冊:api
public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ctx = null; try { ctx = new ClassPathXmlApplicationContext("spring-dubbo-provider.xml"); ctx.start(); System.in.read(); }catch (Exception ex){ System.out.println(ex.getCause().getMessage()); }finally { if(ctx != null){ ctx.close(); } } }
(2)服務消費者進行服務消費:服務器
public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ctx = null; try { ctx = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml"); ctx.start(); SayHello sayHello = (SayHello) ctx.getBean("sayHello"); System.out.println(sayHello.sayHello("你好,我是消費者Jack,我將進行第一次消費!")); System.in.read(); }catch (Exception ex){ System.out.println(ex.getCause().getMessage()); }finally { if(ctx != null) { ctx.close(); } } }
注:因爲DUBBO服務使用依賴於Spring的容器框架,DUBBO的服務註冊與消費都必須創建在容器啓動之後,System.in.read();是爲了保證容器處於啓動狀態,這樣可以保證和註冊中心進行會話。mvc