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的使用。

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. python中异常处理之esle,except,else

    异常是指程序中的例外,违例情况.异常机制是指程序出现错误后,程序的处理方法.当出现错误后,程序的执行流程发生改变,程序的控制权转移到异常处理. python中使用try...except语句捕获异常, ...

  2. PyCharm安装及其使用

    1.前提:Python+selenium的安装教程如下网址 https://www.cnblogs.com/linxiu-0925/p/9597634.html 2.PyCharm安装 1.首先去Py ...

  3. Keras代码超详细讲解LSTM实现细节

    1.首先我们了解一下keras中的Embedding层:from keras.layers.embeddings import Embedding: Embedding参数如下: 输入尺寸:(batc ...

  4. Teaset-React Native UI 组件库

    GitHub地址 https://github.com/rilyu/teaset/blob/master/docs/cn/README.md React Native UI 组件库, 超过 20 个纯 ...

  5. sql 表的连接与查找

    A.left outer join: 左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行. SQL: select a.a, a.b, a.c, b.c, b.d, b.f fro ...

  6. Django时区导致的datetime时间比较报错

    我们使用python 的datetime模块比较Django数据库Datetime字段的时候,可能会出现报错: TypeError: can't compare offset-naive and of ...

  7. [Xamarin] - "GenerateJavaStubs" 异常之解决

    背景 新建的 Xamarin 项目,编译失败. Error The "GenerateJavaStubs" task failed unexpectedly.Error The s ...

  8. java多线程中篇(一) —— Thread详情

    简介 简言之,现在的JDK线程模型基于操作系统原生线程,所以模型依赖于操作系统对线程的支持,另外Windows和Linux系统提供的线程模型就是一对一的 所以可以简单认为: 现在Java线程与操作系统 ...

  9. 修改主机名和修改主机映射和ssh免登陆

    1.修改主机名 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=cc3 2.修改主机映射 vi /etc/hosts 127.0.0.1 loca ...

  10. fork() 函数简介

    fork() 函数简介 fork系统调用用于创建一个新进程,称为子进程,它与进行fork()调用的进程(父进程)并发运行.创建新的子进程后,两个进程都将执行fork()系统调用之后的下一条指令.子进程 ...