Dubbo新手入门实例HelloWorld(基于Zookeeper注册中心)

最近刚接触dubbo,新手入门遇到好多麻烦,网上搜来的入门demo也是各种问题,百般周折自己终于倒腾出来了,与大家共享~

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.0modelVersion>  
  4.   
  5.     <groupId>com.xbzgroupId>  
  6.     <artifactId>dubbo-serverartifactId>  
  7.     <version>0.0.1-SNAPSHOTversion>  
  8.     <packaging>jarpackaging>  
  9.   
  10.     <name>servername>  
  11.     <url>http://maven.apache.orgurl>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  15.     properties>  
  16.   
  17.     <dependencies>  
  18.           
  19.         <dependency>  
  20.             <groupId>org.springframeworkgroupId>  
  21.             <artifactId>spring-contextartifactId>  
  22.             <version>4.1.6.RELEASEversion>  
  23.         dependency>  
  24.           
  25.   
  26.           
  27.         <dependency>  
  28.             <groupId>com.alibabagroupId>  
  29.             <artifactId>dubboartifactId>  
  30.             <version>2.5.3version>  
  31.         dependency>  
  32.           
  33.   
  34.           
  35.         <dependency>  
  36.             <groupId>org.apache.zookeepergroupId>  
  37.             <artifactId>zookeeperartifactId>  
  38.             <version>3.3.6version>  
  39.         dependency>  
  40.           
  41.   
  42.           
  43.         <dependency>  
  44.             <groupId>commons-logginggroupId>  
  45.             <artifactId>commons-loggingartifactId>  
  46.             <version>1.1.1version>  
  47.         dependency>  
  48.         <dependency>  
  49.             <groupId>log4jgroupId>  
  50.             <artifactId>log4jartifactId>  
  51.             <version>1.2.15version>  
  52.             <exclusions>  
  53.                 <exclusion>  
  54.                     <groupId>com.sun.jdmkgroupId>  
  55.                     <artifactId>jmxtoolsartifactId>  
  56.                 exclusion>  
  57.                 <exclusion>  
  58.                     <groupId>com.sun.jmxgroupId>  
  59.                     <artifactId>jmxriartifactId>  
  60.                 exclusion>  
  61.                 <exclusion>  
  62.                     <artifactId>jmsartifactId>  
  63.                     <groupId>javax.jmsgroupId>  
  64.                 exclusion>  
  65.                 <exclusion>  
  66.                     <artifactId>mailartifactId>  
  67.                     <groupId>javax.mailgroupId>  
  68.                 exclusion>  
  69.             exclusions>  
  70.         dependency>  
  71.           
  72.           
  73.           
  74.         <dependency>  
  75.             <groupId>org.jboss.nettygroupId>  
  76.             <artifactId>nettyartifactId>  
  77.             <version>3.2.0.Finalversion>  
  78.         dependency>  
  79.         <dependency>  
  80.             <groupId>com.101tecgroupId>  
  81.             <artifactId>zkclientartifactId>  
  82.             <version>0.8version>  
  83.         dependency>  
  84.           
  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.       
  7.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  8.     <dubbo:protocol name="dubbo" port="20880" />  
  9.       
  10.        
  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.0modelVersion>  
  4.   
  5.     <groupId>com.xbzgroupId>  
  6.     <artifactId>dubbo-clientartifactId>  
  7.     <version>0.0.1-SNAPSHOTversion>  
  8.     <packaging>jarpackaging>  
  9.   
  10.     <name>clientname>  
  11.     <url>http://maven.apache.orgurl>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  15.     properties>  
  16.   
  17.     <dependencies>  
  18.           
  19.         <dependency>  
  20.             <groupId>org.springframeworkgroupId>  
  21.             <artifactId>spring-contextartifactId>  
  22.             <version>4.1.6.RELEASEversion>  
  23.         dependency>  
  24.           
  25.   
  26.           
  27.         <dependency>  
  28.             <groupId>com.alibabagroupId>  
  29.             <artifactId>dubboartifactId>  
  30.             <version>2.5.3version>  
  31.         dependency>  
  32.           
  33.   
  34.           
  35.         <dependency>  
  36.             <groupId>org.apache.zookeepergroupId>  
  37.             <artifactId>zookeeperartifactId>  
  38.             <version>3.3.6version>  
  39.         dependency>  
  40.           
  41.   
  42.           
  43.         <dependency>  
  44.             <groupId>log4jgroupId>  
  45.             <artifactId>log4jartifactId>  
  46.             <version>1.2.15version>  
  47.             <exclusions>  
  48.                 <exclusion>  
  49.                     <groupId>com.sun.jdmkgroupId>  
  50.                     <artifactId>jmxtoolsartifactId>  
  51.                 exclusion>  
  52.                 <exclusion>  
  53.                     <groupId>com.sun.jmxgroupId>  
  54.                     <artifactId>jmxriartifactId>  
  55.                 exclusion>  
  56.                 <exclusion>  
  57.                     <artifactId>jmsartifactId>  
  58.                     <groupId>javax.jmsgroupId>  
  59.                 exclusion>  
  60.                 <exclusion>  
  61.                     <artifactId>mailartifactId>  
  62.                     <groupId>javax.mailgroupId>  
  63.                 exclusion>  
  64.             exclusions>  
  65.         dependency>  
  66.           
  67.   
  68.           
  69.         <dependency>  
  70.             <groupId>com.101tecgroupId>  
  71.             <artifactId>zkclientartifactId>  
  72.             <version>0.8version>  
  73.         dependency>  
  74.         <dependency>  
  75.             <groupId>com.xbzgroupId>  
  76.             <artifactId>dubbo-serverartifactId>  
  77.             <version>0.0.1-SNAPSHOTversion>  
  78.         dependency>  
  79.           
  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

你可能感兴趣的:(分布式系统)