dubbo spring 的使用
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 的使用的更多相关文章
- Dubbo Spring Cloud 之 HTTP 实战
上一篇文章<Spring Cloud Alibaba | Dubbo 与 Spring Cloud 完美结合>我们介绍了Dubbo Spring Cloud的基本使用,使用的服务中心为Sp ...
- 好消息:Dubbo & Spring Boot要来了
Duboo和Spring Boot都是非常优秀的框架,现在它们要结合了.为了简化Dubbo开发集成,阿里Dubbo团队将发布基于Spring Boot的版本,可快速上手Dubbo的分布式开发,并提供了 ...
- dubbo spring bean id冲突
service-security-provider应用有provider和consumer配置文件 其中secutrity-consumer引用两个服务 <dubbo:reference int ...
- 基于maven+dubbo+spring+zookeeper的简单项目搭建
maven下搭建dubbo小demo,供初学者学习,有不正确地方还请见谅. 先推荐一篇创建maven项目的文章,个人认为比较完整详细清楚: http://www.cnblogs.com/leiOOle ...
- 手撕面试官系列(三):微服务架构Dubbo+Spring Boot+Spring Cloud
文章首发于今日头条:https://www.toutiao.com/i6712696637623370248/ 直接进入主题 Dubbo (答案领取方式见侧边栏) Dubbo 中 中 zookeepe ...
- java/spring boot/dubbo/spring cloud/微服务/SOA/分布式经典电子书籍pdf下载
微服务系列 官方文档是最好的资料了. spring cloud官方文档:https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/si ...
- 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 ...
- Dubbo Spring Cloud Motan
跨语言统一治理.Golang,谈谈另辟蹊径的开源RPC框架Motan_搜狐科技_搜狐网 https://www.sohu.com/a/207389967_467759
- Spring Dubbo 开发笔记(一)——概述
概述: Spring Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...
随机推荐
- Promise.resolve的作用
Promise.resolve方法有下面三种形式: Promise.resolve(value); Promise.resolve(promise); Promise.resolve(thenable ...
- Nginx搭建负载均衡集群
(1).实验环境 youxi1 192.168.5.101 负载均衡器 youxi2 192.168.5.102 主机1 youxi3 192.168.5.103 主机2 (2).Nginx负载均衡策 ...
- Python - Django - CSRF
CSRF 攻击: 把 settings.py 中的 csrf 注释掉 正规网站: 创建修改密码页面 password.html: <!DOCTYPE html> <html lang ...
- HTML布局排版之制作个人网站的文章列表
文章列表.博文列表,一般是有文章名字和时间构成的,文章名字后面是时间,点击文章的名字,可进入该文章.为了美观,一般文章名字都有一定的最大字数限制,长宽对齐,等长宽的统一格式比较美观,这种用表格来做比较 ...
- css 修改placeholder样式
input::-webkit-input-placeholder{ color:red; } input::-moz-placeholder{ /* Mozilla Firefox 19+ */ co ...
- 关于Form、ModelForm的一些操作(持续更新)
1.前端循环:后端传到前端的form是可以循环的,以此获得想要展示的元素 <form method="post" class="form-horizontal&qu ...
- java设计模式(一)——单例模式
1.基本介绍 单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供-一个取得其对象实例的方法(静态方法).如:一般情况下,数据库的连接 2.创建方式: ...
- __init__调用之二
class Bar: def __init__(self,name,age): self.suibian = name #self后的名字是啥,对象就可以调用啥,而不是 __init__ 后括号形参 ...
- Deepin 15.11 install nvidia dirver[mei you an zhuang shu ru fa]
1.firstly, exec: sudo vim /etc/modprobe.d/blacklist-nouveau.conf[create], and input [blacklist nouve ...
- 用php做管理后台
最近因处理家庭之事,技术上没有提高,这段时间也陆续的恢复了正常的开发,由于要做一个管理后台,所以在选择语言和架构上搜了不少资料, php 和java 的选择上,后来选择用php作为管理后台开发的语言. ...