1:项目的架构,本项目使用的maven,分为三个模块。

api 为接口 , server 为服务端   consumer 为调用端

2:api的模块结构

该模块主要是定义接口和实体。没什么具体介绍的。

3:server的模块结构

impl:api接口的实现类 。DubboServer:服务启动 。dubbo.xml配置详情。log 打印日志信息

pom文件代码

    <dependencies>
<dependency>
<groupId>com.mav</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <!-- 引入spring的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency> <!-- 引入zk -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency> <!-- 引入dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

dubbo.xml代码

<?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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 扫描包 -->
<context:component-scan base-package="com.enjoy.service"/> <!-- 应用程序名称 -->
<dubbo:application name="dubboServerApp" /> <!-- 注册中心 -->
<dubbo:registry address="zookeeper://192.168.30.128:2181" /> <!-- 注册协议 -->
<dubbo:protocol port="20881" name="rmi" />
<dubbo:protocol port="20882" name="dubbo" /> <!-- 暴露服务 interface 接口全路径 ref 实现类 -->
<dubbo:service interface="com.enjoy.service.OrderService" ref="orderService" protocol="dubbo" /> </beans>

impl实现类代码

@Service("orderService")
public class OrderServiceImpl implements OrderService { public Integer bugShop(Integer money) {
System.out.println("money的数值:"+money);
return money+1;
}
}

启动类代码

public class DubboServer {

    public static void main(String[] args) {
//启动dubbo
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:dubbo.xml");
context.start(); System.out.println("-----dubbo开启-----"); // 保证服务一直开着
synchronized (DubboServer.class) {
try {
DubboServer.class.wait();
} catch (Throwable e) {
}
}
}
}

3:调用端的结构 目录结构和服务端差不多一样,只不过少了实现类,调用者直接调用。pom文件都是一样的。

调用者主要是dubbo.xml不一样

<?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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 应用程序名称 -->
<dubbo:application name="dubboConsumerApp" /> <!-- 注册中心 -->
<dubbo:registry address="zookeeper://192.168.30.128:2181" /> <!-- 注册协议 -->
<dubbo:protocol port="20881" name="rmi" />
<dubbo:protocol port="20882" name="dubbo" /> <!-- 暴露服务 interface 接口全路径 id spring的ID -->
<dubbo:reference id="orderService" interface="com.enjoy.service.OrderService" protocol="dubbo" /> </beans>

测试类

public class DubboConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:dubbo.xml"); context.start(); OrderService orderService = (OrderService)context.getBean("orderService");
Integer order = orderService.bugShop(1);
System.out.println(order); }
}

以上就是dubbo spring的使用。

最新文章

  1. clr 元数据
  2. C++编译期多态与运行期多态
  3. wpf window set window的owner
  4. win dos命令行设置ip和dns
  5. MapReduce的模式、算法和用例
  6. PHP开发框架Laravel优点,Laravel5.3中文文档
  7. Lumia刷机Win10 Mobile 10.0.10166惊魂记
  8. Redbean:入门(二) - Find
  9. Using Boost Libraries in Windows Store and Phone Applications
  10. Centos6.3 jekyll环境安装
  11. Windows Phone 获取网络类型(GSM/CDMA/WIFI/Ethernet)
  12. Qt自定义事件的实现(军队真正干活,但要增加监军,大平台通知事件,事件内容自定义)
  13. SuperSocket源码解析之消息处理
  14. 《Linux命令行与shell脚本编程大全》第十六章 控制脚本
  15. linux 添加ftp用户与登录配置详解
  16. MySQL数据库Inception工具学习与测试 笔记
  17. 闲聊javascript继承和原型
  18. MongpDB 学习手册 - 索引
  19. servlet保存会话数据---利用隐藏域
  20. 闭包----你所不知道的JavaScript系列(4)

热门文章

  1. 【431】Prim 算法 &amp; Kruskal 算法
  2. 一些Python中的二维数组的操作方法
  3. 【物联网】arduino wifi
  4. 迅速生成项目-react-static
  5. vue react 路由history模式刷新404问题解决方案
  6. 【ARTS】01_41_左耳听风-201900819~201900825
  7. AWS 基础设施即代码(五)
  8. LeetCode 258. 各位相加(Add Digits)
  9. [WinForm] - &quot;更新 DataSet 应用程序集对象失败,Visual Studio 自动重启&quot; 之解决
  10. nginx location 路由的几个配置纪要