Dubbo
Dubbo
一、 概念
1 Dubbo 介绍
Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的
RPC 实现服务的输出和输入功能,可以和 Spring 框架无缝集成。Dubbo 框架,是基于容器
运行的.。容器是 Spring。
官方网站 : http://dubbo.apache.org/
阿里巴巴已经将 dubbo 框架捐献给了 Apache 软件基金会
dubbo的详细说明:https://blog.csdn.net/weixin_41485592/article/details/81952499
https://blog.csdn.net/houshaolin/article/details/76408399
dubbo 阿里开源的一个SOA服务治理框架,从目前来看把它称作是一个RCP远程调用框架更为贴切。
单从RPC框架来说,功能较完善,支持多种传输和序列化方案。
所以想必大家已经知道他的核心功能了:就是远程调用。
2 单体架构
单体架构也称之为单体系统或者是单体应用。就是一种把系统中所有的功能、模块耦合
在一个应用中的架构方式。其优点为:项目易于管理、部署简单。缺点:测试成本高、可伸
缩性差、可靠性差、迭代困难、跨语言程度差、团队协作难
3 SOA 架构: Service-Oriented Architecture
面向服务的架构(SOA)是一个组件模型,它将应用程序拆分成不同功能单元(称为服
务)通过这些服务之间定义良好的接口和契约联系起来。接口是采用中立的方式进行定义的,
它应该独立于实现服务的硬件平台、操作系统和编程语言。这使得构建在各种各样的系统中
的服务可以以一种统一和通用的方式进行交互。
4 RPC 远程过程调用 : Remote Procedure Call Protocol
远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底
层网络技术的协议。RPC 协议假定某些传输协议的存在,如 TCP 或 UDP,为通信程序之间
携带信息数据。在 OSI 网络通信模型中,RPC 跨越了传输层和应用层。RPC 使得开发包括
网络分布式多程序在内的应用程序更加容易。
二、 Dubbo 框架结构
1 Dubbo 角色介绍
1.1registry
注册中心. 是用于发布和订阅服务的一个平台.用于替代 SOA 结构体系框架中的 ESB 服
务总线的。
1.1.1发布
开发服务端代码完毕后, 将服务信息发布出去. 实现一个服务的公开.
1.1.2订阅
客户端程序,从注册中心下载服务内容 这个过程是订阅.
订阅服务的时候, 会将发布的服务所有信息,一次性下载到客户端.
客户端也可以自定义, 修改部分服务配置信息. 如: 超时的时长, 调用的重试次数等.
1.2consumer
服务的消费者, 就是服务的客户端.
消费者必须使用 Dubbo 技术开发部分代码. 基本上都是配置文件定义.
1.3provider
服务的提供者, 就是服务端.
服务端必须使用 Dubbo 技术开发部分代码. 以配置文件为主
1.4container
容器. Dubbo 技术的服务端(Provider), 在启动执行的时候, 必须依赖容器才能正常启动.
默认依赖的就是 spring 容器. 且 Dubbo 技术不能脱离 spring 框架.
在 2.5.3 版本的 dubbo 中, 默认依赖的是 spring2.5 版本技术. 可以选用 spring4.5 以下版
本.
在 2.5.7 版本的 dubbo 中, 默认依赖的是 spring4.3.10 版本技术. 可以选择任意的 spring
版本.
1.5monitor
监控中心. 是 Dubbo 提供的一个 jar 工程.
主要功能是监控服务端(Provider)和消费端(Consumer)的使用数据的. 如: 服务端是什么,
有多少接口,多少方法, 调用次数, 压力信息等. 客户端有多少, 调用过哪些服务端, 调用了
多少次等.
2 Dubbo 架构图的执行流程
2.1Dubbo 执行流程
0 start: 启动 Spring 容器时,自动启动 Dubbo 的 Provider
1 register: Dubbo 的 Provider 在启动后自动会去注册中心注册内容.注册的内容包括:
1.1 Provider 的 IP
1.2 Provider 的端口.
1.3 Provider 对外提供的接口列表.哪些方法.哪些接口类
1.4 Dubbo 的版本.
1.5 访问 Provider 的协议
2 subscribe: 订阅.当 Consumer 启动时,自动去 Registry 获取到所已注册的服务的信息.
3 notify: 通知.当 Provider 的信息发生变化时, 自动由 Registry 向 Consumer 推送通知.
4 invoke: 调用. Consumer 调用 Provider 中方法
4.1 同步请求.消耗一定性能.但是必须是同步请求,因为需要接收调用方法后的结
果.
5 count:次数. 每隔 2 分钟,provoider 和 consumer 自动向 Monitor 发送访问次数.Monitor
进行统计.
3 Dubbo 支持的协议
3.1Dubbo 协议(官方推荐协议)
优点:
采用 NIO 复用单一长连接,并使用线程池并发处理请求,减少握手和加大并发效率,
性能较好(推荐使用)
缺点:
大文件上传时,可能出现问题(不使用 Dubbo 文件上传)
3.2RMI(Remote Method Invocation)协议
1) 优点:
JDK 自带的能力。可与原生 RMI 互操作,基于 TCP 协议
2) 缺点:
偶尔连接失败.
3.3Hessian 协议
1) 优点:
可与原生 Hessian 互操作,基于 HTTP 协议
2) 缺点:
需 hessian.jar 支持,http 短连接的开销大
4 Dubbo 支持的注册中心
4.1Zookeeper(官方推荐)
1) 优点:
支持分布式.很多周边产品.
2) 缺点:
受限于 Zookeeper 软件的稳定性.Zookeeper 专门分布式辅助软件,稳定较优
4.2Multicast
1) 优点:
去中心化,不需要单独安装软件.
2) 缺点:
2.2.1 Provider 和 Consumer 和 Registry 不能跨机房(路由)
4.3Redis
1) 优点:
支持集群,性能高
2) 缺点:
要求服务器时间同步.否则可能出现集群失败问题.
4.4Simple
1) 优点:
标准 RPC 服务.没有兼容问题
2) 缺点:
不支持集群
三、 Dubbo 入门案例
1 安装 Zookeeper
请看 zookeeper 视频
2 创建 Provider
2.1创建工程
Provider 工程类型是 jar 类型
2.2修改 POM 文件添加 Dubbo 的坐标
2.2.1关于版本的问题。在 Dubbo 的 2.5.3 版本中对于 spring
的支持是 2.5.x。替换低版本的 spring。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bjsxt</groupId> <artifactId>01-dubbo-provier-demo</artifactId > <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.alibaba/dub bo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <!-- 去除依赖 --> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencies> </project>
2.2.2Dubbo 的 2.5.4 版本中对于 spring 的支持为 4.3.10
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bjsxt</groupId> <artifactId>01-dubbo-provier-demo</artifactId > <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.alibaba/dub bo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.4</version> </dependency> </dependencies> </project>
2.3创建服务
服务:项目中对外界所提供的能力
服务标准:接口+接口实现类
接口:对外界描述当前服务的信息,如协议类型,服务提供者的地址,发布的服务名
称,服务中方法名称等信息。
接口实现类:对发布的接口中的标准的具体实现。
2.3.1创建接口
/* 服务接口*
@author Administrator**/
public interface DubboService
{
String showMsg(String str);
}
2.3.2创建接口实现类
/** * 服务接口实现类 * @author Administrator * */ public class DubboServiceImpl implements DubboService { @Override public String showMsg(String str) { return "Hello Dubbo "+str; } }
2.3.3配置服务
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/bean s http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.x sd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 给应用起名 --> <dubbo:application name="myprovider"/> <!-- 配置注册中心 --> <!-- address: 注册中心的 IP 和端口,注意:如果 zookeeper 是一个 集群,哪些要将集群中所有的 IP 和端口添加该属性中--> <!-- protocol: 注册中心的类型--> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:2182,192.168.70.14 3:2183" protocol="zookeeper"></dubbo:registry> <!-- 配置协议与端口 --> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!-- 配置注册接口 --> <dubbo:service interface="com.bjsxt.service.DubboService" ref="dubboServiceImpl"></dubbo:service> <bean id="dubboServiceImpl" class="com.bjsxt.service.impl.DubboServiceImpl"></bean> </beans>
2.4启动 Provider
2.4.1启动 Zookeeper 注册中心
2.4.2启动 Spring 容器
2.4.2.1 通过 SpringAPI 启动容器
在 ApplicationContext 接口中未定义 start()方法,需要时用接口实现类
2.4.2.2 启动时异常信息
JDK 版本不符合要求。注意 dubbo2.5.4 要求 jdk 必须是 1.8 或者以上的版本
2.4.2.3 工具名称为:zkclient
修改 POM 文件添加 zkclient 坐标
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
2.4.2.4 启动代码
public class Start { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application-dubbo. xml"); ac.start(); System.in.read(); } }
2.4.2.5 使用 DubboAPI 启动 Provider
public class Start { public static void main(String[] args) throws Exception { /*ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application-dubbo. xml"); ac.start(); System.in.read();*/ //Main 类下的 main 方法在启动时默认的回去 classpath:/META-INF/spring/*.xml Main.main(args); } }
特点:
1,自带线程阻塞
2,支持优雅关系
2.5注册中心中的服务发布信息
使用 zkCli.sh 连接 ZooKeeper 服务,使用 ls 命令查看服务信息。
ls /dubbo/服务接口名称/providers
显示结果如下:
dubbo%3A%2F%2F192.168.2.108%3A20880%2Fcom.bjsxt.service.DubboService%3Fanyh
ost%3Dtrue%26application%3Dmyprovider%26dubbo%3D2.5.4%26generic%3Dfalse%26interfa
ce%3Dcom.bjsxt.service.DubboService%26methods%3DshowMsg%26pid%3D3936%26side%3
Dprovider%26timestamp%3D1536807891514
转换结果如下:
dubbo://192.168.2.108:20880/com.bjsxt.service.DubboService?anyhost=true&application=m
yprovider&dubbo=2.5.4&generic=false&interface=com.bjsxt.service.DubboService&methods=sh
owMsg&pid=3936&side=provider×tamp=1536807891514
3 Dubbo Admin 管理平台搭建
从github上下载dubbo admin
3.1安装 Admin 管理平台
Admin 管理平台是一个 war 项目
在 Linux 系统中安装 JDK 以及 tomcat。
使用 unzip 命令解压 zip 压缩包。
删除 tomcat/webapps 目录下的 ROOT 目录
将解压出来的 ROOT 目录拷贝到 tomcat/webapps 目录下
3.2配置 Admin 管理平台
进入
/usr/local/tomcat/webapps/ROOT/WEB-INF找到dubbo.properties配置如下。
Address:zookeeper 的链接地址。如果是集群给一个就可以了
Root.password 设置默认 root 用户的密码
Guest.password 设置默认 guest 用户的密码
3.3访问 Admin 管理平台
4 创建 Consumer
4.1创建工程
4.2修改 POM 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bjsxt</groupId> <artifactId>02-dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> </project>
4.3编写 Consumer
4.3.1接口
public interface UserService {
void test(String str);
}
4.3.2接口实现类
@Service public class UserServiceImpl implements UserService { @Autowired private DubboService dubboService; @Override public void test(String str) { //调用 Provider 下的服务 System.out.println(this.dubboService); String var = this.dubboService.showMsg(" bjsxt "); System.out.println(var); } }
4.4配置 Consumer
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/be ans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins tance" xmlns:aop="http://www.springframework.org/schem a/aop" xmlns:context="http://www.springframework.org/s chema/context" xmlns:tx="http://www.springframework.org/schema /tx" xmlns:dubbo="http://code.alibabatech.com/schema /dubbo" xsi:schemaLocation="http://www.springframework. org/schema/beans http://www.springframework.org/schema/beans/spr ing-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/sprin g-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring -tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/s pring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo. xsd"> <context:component-scan base-package="com.bjsxt.service"/> <dubbo:application name="myconsumer"/> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:218 2,192.168.70.143:2183" protocol="zookeeper"></dubbo:registry> <!-- 获取接口 --> <!-- 获取到接口的代理对象,生成接口的 Proxy, 放入到 spring 容器中 --> <dubbo:reference id="dubboService" interface="com.bjsxt.service.DubboService"></du bbo:reference> </beans>
4.5测试 Consumer
4.5.1测试结果
四、 Dubbo 实战案例
1 案例介绍
1.1需求:完成对用户表的 CRUD 操作
1.2项目架构方式:SOA 面向服务架构
1.3技术选型:Maven,SpringMVC,Spring,Mybatis,Dubbo,
Zookeeper,MySQL
2 创建表
CREATE TABLE `users` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) DEFAULT NULL,
`userage` int(11) DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
3 项目设计
4 创建项目
4.1创建 dubbo-parent
4.1.1创建项目
4.1.2修改 POM 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/POM/4. 0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bjsxt</groupId> <artifactId>dubbo-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 对依赖的 jar 包的版本统一进行定义 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.v ersion> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <druid.version>1.0.9</druid.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <tomcat.version>2.2</tomcat.version> <jsp-api.version>2.0</jsp-api.version> <zkClient-version>0.10</zkClient-version> <dubbo-version>2.5.4</dubbo-version> </properties> <!-- jar 包的依赖注入 ,由于该工程是一个父工程, 所以 jar 包在该 pom 文件中只是声明--> <dependencyManagement> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!-- 日志处理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- JSP 相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo-version}</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkClient-version}</version> </dependency> </dependencies> </dependencyManagement> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> <!-- tomcat 插件,由于子项目不一定每个都是 web 项目,所以该插件只是声明,并未开启 --> <pluginManagement> <plugins> <!-- 配置 Tomcat 插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>${tomcat.version}</version> </plugin> </plugins> </pluginManagement> </build> </project>
4.2创建 dubbo-pojo
4.2.1创建项目
4.2.2创建 Users 实体
package com.bjsxt.pojo; public class Users { private int userid; private String username; private int userage; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getUserage() { return userage; } public void setUserage(int userage) { this.userage = userage; } }
4.3dubbo-mapper
4.3.1创建项目
4.3.2创建 UsersMapper 接口
package com.bjsxt.mapper; public interface UsersMapper { }
4.3.3创建 UsersMapper 映射配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.bjsxt.mapper.UserMapper" > </mapper>
4.3.4修改 POM 文件
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-mapper</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
4.4创建 dubbo-user-provider
4.4.1创建项目
4.5创建 dubbo-user-interface
4.5.1创建项目
4.5.2修改 POM 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-user-interface</artifactId> <dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
4.6创建 dubbo-user-service
4.6.1创建项目
4.6.2修改 POM 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-user-service</artifactId> <dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-mapper</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies> </project>
4.6.3配置 MyBatis 与 Dubbo
spring的配置文件默认在resources下的META-INF下的spring下读取。
applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans " xmlns:xsi="http://www.w3.org/2001/XMLSchema-i nstance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org /schema/context" xmlns:mvc="http://www.springframework.org/sch ema/mvc" xsi:schemaLocation="http://www.springframewor k.org/schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-m vc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd"> <!-- 配置解析 properties 文件的工具类 --> <context:property-placeholder location="classpath:*.properties"/> <!-- 配置数据源 dataSource --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 创建 mybatis 的上下文对象 --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="configLocation"> <value>classpath:mybatis/SqlMapperClient.xml</v alue> </property> </bean> <!-- 扫描 mybatis 的接口与映射配置文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConf igurer"> <property name="basePackage" value="com.bjsxt.mapper"/> </bean> </beans>
applicationContext-service.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/be ans" xmlns:xsi="http://www.w3.org/2001/XMLSchem a-instance" xmlns:p="http://www.springframework.org/schema/ p" xmlns:context="http://www.springframework. org/schema/context" xmlns:mvc="http://www.springframework.org/ schema/mvc" xsi:schemaLocation="http://www.springframe work.org/schema/beans http://www.springframework.org/schema/beans/spr ing-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/sprin g-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/s pring-context.xsd"> <!-- 扫描 bean 对象 --> <context:component-scan base-package="com.bjsxt.dubbo.service.impl"/> </beans>
applicationContext-trans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans " xmlns:context="http://www.springframework.org /schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/sch ema/aop" xmlns:tx="http://www.springframework.org/schema/tx " xmlns:xsi="http://www.w3.org/2001/XMLSchema-i nstance" xsi:schemaLocation="http://www.springframewor k.org/schema/beans http://www.springframework.org/schema/beans/spring -beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx -4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/springutil-4.0.xsd"> <!-- 配置事物管理器的切面 --> <bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事物传播行为 :其实就是那些方法应该受什 么样的事物控制--> <tx:advice id="advice" transaction-manager="transactionMananger"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="dorp*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类下的方法需要参与到当前的事物管理中 。 配置切点 --> <aop:config> <aop:advisor advice-ref="advice" pointcut="execution(* com.bjsxt.dubbo.service.impl*.*(..))"/> </aop:config> </beans>
application-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-instan ce" xmlns:aop="http://www.springframework.org/schema/a op" xmlns:context="http://www.springframework.org/sche ma/context" xmlns:tx="http://www.springframework.org/schema/tx " xmlns:dubbo="http://code.alibabatech.com/schema/du bbo" xsi:schemaLocation="http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx .xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <import resource="../../applicationContext-dao.xml"/> <import resource="../../applicationContext-service.xml"/> <import resource="../../applicationContext-trans.xml"/> <dubbo:application name="user-provider"/> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:2182,1 92.168.70.143:2183" protocol="zookeeper"></dubbo:registry> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> </beans>
4.6.4测试整合
关于 dubbo 与 spring 的 jar 注入问题。
在开发时对于 spring 的 jar 的处理上我们有两种方案
1,依赖 dubbo 的坐标导入 spring 的容器所依赖的 jar 包。在使用过程中缺少 spring 的
组件时,我们在手动添加坐标。
2,在添加 dubbo 的坐标中剔除已包含的 spring 的 jar 包。自己手动导入所有使用 spring
组件的坐标。
4.7创建 dubbo-user-consumer
4.7.1创建项目
4.8创建 dubbo-user-portal-service
4.8.1创建项目
4.9修改 POM 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-user-portal-service</artifa ctId> <dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project
4.10创建 dubbo-user-portal
4.10.1 创建项目
4.10.2 修改 POM 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-user-portal</artifactId> <packaging>war</packaging> <dependencies> <!-- 单元测试 --> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-portal-service</artifact Id> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <!-- 日志处理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!-- JSP 相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 配置 Tomcat 插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <path>/</path> <port>8080</port> </configuration> </plugin> </plugins> </build> </project>
4.10.3 配置 SpringMVC,Spring,web.xml,Dubbo
applicationContext-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-instan ce" xmlns:aop="http://www.springframework.org/schema/a op" xmlns:context="http://www.springframework.org/sche ma/context" xmlns:tx="http://www.springframework.org/schema/tx " xmlns:dubbo="http://code.alibabatech.com/schema/du bbo" xsi:schemaLocation="http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx .xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:application name="myconsumer"/> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:2182,1 92.168.70.143:2183" protocol="zookeeper"></dubbo:registry> </beans>
applicationContext-service.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans " xmlns:xsi="http://www.w3.org/2001/XMLSchema-i nstance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org /schema/context" xmlns:mvc="http://www.springframework.org/sch ema/mvc" xsi:schemaLocation="http://www.springframewor k.org/schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-m vc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd"> <!-- 扫描 bean 对象 --> <context:component-scan base-package="com.bjsxt.service"/> </beans>
springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans " xmlns:xsi="http://www.w3.org/2001/XMLSchema-i nstance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org /schema/context" xmlns:mvc="http://www.springframework.org/sch ema/mvc" xsi:schemaLocation="http://www.springframewor k.org/schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-m vc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd"> <!-- 包的扫描器主要是扫描@controller --> <context:component-scan base-package="com.bjsxt.web.controller"/> <!-- 注册两个新对象 主要是为了来处理 springmvc 中的其他 anntation 如:@requestmapping --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.Intern alResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /><!-- jsp 所在的前缀 --> <property name="suffix" value=".jsp" /> </bean> <!-- 配置静态资源映射 --> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> </beans>
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/n s/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 上下文参数,告诉 Spring 配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context .ContextLoaderListener</listener-class> </listener> <!-- 配置 springmvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet. DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-val ue> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.Ch aracterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
4.10.4 测试整合
5 实现业务
5.1添加用户
5.1.1实现添加用户服务
5.1.1.1 dubbo-mapper
public interface UsersMapper {
void insertUsers(Users users);
}
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.bjsxt.mapper.UsersMapper"> <insert id="UsersMapper" parameterType="com.bjsxt.pojo.Users"> insert into users(username,userage) values(#{username},#{userage}) </insert> </mapper>
5.1.1.2 dubbo-user-interface
public interface AddUserDubboService { void addUser(Users users); }
5.1.1.3 dubbo-user-service
@Service public class AddUserDubboServiceImpl implements AddUserDubboService { @Autowired private UsersMapper usersMapper; @Override public void addUser(Users users) { this.usersMapper.insertUsers(users); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xmlns:aop="http://www.springframework.org/schema/a op" xmlns:context="http://www.springframework.org/sche ma/context" xmlns:tx="http://www.springframework.org/schema/tx " xmlns:dubbo="http://code.alibabatech.com/schema/du bbo" xsi:schemaLocation="http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx .xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <import resource="../../applicationContext-dao.xml"/> <import resource="../../applicationContext-service.xml"/> <import resource="../../applicationContext-trans.xml"/> <dubbo:application name="user-provider"/> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:2182,1 92.168.70.143:2183" protocol="zookeeper"></dubbo:registry> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <dubbo:service interface="com.bjsxt.dubbo.service.AddUserDubboSer vice" ref="addUserDubboServiceImpl"></dubbo:service> </beans>
5.1.2实现添加用户业务
5.1.2.1 dubbo-user-portal-service
Pom 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instan ce" xsi:schemaLocation="http://maven.apache.org/P OM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-user-portal-service</artifa ctId> <dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-user-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
业务接口以及接口实现
public interface UserService {
void addUsers(Users users);
}
@Service public class UserServiceImpl implements UserService { @Autowired private AddUserDubboService addUserDubboService; @Override public void addUsers(Users users) { this.addUserDubboService.addUser(users); } }
dubbo-user-portal
UserController @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/addUser") public String addUser(Users users){ this.userService.addUsers(users); return "ok"; } }
applicationContext-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-instan ce" xmlns:aop="http://www.springframework.org/schema/a op" xmlns:context="http://www.springframework.org/sche ma/context" xmlns:tx="http://www.springframework.org/schema/tx " xmlns:dubbo="http://code.alibabatech.com/schema/du bbo" xsi:schemaLocation="http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx .xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:application name="myconsumer"/> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:2182,1 92.168.70.143:2183" protocol="zookeeper"></dubbo:registry> <dubbo:reference id="addUserDubboService" interface="com.bjsxt.dubbo.service.AddUserDubboSer vice"></dubbo:reference> </beans>
addUser.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="/user/addUser" method="post"> 用户名:<input type="text" name="username"/><br/> 用户年龄:<input type="text" name="userage"/><br/> <input type="submit" value="OKOK"/> </form> </body> </html>
Ok.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 操作成功請返回 </body> </html>
测试
5.2查询用户
5.2.1实现查询用户服务
5.2.1.1 dubbo-user-interface
public interface FindUserDubboService { List<Users> selectUsersAll(); }
5.2.1.2 dubbo-user-service
@Service public class FindUserDubboServiceImpl implements FindUserDubboService { @Autowired private UsersMapper usersMapper; @Override public List<Users> selectUsersAll() { return this.usersMapper.selectAll(); } }
5.2.1.3 application-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-instan ce" xmlns:aop="http://www.springframework.org/schema/a op" xmlns:context="http://www.springframework.org/sche ma/context" xmlns:tx="http://www.springframework.org/schema/tx " xmlns:dubbo="http://code.alibabatech.com/schema/du bbo" xsi:schemaLocation="http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring -beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-a op.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx .xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spri ng-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <import resource="../../applicationContext-dao.xml"/> <import resource="../../applicationContext-service.xml"/> <import resource="../../applicationContext-trans.xml"/> <dubbo:application name="user-provider"/> <dubbo:registry address="192.168.70.143:2181,192.168.70.143:2182,1 92.168.70.143:2183" protocol="zookeeper"></dubbo:registry> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <dubbo:service interface="com.bjsxt.dubbo.service.AddUserDubboSer vice" ref="addUserDubboServiceImpl"></dubbo:service> <dubbo:service interface="com.bjsxt.dubbo.service.FindUserDubboSe rvice" ref="findUserDubboServiceImpl"></dubbo:service> </beans>
5.2.2实现查询用户业务
5.2.2.1 dubbo-user-portal-service
public interface UserService { void addUsers(Users users); List<Users> findUserAll(); } @Service public class UserServiceImpl implements UserService { @Autowired private AddUserDubboService addUserDubboService; @Autowired private FindUserDubboService findUserDubboService; @Override public void addUsers(Users users) { this.addUserDubboService.addUser(users); } @Override public List<Users> findUserAll() { return this.findUserDubboService.selectUsersAll(); } }