Dubbo DEMO HelloWorld(zookeeper)

1.创建服务方项目dubbo-server,在pom.xml中构建项目依赖

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.xbz</groupId>  
  6.     <artifactId>dubbo-server</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>server</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <!-- spring begin -->  
  19.         <dependency>  
  20.             <groupId>org.springframework</groupId>  
  21.             <artifactId>spring-context</artifactId>  
  22.             <version>4.1.6.RELEASE</version>  
  23.         </dependency>  
  24.         <!-- spring end -->  
  25.   
  26.         <!-- dubbo begin -->  
  27.         <dependency>  
  28.             <groupId>com.alibaba</groupId>  
  29.             <artifactId>dubbo</artifactId>  
  30.             <version>2.5.3</version>  
  31.         </dependency>  
  32.         <!-- dubbo end -->  
  33.   
  34.         <!-- 注册中心zookeeper begin -->  
  35.         <dependency>  
  36.             <groupId>org.apache.zookeeper</groupId>  
  37.             <artifactId>zookeeper</artifactId>  
  38.             <version>3.3.6</version>  
  39.         </dependency>  
  40.         <!-- 注册中心zookeeper end -->  
  41.   
  42.         <!-- log begin -->  
  43.         <dependency>  
  44.             <groupId>commons-logging</groupId>  
  45.             <artifactId>commons-logging</artifactId>  
  46.             <version>1.1.1</version>  
  47.         </dependency>  
  48.         <dependency>  
  49.             <groupId>log4j</groupId>  
  50.             <artifactId>log4j</artifactId>  
  51.             <version>1.2.15</version>  
  52.             <exclusions>  
  53.                 <exclusion>  
  54.                     <groupId>com.sun.jdmk</groupId>  
  55.                     <artifactId>jmxtools</artifactId>  
  56.                 </exclusion>  
  57.                 <exclusion>  
  58.                     <groupId>com.sun.jmx</groupId>  
  59.                     <artifactId>jmxri</artifactId>  
  60.                 </exclusion>  
  61.                 <exclusion>  
  62.                     <artifactId>jms</artifactId>  
  63.                     <groupId>javax.jms</groupId>  
  64.                 </exclusion>  
  65.                 <exclusion>  
  66.                     <artifactId>mail</artifactId>  
  67.                     <groupId>javax.mail</groupId>  
  68.                 </exclusion>  
  69.             </exclusions>  
  70.         </dependency>  
  71.         <!-- log end -->  
  72.           
  73.         <!-- other begin -->  
  74.         <dependency>  
  75.             <groupId>org.jboss.netty</groupId>  
  76.             <artifactId>netty</artifactId>  
  77.             <version>3.2.0.Final</version>  
  78.         </dependency>  
  79.         <dependency>  
  80.             <groupId>com.101tec</groupId>  
  81.             <artifactId>zkclient</artifactId>  
  82.             <version>0.8</version>  
  83.         </dependency>  
  84.         <!-- other end -->  
  85.     </dependencies>  
  86. </project>  
2.测试接口DemoService和实现类DemoServiceImpl

[java]  view plain  copy
  1. package com.xbz.service;  
  2.   
  3. public interface DemoService {  
  4.   
  5.     String sayHello(String name);  
  6.   
  7. }  
[java]  view plain  copy
  1. package com.xbz.service.impl;  
  2.   
  3. import com.xbz.service.DemoService;  
  4.   
  5. public class DemoServiceImpl implements DemoService {  
  6.   
  7.     public String sayHello(String name) {  
  8.         System.out.println("init : " + name);  
  9.         return "hello " + name;  
  10.     }  
  11.   
  12. }  

3.applicationProvider.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     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 ">  
  5.     <dubbo:application name="dubbo-demo" />  
  6.     <!-- zookeeper注册中心 -->  
  7.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  8.     <dubbo:protocol name="dubbo" port="20880" />  
  9.       
  10.     <!-- 和本地bean一样实现服务 -->   
  11.     <bean id="demoService" class="com.xbz.service.impl.DemoServiceImpl" />  
  12.   
  13.     <!-- 向注册中心注册暴漏服务地址,注册服务 -->  
  14.     <dubbo:service interface="com.xbz.service.DemoService"  
  15.         ref="demoService" executes="10" />  
  16.   
  17. </beans>  

4.服务方主方法ServerMain

[java]  view plain  copy
  1. package main;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class ServerMain {  
  8.   
  9.     public static void main(String[] args) throws IOException {  
  10.   
  11.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });  
  12.         context.start();  
  13.   
  14.         System.out.println("输入任意按键退出 ~ ");  
  15.         System.in.read();  
  16.         context.close();  
  17.     }  
  18. }  
至此,服务方的配置就已经完成了~下面是服务方项目结构:


接下来我们创建客户端消费方dubbo-client项目
1.创建dubbo-client,在pom.xml中构建项目依赖

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.xbz</groupId>  
  6.     <artifactId>dubbo-client</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>client</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <!-- spring begin -->  
  19.         <dependency>  
  20.             <groupId>org.springframework</groupId>  
  21.             <artifactId>spring-context</artifactId>  
  22.             <version>4.1.6.RELEASE</version>  
  23.         </dependency>  
  24.         <!-- spring end -->  
  25.   
  26.         <!-- dubbo begin -->  
  27.         <dependency>  
  28.             <groupId>com.alibaba</groupId>  
  29.             <artifactId>dubbo</artifactId>  
  30.             <version>2.5.3</version>  
  31.         </dependency>  
  32.         <!-- dubbo end -->  
  33.   
  34.         <!-- 注册中心zookeeper begin -->  
  35.         <dependency>  
  36.             <groupId>org.apache.zookeeper</groupId>  
  37.             <artifactId>zookeeper</artifactId>  
  38.             <version>3.3.6</version>  
  39.         </dependency>  
  40.         <!-- 注册中心zookeeper end -->  
  41.   
  42.         <!-- log begin -->  
  43.         <dependency>  
  44.             <groupId>log4j</groupId>  
  45.             <artifactId>log4j</artifactId>  
  46.             <version>1.2.15</version>  
  47.             <exclusions>  
  48.                 <exclusion>  
  49.                     <groupId>com.sun.jdmk</groupId>  
  50.                     <artifactId>jmxtools</artifactId>  
  51.                 </exclusion>  
  52.                 <exclusion>  
  53.                     <groupId>com.sun.jmx</groupId>  
  54.                     <artifactId>jmxri</artifactId>  
  55.                 </exclusion>  
  56.                 <exclusion>  
  57.                     <artifactId>jms</artifactId>  
  58.                     <groupId>javax.jms</groupId>  
  59.                 </exclusion>  
  60.                 <exclusion>  
  61.                     <artifactId>mail</artifactId>  
  62.                     <groupId>javax.mail</groupId>  
  63.                 </exclusion>  
  64.             </exclusions>  
  65.         </dependency>  
  66.         <!-- log end -->  
  67.   
  68.         <!-- other begin -->  
  69.         <dependency>  
  70.             <groupId>com.101tec</groupId>  
  71.             <artifactId>zkclient</artifactId>  
  72.             <version>0.8</version>  
  73.         </dependency>  
  74.         <dependency>  
  75.             <groupId>com.xbz</groupId>  
  76.             <artifactId>dubbo-server</artifactId>  
  77.             <version>0.0.1-SNAPSHOT</version>  
  78.         </dependency>  
  79.         <!-- other end -->  
  80.     </dependencies>  
  81. </project>  

2.applicationConsumer.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  5. http://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  6.     <dubbo:application name="consumer-of-dubbo-demo" />  
  7.   
  8.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  9.   
  10.     <!-- 向注册中心订阅服务 -->  
  11.     <dubbo:reference id="demoService" interface="com.xbz.service.DemoService" />  
  12. </beans>   

3.客户端消费方主方法ClientMain

[java]  view plain  copy
  1. package main;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. import com.xbz.service.DemoService;  
  6.   
  7. public class ClientMain {  
  8.     public static void main(String[] args) {  
  9.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  10.                 new String[] { "applicationConsumer.xml" });  
  11.         context.start();  
  12.         DemoService service = (DemoService) context.getBean("demoService");  
  13.         System.out.println(service.sayHello("world"));  
  14.         context.close();  
  15.     }  
  16. }  

客户端消费方项目结构如下


至此,一个基本的dubbo项目就已经构建完成了.在本机上启动zookeeper注册中心(直接解压到英文路径下直接运行根路径下bin/zkServer.cmd启动)


启动服务方


启动成功,线程阻塞~

运行客户端消费方


输出hello world,调用成功 !

此时服务端控制台输出如下


demo下载:http://download.csdn.net/detail/xingbaozhen1210/9532171

zookeeper即下即用版:http://download.csdn.net/detail/xingbaozhen1210/9532177

相关文章
相关标签/搜索