首页 > 其他 > 详细

dubbo client server demo

时间:2015-10-29 13:38:26      阅读:309      评论:0      收藏:0      [点我收藏+]
个客户端。

    服务端

    服务端maven父工程

    首先搭建一个maven父工程,引入dubbo和spring的依赖,dubbo可以和spring无缝集成。
  1. <properties>  
  2.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.         <v.dubbo.ext>1.3.0-SNAPSHOT</v.dubbo.ext>  
  4.         <v.spring>3.1.2.RELEASE</v.spring>  
  5.         <v.plugin.jar>2.3.1</v.plugin.jar>  
  6.     </properties>  
  7.   
  8.     <dependencies>  
  9.         <dependency>  
  10.             <groupId>com.jd.dubbo.ext</groupId>  
  11.             <artifactId>dubbo-ext-spi</artifactId>  
  12.             <version>${v.dubbo.ext}</version>  
  13.         </dependency>  
  14.         <dependency>  
  15.             <groupId>org.springframework</groupId>  
  16.             <artifactId>spring-core</artifactId>  
  17.             <version>${v.spring}</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.             <groupId>org.springframework</groupId>  
  21.             <artifactId>spring-beans</artifactId>  
  22.             <version>${v.spring}</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.springframework</groupId>  
  26.             <artifactId>spring-context</artifactId>  
  27.             <version>${v.spring}</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.springframework</groupId>  
  31.             <artifactId>spring-context-support</artifactId>  
  32.             <version>${v.spring}</version>  
  33.         </dependency>  
  34.         <dependency>  
  35.             <groupId>org.springframework</groupId>  
  36.             <artifactId>spring-tx</artifactId>  
  37.             <version>${v.spring}</version>  
  38.         </dependency>  
  39.         <dependency>  
  40.             <groupId>org.springframework</groupId>  
  41.             <artifactId>spring-orm</artifactId>  
  42.             <version>${v.spring}</version>  
  43.         </dependency>  
  44.         <dependency>  
  45.             <groupId>org.springframework</groupId>  
  46.             <artifactId>spring-web</artifactId>  
  47.             <version>${v.spring}</version>  
  48.         </dependency>  
  49.     </dependencies>  
    这些都是开发dubbo服务端必须要用的依赖包。

    服务端接口子模块

    这个模块负责向第三方应用暴露接口的定义,实现在其它的包里。third party 应用需要引入这个包,但无法看到接口的实现。
    这个模块十分简单,只需提供接口定义。
  1. package org.dubbo.server.api;  
  2.   
  3. public interface DemoService {  
  4.      public String sayHello(String name);    
  5. }  

    服务端接口实现子模块

    重新创建一个maven module,这个模块负责实现上面模块定义的接口。
    实现如下:
  1. package org.dubbo.server.service;  
  2.   
  3. import org.dubbo.server.api.DemoService;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. @Service ("demoService")  
  7. public class DemoServiceImpl implements DemoService{  
  8.      public String sayHello(String name) {    
  9.          return "Hello " + name;    
  10.   }    
  11. }  
    同时服务端应该作为一个独立的应用部署起来,处理客户端的请求。
  1. package org.dubbo.server.service;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5.   
  6. public class DubboServer {  
  7.     public static void main(String[] args) throws Exception {    
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});    
  9.         context.start();    
  10.   
  11.         System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟    
  12.     }    
  13. }  

    好,服务端跑起来了。

    服务端dubbo配置

  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.     xmlns:context="http://www.springframework.org/schema/context"  
  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.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context.xsd"  
  11.         default-autowire="byName">  
  12.           
  13.     <context:component-scan base-package="org.dubbo.server.service.**" />  
  14.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  15.     <dubbo:application name="hehe_consumer" organization="risk.im.jd.com"  
  16.         owner="lvsheng" />  
  17.   
  18.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  19.     <dubbo:registry id="registry" address="10.28.163.15:6060" />  
  20.   
  21.     <dubbo:protocol id="protocol" name="dubbo" port="25000"  
  22.         heartbeat="0" threadpool="cached" threads="512" />  
  23.   
  24.     <dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000"  
  25.         retries="5" loadbalance="roundrobin" cluster="failover" registry="registry">  
  26.     </dubbo:provider>  
  27.   
  28.     <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
  29.     <dubbo:service interface="org.dubbo.server.api.DemoService" ref="demoService"  
  30.         provider="im.riskctrl.dubbo.provider" >  
  31.     </dubbo:service>  
  32.   
  33. </beans>    

    注册中心

    dubbo注册中心我使用的是dubbo官网的注册中心demo。

    客户端

    客户端简单的搭建一个普通的maven工程就行了。pom依赖跟服务端的一致。
    客户端的调用代码如下:
  1. package com.jd.lvsheng.dubbo.client.test;  
  2.   
  3. import org.dubbo.server.api.DemoService;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. @Service ("dubboConsumer")  
  8. public class DubboConsumer {  
  9.   
  10.     public static void main(String[] args) throws Exception {  
  11.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });  
  12.         context.start();  
  13.         DemoService demoService = context.getBean("demoService", DemoService.class);  
  14.         System.out.println(demoService.sayHello("aaa"));  
  15.         System.in.read();  
  16.     }  
  17. }  
    客户端的spring配置如下:
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  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.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context.xsd"  
  11.         default-autowire="byName">  
  12.           
  13.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  14.     <dubbo:application name="dubbo_consumer" organization="risk.im.com"  
  15.         owner="lvsheng" />  
  16.   
  17.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  18.     <dubbo:registry id="registry" address="10.28.163.15:6060" />  
  19.   
  20.     <dubbo:protocol id="protocol" name="dubbo" port="25001"  
  21.         heartbeat="0" threadpool="cached" threads="512" />  
  22.   
  23.     <dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000"  
  24.         retries="5" loadbalance="roundrobin" cluster="failover" registry="registry">  
  25.     </dubbo:provider>  
  26.   
  27.     <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
  28.     <dubbo:reference interface="org.dubbo.server.api.DemoService" id="demoService" registry="registry">  
  29.     </dubbo:reference>  
  30.   
  31. </beans>    
    整个工程是可以运行的。

dubbo client server demo

原文:http://my.oschina.net/u/2357525/blog/523456

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!