简单易懂dubbo入门实例

一、创建Maven多模块项目

 

项目结构如下

 

 

模块介绍:

 

dubbo-api            ----API接口

dubbo-consumer ----消费者

dubbo-provider    ----生产者

 

dubbo-api  Service 提供的接口,provider 实现了 api 接口,并对外开放http端口,consumer依赖 api 工程,但是不会直接依赖provider工程。 这种可以让消费者仅仅依赖api工程,而不需要对provider工程的依赖,也是松耦合的经典。

 

(1)创建root项目

 

首先创建root项目,把一些通用的依赖全部放置到这里,步骤如下:

 

 

 

 

创建成功之后如下

 

 

把上面的src目录全部删除掉,只留下pom.xml

 

 

 

然后修改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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.fendo</groupId>  
  4.   <artifactId>Dubbo_demos</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <packaging>pom</packaging>  
  7.   <name>dubbo</name>  
  8.   <description>dubbo示例</description>  
  9.     
  10.     
  11.   <properties>  
  12.     <maven.compiler.source>1.8</maven.compiler.source>  
  13.     <maven.compiler.target>1.8</maven.compiler.target>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  16.       
  17.     <!-- spring版本号 -->  
  18.     <spring.version>4.2.5.RELEASE</spring.version>  
  19.       
  20.     <!-- log4j日志包版本号 -->  
  21.     <slf4j.version>1.7.18</slf4j.version>  
  22.     <log4j.version>1.2.17</log4j.version>  
  23.       
  24.    </properties>  
  25.      
  26.      
  27.      
  28.    <dependencyManagement>  
  29.       <dependencies>  
  30.           
  31.         <!-- 添加junit4依赖 -->  
  32.         <dependency>  
  33.           <groupId>junit</groupId>  
  34.           <artifactId>junit</artifactId>  
  35.           <version>4.11</version>  
  36.         </dependency>  
  37.   
  38.         <dependency>  
  39.             <groupId>org.springframework</groupId>  
  40.             <artifactId>spring-core</artifactId>  
  41.             <version>${spring.version}</version>  
  42.         </dependency>  
  43.   
  44.         <dependency>  
  45.             <groupId>org.springframework</groupId>  
  46.             <artifactId>spring-aop</artifactId>  
  47.             <version>${spring.version}</version>  
  48.         </dependency>  
  49.         <dependency>  
  50.             <groupId>org.springframework</groupId>  
  51.             <artifactId>spring-context-support</artifactId>  
  52.             <version>${spring.version}</version>  
  53.         </dependency>  
  54.   
  55.      
  56.         <!-- 添加日志相关jar包 -->  
  57.         <dependency>  
  58.           <groupId>log4j</groupId>  
  59.           <artifactId>log4j</artifactId>  
  60.           <version>${log4j.version}</version>  
  61.         </dependency>  
  62.         <dependency>  
  63.           <groupId>org.slf4j</groupId>  
  64.           <artifactId>slf4j-api</artifactId>  
  65.           <version>${slf4j.version}</version>  
  66.         </dependency>  
  67.         <dependency>  
  68.           <groupId>org.slf4j</groupId>  
  69.           <artifactId>slf4j-log4j12</artifactId>  
  70.           <version>${slf4j.version}</version>  
  71.         </dependency>  
  72.       
  73.          <dependency>  
  74.              <groupId>commons-logging</groupId>  
  75.              <artifactId>commons-logging</artifactId>  
  76.              <version>1.2</version>  
  77.          </dependency>  
  78.       
  79.         <!-- dubbo -->  
  80.         <dependency>  
  81.           <groupId>com.alibaba</groupId>  
  82.           <artifactId>dubbo</artifactId>  
  83.           <version>2.5.3</version>  
  84.           <exclusions>  
  85.             <exclusion>  
  86.               <groupId>org.springframework</groupId>  
  87.               <artifactId>spring</artifactId>  
  88.             </exclusion>  
  89.           </exclusions>  
  90.         </dependency>  
  91.       
  92.         <!-- Zookeeper zkclient-->  
  93.         <dependency>  
  94.             <groupId>com.github.sgroschupf</groupId>  
  95.             <artifactId>zkclient</artifactId>  
  96.             <version>0.1</version>  
  97.         </dependency>  
  98.       
  99.    </dependencies>  
  100.   </dependencyManagement>  
  101.     
  102.     
  103. </project>  
 

 

 

(2)创建api项目

 

新建一个Module

 

 

输入module名,选择类型为java项目

 

 

 

 

点击"Finish",创建完成如下

 

 

他会自动在root目录下把dubbo-api这么module加入进来。而dubbo-api会自动继承root

 

 

 

 

(3)创建provider(生产者)

 
 



 

 

 

 

 

 

 

点击"Finish"生成如下

 

 

修改POM.XML为如下:

 

 

[html] view plain copy
 
  1. <?xml version="1.0"?>  
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  4.   <modelVersion>4.0.0</modelVersion>  
  5.     
  6.   <parent>  
  7.     <artifactId>Dubbo_demos</artifactId>  
  8.     <groupId>com.fendo</groupId>  
  9.     <version>0.0.1-SNAPSHOT</version>  
  10.   </parent>  
  11.     
  12.   <groupId>com.fendo</groupId>  
  13.   <artifactId>dubbo-provider</artifactId>  
  14.   <version>0.0.1-SNAPSHOT</version>  
  15.   <name>dubbo-provider</name>  
  16.   <url>http://maven.apache.org</url>  
  17.     
  18.   <properties>  
  19.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  20.   </properties>  
  21.     
  22.     
  23.   <dependencies>  
  24.     
  25.         <dependency>  
  26.             <groupId>com.fendo</groupId>  
  27.             <artifactId>dubbo-api</artifactId>  
  28.             <version>0.0.1-SNAPSHOT</version>  
  29.         </dependency>  
  30.           
  31.     
  32.         <dependency>  
  33.           <groupId>junit</groupId>  
  34.           <artifactId>junit</artifactId>  
  35.         </dependency>  
  36.   
  37.         <dependency>  
  38.             <groupId>org.springframework</groupId>  
  39.             <artifactId>spring-core</artifactId>  
  40.         </dependency>  
  41.   
  42.         <dependency>  
  43.             <groupId>org.springframework</groupId>  
  44.             <artifactId>spring-aop</artifactId>  
  45.         </dependency>  
  46.           
  47.         <dependency>  
  48.             <groupId>org.springframework</groupId>  
  49.             <artifactId>spring-context-support</artifactId>  
  50.         </dependency>  
  51.           
  52.           
  53.         <!-- 添加日志相关jar包 -->  
  54.         <dependency>  
  55.           <groupId>log4j</groupId>  
  56.           <artifactId>log4j</artifactId>  
  57.         </dependency>  
  58.         <dependency>  
  59.           <groupId>org.slf4j</groupId>  
  60.           <artifactId>slf4j-api</artifactId>  
  61.         </dependency>  
  62.         <dependency>  
  63.           <groupId>org.slf4j</groupId>  
  64.           <artifactId>slf4j-log4j12</artifactId>  
  65.         </dependency>  
  66.       
  67.          <!-- dubbo -->  
  68.         <dependency>  
  69.           <groupId>com.alibaba</groupId>  
  70.           <artifactId>dubbo</artifactId>  
  71.           <exclusions>  
  72.             <exclusion>  
  73.               <groupId>org.springframework</groupId>  
  74.               <artifactId>spring</artifactId>  
  75.             </exclusion>  
  76.           </exclusions>  
  77.         </dependency>  
  78.       
  79.         <!-- Zookeeper zkclient-->  
  80.         <dependency>  
  81.             <groupId>com.github.sgroschupf</groupId>  
  82.             <artifactId>zkclient</artifactId>  
  83.         </dependency>  
  84.   </dependencies>  
  85.     
  86. </project>  



 



(4)创建consumer(消费者)

 

 

 

 

 

 

创建成如下

 

 

 

修改POM.XML为如下:

 

 

[html] view plain copy
 
  1. <?xml version="1.0"?>  
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  4.   <modelVersion>4.0.0</modelVersion>  
  5.     
  6.     
  7.   <parent>  
  8.     <artifactId>Dubbo_demos</artifactId>  
  9.     <groupId>com.fendo</groupId>  
  10.     <version>0.0.1-SNAPSHOT</version>  
  11.   </parent>  
  12.     
  13.   <groupId>com.fendo</groupId>  
  14.   <artifactId>dubbo-consumer</artifactId>  
  15.   <version>0.0.1-SNAPSHOT</version>  
  16.   <name>dubbo-consumer</name>  
  17.   <url>http://maven.apache.org</url>  
  18.     
  19.     
  20.   <properties>  
  21.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  22.   </properties>  
  23.     
  24.     
  25.   <dependencies>  
  26.     
  27.     <dependency>  
  28.       <groupId>junit</groupId>  
  29.       <artifactId>junit</artifactId>  
  30.     </dependency>  
  31.       
  32.      <dependency>  
  33.         <groupId>com.fendo</groupId>  
  34.         <artifactId>dubbo-api</artifactId>  
  35.         <version>0.0.1-SNAPSHOT</version>  
  36.      </dependency>  
  37.       
  38.     <dependency>  
  39.         <groupId>org.springframework</groupId>  
  40.         <artifactId>spring-core</artifactId>  
  41.     </dependency>  
  42.   
  43.     <dependency>  
  44.         <groupId>org.springframework</groupId>  
  45.         <artifactId>spring-aop</artifactId>  
  46.     </dependency>  
  47.       
  48.     <dependency>  
  49.         <groupId>org.springframework</groupId>  
  50.         <artifactId>spring-context-support</artifactId>  
  51.     </dependency>  
  52.   
  53.     <!-- dubbo -->  
  54.     <dependency>  
  55.       <groupId>com.alibaba</groupId>  
  56.       <artifactId>dubbo</artifactId>  
  57.       <exclusions>  
  58.         <exclusion>  
  59.           <groupId>org.springframework</groupId>  
  60.           <artifactId>spring</artifactId>  
  61.         </exclusion>  
  62.       </exclusions>  
  63.     </dependency>  
  64.      
  65.     <!-- Zookeeper zkclient-->  
  66.     <dependency>  
  67.         <groupId>com.github.sgroschupf</groupId>  
  68.         <artifactId>zkclient</artifactId>  
  69.     </dependency>  
  70.       
  71.   </dependencies>  
  72. </project>  



 

创建完成后,整体结构如下

 

 

这种结构看起来不是很美观,把它修改成如下显示,一个项目下包含多个子模块

 

 

 

二、定义接口API、bo对象、公共类及方法,这些是要提供方和调用方都需要的。

 

在dubbo-api模块中创建一个接口对外开发

 

 

 

 

三、定义Dubbo服务service,实现dubbo-api中的api接口,书写业务逻辑及DB操作。通过spring暴露服务。

 

创建DemoService实现类DemoServiceImpl

 

 

[html] view plain copy
 
  1. public class DemoServiceImpl implements DemoService {  
  2.   
  3.     public List<String> getPermissions(Long id) {  
  4.         List<Stringdemo = new ArrayList<String>();  
  5.         demo.add(String.format("Permission_%d", id - 1));  
  6.         demo.add(String.format("Permission_%d", id));  
  7.         demo.add(String.format("Permission_%d", id + 1));  
  8.         return demo;  
  9.     }  
  10.   
  11.      
  12. }  


以及消费者启动类Provider

 

 

[html] view plain copy
 
  1. public class Provider {  
  2.   
  3.     public static void main(String[] args) throws IOException{  
  4.           
  5.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");  
  6.         System.out.println(context.getDisplayName() + ": here");  
  7.         context.start();  
  8.         System.out.println("服务已经启动...");  
  9.         System.in.read();  
  10.     }  
  11. }  


 

 

 

然后创建resources目录

 

 

在下新建两个xml文件

 

 

provider.xml是dubbo配置文件

 

[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"  
  4.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.        http://code.alibabatech.com/schema/dubbo  
  8.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  9.          
  10.          
  11.     <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->  
  12.     <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>  
  13.     <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->  
  14.     <dubbo:registry address="zookeeper://localhost:2181"/>  
  15.     <!-- 用dubbo协议在20880端口暴露服务 -->  
  16.     <dubbo:protocol name="dubbo" port="20880" />  
  17.     <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->  
  18.     <dubbo:service interface="com.fendo.dubbo.DemoService" ref="demoService" protocol="dubbo" />  
  19.     <!--具体实现该接口的 bean-->  
  20.     <bean id="demoService" class="org.dubbo.provider.impl.DemoServiceImpl"/>  
  21. </beans>  

 

 

如果发现eclipse提示错误

 

 

解决方案,就是下载一个dubbo.xsd文件然后

 

windows->preferrence->xml->xmlcatalog 



add->catalog entry  ->file system 选择刚刚下载的文件路径



修改key值和配置文件的http://code.alibabatech.com/schema/dubbo/dubbo.xsd 相同,点击OK之后就不会报错了。。

 

dubbo.xsd文件地址:http://download.csdn.net/detail/u011781521/9914946

 


四、定义调用端也叫消费端,实现对应的控制层。通过spring获取服务。

 

和上面差不多

 

创建消费者运行类:

 

 

[html] view plain copy
 
  1. public class Consumer   
  2. {  
  3.     public static void main(String[] args) {  
  4.         //测试常规服务  
  5.         ClassPathXmlApplicationContext context =  
  6.                 new ClassPathXmlApplicationContext("consumer.xml");  
  7.         context.start();  
  8.         System.out.println("consumer start");  
  9.         DemoService demoService = (DemoService) context.getBean(DemoService.class);  
  10.         System.out.println("consumer");  
  11.         System.out.println(demoService.getPermissions(1L));  
  12.     }  
  13. }  


consumer.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"  
  4.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  7.       
  8.     <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>  
  9.     <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->  
  10.     <dubbo:registry address="zookeeper://localhost:2181"/>  
  11.     <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->  
  12.     <dubbo:reference id="permissionService" interface="org.dubbo.api.DemoService"/>  
  13.       
  14. </beans>  



 

五、使用zookeeper注册中心。

 

运行zookeeper/bin目录下的zkServer.cmd

 

 

启动zookeeper

 

 

六、启动Dubbo的service端,即provider。

 

启动zookeeper之后,然后运行dubbo-provider中的Provider类

 

 

启动成功之后,如上所示,然后在dubbo-admin控制台会多出一个生产者

 

 

显示还没有消费者。。


七、启动portal端,即所谓的consumer。

 

然后启动dubbo-consumer中的Consumer类

 

 

会打印如上信息,表示消费成功!!!

posted @ 2018-05-17 15:44  月眸  阅读(1107)  评论(0编辑  收藏  举报