Dubbo服务端/客户端demo

               

    项目组采用分布式服务,线上有几十个应用,RPC调用完全依靠Dubbo。平时开发一直都是用其他人搭好的dubbo环境,最近自己抽空独立的搭建dubbo小demo,一个服务端,一个客户端。

    一 服务端

    服务端maven父工程

    首先搭建一个maven父工程,引入dubbo和spring的依赖,dubbo可以和spring无缝集成。
<properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <v.dubbo.ext>1.3.0-SNAPSHOT</v.dubbo.ext>  <v.spring>3.1.2.RELEASE</v.spring>  <v.plugin.jar>2.3.1</v.plugin.jar> </properties> <dependencies>  <dependency>   <groupId>com.jd.dubbo.ext</groupId>   <artifactId>dubbo-ext-spi</artifactId>   <version>${v.dubbo.ext}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-core</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-beans</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context-support</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-tx</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-orm</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-web</artifactId>   <version>${v.spring}</version>  </dependency> </dependencies>
    这些都是开发dubbo服务端必须要用的依赖包。

    服务端接口子模块

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

    服务端接口实现子模块

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

    好,服务端跑起来了。

    服务端dubbo配置

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

    二 注册中心

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

    三 客户端

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

   四 完整工程

    服务端代码请移步  https://github.com/bruce256/dubbo-server
    客户端代码请移步  https://github.com/bruce256/dubbo.test.client




           

猜你喜欢

转载自blog.csdn.net/qq_44925357/article/details/89353513