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

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

2:api的模块结构

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

3:server的模块结构

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

pom文件代码

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.mav</groupId>
  4. <artifactId>dubbo_api</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. </dependency>
  7.  
  8. <!-- 引入spring的jar -->
  9. <dependency>
  10. <groupId>org.springframework</groupId>
  11. <artifactId>spring-core</artifactId>
  12. <version>4.0.2.RELEASE</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-context</artifactId>
  17. <version>4.0.2.RELEASE</version>
  18. </dependency>
  19.  
  20. <!-- 引入zk -->
  21. <dependency>
  22. <groupId>com.101tec</groupId>
  23. <artifactId>zkclient</artifactId>
  24. <version>0.3</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.zookeeper</groupId>
  28. <artifactId>zookeeper</artifactId>
  29. <version>3.4.5</version>
  30. </dependency>
  31.  
  32. <!-- 引入dubbo -->
  33. <dependency>
  34. <groupId>com.alibaba</groupId>
  35. <artifactId>dubbo</artifactId>
  36. <version>2.5.7</version>
  37. <scope>compile</scope>
  38. <exclusions>
  39. <exclusion>
  40. <artifactId>spring</artifactId>
  41. <groupId>org.springframework</groupId>
  42. </exclusion>
  43. </exclusions>
  44. </dependency>
  45. </dependencies>

dubbo.xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  11. http://code.alibabatech.com/schema/dubbo
  12. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  13.  
  14. <!-- 扫描包 -->
  15. <context:component-scan base-package="com.enjoy.service"/>
  16.  
  17. <!-- 应用程序名称 -->
  18. <dubbo:application name="dubboServerApp" />
  19.  
  20. <!-- 注册中心 -->
  21. <dubbo:registry address="zookeeper://192.168.30.128:2181" />
  22.  
  23. <!-- 注册协议 -->
  24. <dubbo:protocol port="20881" name="rmi" />
  25. <dubbo:protocol port="20882" name="dubbo" />
  26.  
  27. <!-- 暴露服务 interface 接口全路径 ref 实现类 -->
  28. <dubbo:service interface="com.enjoy.service.OrderService" ref="orderService" protocol="dubbo" />
  29.  
  30. </beans>

impl实现类代码

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

启动类代码

  1. public class DubboServer {
  2.  
  3. public static void main(String[] args) {
  4. //启动dubbo
  5. ClassPathXmlApplicationContext context =
  6. new ClassPathXmlApplicationContext("classpath:dubbo.xml");
  7. context.start();
  8.  
  9. System.out.println("-----dubbo开启-----");
  10.  
  11. // 保证服务一直开着
  12. synchronized (DubboServer.class) {
  13. try {
  14. DubboServer.class.wait();
  15. } catch (Throwable e) {
  16. }
  17. }
  18. }
  19. }

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  11. http://code.alibabatech.com/schema/dubbo
  12. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  13.  
  14. <!-- 应用程序名称 -->
  15. <dubbo:application name="dubboConsumerApp" />
  16.  
  17. <!-- 注册中心 -->
  18. <dubbo:registry address="zookeeper://192.168.30.128:2181" />
  19.  
  20. <!-- 注册协议 -->
  21. <dubbo:protocol port="20881" name="rmi" />
  22. <dubbo:protocol port="20882" name="dubbo" />
  23.  
  24. <!-- 暴露服务 interface 接口全路径 id spring的ID -->
  25. <dubbo:reference id="orderService" interface="com.enjoy.service.OrderService" protocol="dubbo" />
  26.  
  27. </beans>

测试类

  1. public class DubboConsumer {
  2. public static void main(String[] args) {
  3. ClassPathXmlApplicationContext context =
  4. new ClassPathXmlApplicationContext("classpath:dubbo.xml");
  5.  
  6. context.start();
  7.  
  8. OrderService orderService = (OrderService)context.getBean("orderService");
  9. Integer order = orderService.bugShop(1);
  10. System.out.println(order);
  11.  
  12. }
  13. }

以上就是dubbo spring的使用。

dubbo spring 的使用的更多相关文章

  1. Dubbo Spring Cloud 之 HTTP 实战

    上一篇文章<Spring Cloud Alibaba | Dubbo 与 Spring Cloud 完美结合>我们介绍了Dubbo Spring Cloud的基本使用,使用的服务中心为Sp ...

  2. 好消息:Dubbo & Spring Boot要来了

    Duboo和Spring Boot都是非常优秀的框架,现在它们要结合了.为了简化Dubbo开发集成,阿里Dubbo团队将发布基于Spring Boot的版本,可快速上手Dubbo的分布式开发,并提供了 ...

  3. dubbo spring bean id冲突

    service-security-provider应用有provider和consumer配置文件 其中secutrity-consumer引用两个服务 <dubbo:reference int ...

  4. 基于maven+dubbo+spring+zookeeper的简单项目搭建

    maven下搭建dubbo小demo,供初学者学习,有不正确地方还请见谅. 先推荐一篇创建maven项目的文章,个人认为比较完整详细清楚: http://www.cnblogs.com/leiOOle ...

  5. 手撕面试官系列(三):微服务架构Dubbo+Spring Boot+Spring Cloud

    文章首发于今日头条:https://www.toutiao.com/i6712696637623370248/ 直接进入主题 Dubbo (答案领取方式见侧边栏) Dubbo 中 中 zookeepe ...

  6. java/spring boot/dubbo/spring cloud/微服务/SOA/分布式经典电子书籍pdf下载

    微服务系列 官方文档是最好的资料了. spring cloud官方文档:https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/si ...

  7. dubbo spring pom文件报错:提示no declaration can be found for element 'dubbo:service'.

    pom文件报错:The matching wildcard is strict, but no declaration can be found for  element 'dubbo:service ...

  8. Dubbo Spring Cloud Motan

    跨语言统一治理.Golang,谈谈另辟蹊径的开源RPC框架Motan_搜狐科技_搜狐网 https://www.sohu.com/a/207389967_467759

  9. Spring Dubbo 开发笔记(一)——概述

    概述: Spring Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...

随机推荐

  1. VMware Workstation 将虚拟机挂起后,电脑会很卡,SCSI转换成IDE就可以了

    摘自:http://www.360doc.com/content/15/0405/09/10098873_460727712.shtml 用过 VMware Workstation 的人,不知道有没有 ...

  2. hadoop记录-flink测试

    1.启动集群 bin/start-cluster.sh 2.jps查看进程 3.打开网页端(192.168.66.128:8081) 4.造数据:nc -lk 9000 5.执行./bin/flink ...

  3. WebGL高级编程:开发Web3D图形 PDF(中文版带书签)

    WebGL高级编程:开发Web3D图形 目录 WebGL简介11.1 WebGL基础11.2 浏览器3D图形吸引人的原因21.3 设计一个图形API31.3.1 即时模式API31.3.2 保留模式A ...

  4. 如何让winrar5压缩的文件能用低版本winrar打开

    https://jingyan.baidu.com/article/39810a2348ab24b636fda681.html 在压缩文件格式选项处点选[RAR4]选项,即之前版本的winrar支持的 ...

  5. .net core 使用ClaimsIdentity实现登录授权

    一.新建用户 1.先新建一个用户表,用户存储用户信息. public class UserInfo { public const string Salt = "cesi"; [Ke ...

  6. SSH 连接时间超时

    linux服务端 # vi /etc/ssh/sshd_config ClientAliveInterval 60 ClientAliveCountMax 3 # 注: # ClientAliveIn ...

  7. LVS-TUN模式

    TUN模式: 其实数据转发原理和上图是一样的,不过这个我个人认为主要是位于不同位置(不同机房):LB是通过隧道进行了信息传输,虽然增加了负载,可是因为地理位置不同的优势,还是可以参考的一种方案: 优点 ...

  8. SpringCloud服务注册与发现中心-Eureka

    1.服务注册与发现的好处: 假设没有这个东西,那么如果存在a,b,c三个同样的服务: 而现在有一个u服务需要用到a或b或c提供的接口,那么u里面肯定是需要配置这三个服务的地址,然后调用的时候还有问题就 ...

  9. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

  10. Git使用总结(三):协同开发常见冲突

    1.不同人修改了不同的文件 a.账户A,账户B分别从远端拉取了相同分支     b.账户A修改了main.cpp文件后提交到远端,账户B修改fun.cpp文件提交远端时会报如下错误           ...