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. matlab学习笔记2--matlab的帮助

    一起来学matlab-matlab学习笔记2--matlab的帮助 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感谢张 ...

  2. Eureka 与 zookeeper 的区别、

    前言在微服务的开发过程中,如果使用的是 Dubbo 那就必须使用到 Zookeeper ,在使用 Spring Cloud Eureka 时,自然其功能更强大得多.博主也不得不感叹,Spring Cl ...

  3. 【ROC+AUC】

    http://m.elecfans.com/article/736801.html https://blog.csdn.net/xyz1584172808/article/details/818392 ...

  4. LODOP中带caption的表格被关联并次页偏移测试

    ADD_PRINT_TABLE中的thead和tfoot可以每页输出,后面的打印项关联表格,可以紧跟着表格,实现在表格后面紧跟着输出内容的效果,表格可以自动分页,并总是跟在表格后面 ,在表格最后输出. ...

  5. vue 组件传值$attrs $listeners $bus provide/inject $parent/$children

    $attrs 包含了父作用域中不作为prop被识别的特性绑定,当一个组件没有声明props时,这里会包含所有父作用域的绑定, $listeneers 包含了父作用域中的v-on事件监听器,它可以通过v ...

  6. phar缓存 编译缓存 提高phar文件包加载速度

    phar文件可以把用到的PHP文件全部打包在一个文件中,十分方便网站部署.但是单个的PHP文件可以使用opcache缓存(字节码缓存),以提升PHP的运行速度.那么PHAR文件包如何使用缓存呢. 这里 ...

  7. extract()函数:用于从一个date或者interval类型中截取到特定的部分

    extract()函数:用于从一个date或者interval类型中截取到特定的部分 ### extract 语法extract ( { year | month | day | hour | min ...

  8. c# 基础类型探索

    一.前言 本章节主要是探索 C# 的基本类型,一直以来我本人常用都是 int .double.bool.decimal.string 这五个类型,其对其它类型没有认真了解过.只是以前在学习的时候背了些 ...

  9. 使用java类加载器,报异常java.nio.file.InvalidPathException

    String path = Label.class.getClassLoader().getResource("").getPath(); /F:/idea-Java/ImageD ...

  10. 转:Cesium 和 Webpack

    原文地址:https://www.jianshu.com/p/85917bcc023f 注意:webpack 和 webpack-cli 的安装参考 https://www.cnblogs.com/m ...