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文件之间变量和函数的 获取/调用 的方法

  2. Spring MVC 数据转换和格式化

    HttpMessageConverter和JSON消息转换器 HttpMessageConverter是定义从HTTP接受请求信息和应答给用户的 HttpMessageConverter是一个比较广的 ...

  3. 安卓apk反编译

    在win环境反编译安卓APP的.apk文件 安卓apk 反编译为 Java源码图文教程 Android安全攻防战,反编译与混淆技术完全解析(上)

  4. Python - Django - ORM Django 终端打印 SQL 语句

    在 settings.py 中添加以下内容: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'c ...

  5. Python - Django - ORM 不常用字段

    BigAutoField(AutoField): bigint 自增列,必须填入参数 primary_key=True 如果没有写自增列,则会自动创建一个列名为 id 的列 SmallIntegerF ...

  6. Delphi中进行延时的4种方法

     1.挂起,不占CPUsleep2.不挂起,占cpuprocedure Delay(msecs:integer);varFirstTickCount:longint;beginFirstTickCou ...

  7. PLSQL集合类型的使用总结

    PLSQL集合类型的使用总结 在pl sql 中,集合(collection) 是一组有序的元素组成的对象,这些元素的类型必须一致. pl sql 将collection 分成3 类,分别为Assoc ...

  8. [lodop]css样式after、before添加content内容之前和之后

    css样式可以在内容之前和之后加内容.格式是:css类名:before{content:在之前加的内容}css类名:after{content:在之后加的内容}这种写法在LODOP里直接测试是不行的, ...

  9. Django 高级视图

    一.Django限制请求method 常用的请求method: GET请求:GET请求一般用来向服务器索取数据,但不会向服务器提交数据,不会对服务器的状态进行更改.比如向服务器获取某篇文章的详情. P ...

  10. ATT&CK框架学习

     ATT&CK模型 ATT&CK是分析攻击者行为(即TTPs)的威胁分析框架.ATT&CK框架核心就是以矩阵形式展现的TTPs,即Tactics, Techniques and ...