首先我们来看一下dubbo的架构:

所以通过此图,我们看到就是服务的提供者将服务注册到注册中心,服务的消费者从注册中心获取服务,monitor监控服务的调用。

关于dubbo的使用,我们举个简单的例子:

存在2个系统,A系统和B系统,A系统调用B系统的接口获取数据,用于查询用户列表。

废话不多少,直接构建项目:

构建一个dubbo-ab-api ,此项目包含供其他两个项目使用的POJO类和接口Service类

1、在Eclipse中点检新建项目,选择maven项目后,点击“next”:

2、项目存储位置,点击“next”:

3、因为建立的是“jar”项目,选择“maven-archetype-quickstart”,点击“next”:

4、填写Group ID(项目组织唯一的标识符)和Artifact ID(项目的唯一的标识符) 点击“Finish”,完成项目创建。

5、项目创建好后,在项目中增加pojo和service包:

6、在pojo包中添加User.java 类。

package cn.itcast.dubbo.pojo;import java.io.Serializable;public class User implements Serializable {/*** */private static final long serialVersionUID = -8096919250664823274L;private Long id;private String username;private String password;private Integer age;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";}}

7、在service包中UserService.java 接口类。

package cn.itcast.dubbo.service;import java.util.List;import cn.itcast.dubbo.pojo.User;public interface UserService {/*** 查询所有的用户数据** @return*/public List<User> queryAll();
}

7、将JAR包安装到本地仓库,供其他Maven项目依赖使用。(如果有maven私仓,则将jar包添加到maven私仓中,供其他项目调用)

创建dubbo-b项目,此项目为web项目,作为服务提供方

1、在Eclipse中点检新建项目,选择maven项目后,点击“next”:

2、项目存储位置,点击“next”:

3、建立web项目,选择“maven-archetype-webapp”,点击“next”:

4、填写Group ID(项目组织唯一的标识符)和Artifact ID(项目的唯一的标识符) 点击“Finish”,完成项目创建。

5、项目创建好后,在项目中增加cn.itcast.dubbo.service.impl包:

6、在“pom.xml”导入依赖:

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.itcast.dubbo</groupId><artifactId>dubbo-b</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>dubbo-b Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.3.RELEASE</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.4</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.3</version></dependency><dependency><groupId>cn.itcast.dubbo</groupId><artifactId>dubbo-ab-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><!-- 排除传递spring依赖 --><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8081</port><path>/</path></configuration></plugin></plugins></build>
</project>

7、在cn.itcast.dubbo.service.impl包中创建UserServiceImpl实现类

package cn.itcast.dubbo.service.impl;import java.util.ArrayList;
import java.util.List;import cn.itcast.dubbo.pojo.User;
import cn.itcast.dubbo.service.UserService;public class UserServiceImpl implements UserService {/*** 实现查询,这里做模拟实现,不做具体的数据库查询*/public List<User> queryAll() {List<User> list = new ArrayList<User>();for (int i = 0; i < 10; i++) {User user = new User();user.setAge(10 + i);user.setId(Long.valueOf(i + 1));user.setPassword("123456");user.setUsername("username_" + i);list.add(user);}return list;}}

8、在“src/main/resources”包中创建dubbo的配置文件“dubbo-applicationContext.xml”

<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/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="dubbo-b-server" /><!-- 这里使用的注册中心是zookeeper --><dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20880" /><!-- 将该接口暴露到dubbo中 --><dubbo:service interface="cn.itcast.dubbo.service.UserService" ref="userServiceImpl" /><!-- 将具体的实现类加入到Spring容器中 --><bean id="userServiceImpl" class="cn.itcast.dubbo.service.impl.UserServiceImpl" /><dubbo:monitor protocol="registry"></dubbo:monitor></beans>

9、使用spring与dubbo进行了无缝整合,要在web.xml中将配置文件引入

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>dubbo-b</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-applicationContext.xml</param-value></context-param><!--Spring的ApplicationContext 载入 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

创建dubbo-a项目,作为服务消费方。

1、在Eclipse中点检新建项目,选择maven项目后,点击“next”:

2、项目存储位置,点击“next”:

3、因为建立的是“jar”项目,选择“maven-archetype-quickstart”,点击“next”:

4、填写Group ID(项目组织唯一的标识符)和Artifact ID(项目的唯一的标识符) 点击“Finish”,完成项目创建。

5、在“pom.xml”导入依赖:

<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>cn.itcast.dubbo</groupId><artifactId>dubbo-a</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>dubbo-a</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.3.RELEASE</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><!-- 排除传递spring依赖 --><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>cn.itcast.dubbo</groupId><artifactId>dubbo-ab-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
</project>

6、在“src/main/resources”包中创建配置服务的消费者“dubbo-consumer.xml” 。

<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/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="dubbo-a-consumer" /><!-- 这里使用的注册中心是zookeeper --><dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/><!-- 从注册中心中查找服务 --><dubbo:reference id="userService" interface="cn.itcast.dubbo.service.UserService"/></beans>

7、编写测试代码

public static void main( String[] args ){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:*.xml");UserService userService = applicationContext.getBean(UserService.class);List<User> users = userService.queryAll();for (User user : users) {System.out.println(user);}}

运行结果截图:

Dubbo 在maven项目中的应用相关推荐

  1. 在maven项目中使用Junit进行单元测试

    在maven项目中使用Junit进行单元测试(一) 在maven项目中使用Junit进行单元测试一 创建maven项目 编写测试用代码 小结 这是第一篇博文,所以我决定先从比较简单的内容写起,同时熟悉 ...

  2. 解决 maven 项目中加入了 lombok 库后依然报错的问题

    解决 maven 项目中加入了 lombok 库后依然报错的问题 参考文章: (1)解决 maven 项目中加入了 lombok 库后依然报错的问题 (2)https://www.cnblogs.co ...

  3. executequery方法_在IDEA的maven项目中连接使用MySQL8.0方法教程

    首先看一下我的基本的开发环境: 操作系统:MacOS 10.13.5编辑器:IDEA 2018.3其他:MySQL8.0.15.Maven 3.3.9.JDK 1.8 好,下面就正式开始: 第一步:在 ...

  4. 【报错笔记】在maven项目中jsp页面使用window.location.href给controller传参时参数过长所以路径无法跳转至controller

    在maven项目中jsp页面使用window.location.href给controller传参时参数过长所以路径无法跳转至controller 解决方案:使用a链接来用

  5. Maven项目中使用JUnit进行单元测试

    1.打开maven项目中的pom.xml,添加JUnit 的jar包 2.在src/test/java下右键新建JUnit Test Cast 转载于:https://www.cnblogs.com/ ...

  6. 解决“Maven项目中的Dynamic Web Module 3.0 requires Java 1.6 or newer”问题

    转载自   解决"Maven项目中的Dynamic Web Module 3.0 requires Java 1.6 or newer"问题 错误描述 当创建有动态web模块3.0 ...

  7. idea新建scala文件_IDEA maven项目中新建.scala文件

    本文首发于我的博客[IDEA maven项目中新建.scala文件] 分为三步 第一步.IDEA中安装scala插件 1.搜索安装 File-Sittings-Plugins-搜索安装scala 2. ...

  8. Maven项目中使用本地JAR包

    在Maven项目中使用本地JAR包有两种方法: 1. 使用system scope <dependencies><dependency><groupId>org.r ...

  9. java maven 读取配置文件_Java项目和maven项目中如何获取设置配置文件中的属性

    通常情况下,我们会在一些配置文件文件中配置一些属性.如: indexPath = E\:\\Tomcat_7.0\\webapps\\ipost_stage\\lucene\\index imgUpl ...

最新文章

  1. php mysqli扩展之预处理
  2. 成功解决WARNING: Ignoring invalid distribution -illow (E:\program files\python\python36\lib\site-package
  3. python两个等号和一个等号_Python-一个变量等于另一个变量
  4. 【Tensorflow】相关面试题整理(转)
  5. java项目ppt介绍_Java课设项目介绍及分析.ppt
  6. 数据库系统概论笔记三——销售管理子系统E-R图的设计
  7. 阿里云部署RSSHub踩坑笔记
  8. Wework的线上社交,能否支撑其169亿估值?
  9. SDPT3 4.0版——半正定二次线性规划的Matlab软件
  10. 计算机专业surface pro,微软Surface Pro 7详细评测:仍旧是最好的二合一平板电脑
  11. 文明6/Civilization VI 全DLC解锁
  12. python批量删除图片和空文件夹
  13. 关于elementui的table固定高度出现的表格高度有空缺
  14. 使用PlayCanvas制作一个简单的小游戏(一)
  15. c语言笔记本无法读取鼠标,USB鼠标失灵了怎么办 电脑无法识别USB鼠标【详解】...
  16. linux 内核 输出,Linux基础命令---dmeg显示内核输出
  17. 东师《构成设计基础》离线作业
  18. PMP 项目管理 考前专题(02)敏捷开发专题总结
  19. java源代码转jar包
  20. Lies, dxmn lies and Chin-ese science ~~where all the truth gone?

热门文章

  1. 36 岁开发者应聘被拒,这 3 位 50 岁程序员的生存秘籍送给你!
  2. 坦白讲!90%的数据分析师都不合格!!
  3. POJ-3662 Telephone Lines 二分+双端队列
  4. Zeppelin源码
  5. 断点续传---多线程下载进阶(一)
  6. Java基础-流程控制
  7. .Net Base64编码
  8. 2012体感发展加速,微软再添新对手
  9. Pass4side CompTIA PK0-002题库下载
  10. 全球顶级设计师云集天猫双11 超1000款时尚大牌新品首发