一、新建普通maven项目

1、首先,新建3个普通maven商城项目,模拟以往常见的Java单体应用开发,mall-interface是存放接口和公共代码部分,order-service-consumer和user-service-provider的pom依赖于mall-interface。

2、在order-service-consumer和user-service-provider中分别实现接口,编写各自的实现类

以往,如果order-service-consumer和user-service-provider有相互调用,一般需要当作一个模块引用,部署到服务器的时候需要部署全部模块,分布部署多个服务器的话就很麻烦,并且大的项目模块多的放在一起不方便开发和管理。

那么,把项目中各个模块分开部署那不就好了?但是如果直接把order-service-consumer 和user-service-provider分开部署不同服务器上,显然他们不能相互调用业务接口。这时Dubbo作为RPC框架,它的用处显现出来了。简单的说,Dubbo可以远程调用部署在不同服务器上的业务接口。

通过Dubbo改造,即使order-service-consumer 和user-service-provider部署不同机器,两个模块可以像调用本地接口一样调用远程服务。

二、通过Dubbo改造普通项目

1、改造user-service-provider项目,通过Dubbo发布服务

1.1 在pom.xml中引入Duoob依赖

<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.xg.xmall.dubbo</groupId>
<artifactId>user-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.xg.xmall</groupId>
<artifactId>mall-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.2 在资源文件夹新建Dubbo配置文件 provider.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: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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-service-provider"></dubbo:application> <!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry> <!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol> <!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service
interface="com.xg.xmall.dubbo.service.UserService"
ref="userServiceImpl" timeout="1000" version="1.0.0">
<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service> <!-- 服务的实现 -->
<bean id="userServiceImpl"
class="com.xg.xmall.dubbo.service.impl.UserServiceImpl"></bean> </beans>

2.3 编写Dubbo服务启动类 MainProviderApplication,启动服务发布注册服务

发布服务之前需要启动 Zookeeper

从Dubbo 服务监控中可以看见服务发布成功

2、改造order-service-consumer项目,通过Dubbo访问服务

1.1 同样在项目下的pom.xml中引入Duoob依赖

<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.xg.xmall.dubbo</groupId>
<artifactId>user-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.xg.xmall</groupId>
<artifactId>mall-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.2 在资源文件夹新建Dubbo配置文件 consumer.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:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 扫描组件 -->
<context:component-scan
base-package="com.xg.xmall.dubbo.service.impl"></context:component-scan> <dubbo:application name="order-service-consumer"></dubbo:application> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!--声明需要调用的远程服务的接口,生成远程服务代理 -->
<dubbo:reference
interface="com.xg.xmall.dubbo.service.UserService" id="userService"
timeout="5000" retries="3" version="*">
</dubbo:reference> <dubbo:monitor protocol="registry"></dubbo:monitor> </beans>

2.3 编写Dubbo服务启动类 MainConsumerApplication,启动获取服务

order-service-consumer并没有存放用户信息实现类,只是注入服务的接口,就可以获取其他项目提供的用户信息。可见,通过Dubbo调用远程服务成功

Dubbo 改造普通单体项目的更多相关文章

  1. 使用 GNU autotools 改造一个软件项目

    使用 GNU autotools 改造一个软件项目 及永刚 jungle@soforge.com 2006 年 3 月 24 日  版本:0.3 本文不是一篇规范的教程,而是用一个软件项目作为例子,演 ...

  2. CentOS 7.6部署Vue + SrpingBoot + MySQL单体项目

    对于独立的项目(前端.后台单体服务.数据库),部署到新服务器上时,常常需要繁琐的配置与环境安装,这里介绍Centos 7.6下如何搭建基于Docker的环境,以及如何使用docker部署一套Vue + ...

  3. JavaWeb单体项目的分层设计与实现

    1.概述 为什么要把一个完整的项目(Project)按层拆分成多个模块(Module)? 1)使项目层次更加的清晰: 2)提高代码的复用性: 3)细化分工: 4)解耦. 是不是听起来很高大尚,今天就简 ...

  4. 👍SpringSecurity单体项目最佳实践

    SpringSecurity单体项目最佳实践 到这里,我们的SpringSecurity就已经完结啦,文章中可能有些地方不能做到全面覆盖,视频教程地址 初始项目地址 完成项目地址 1.搭建环境 建议下 ...

  5. 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(2)

    我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...

  6. 配置dubbo架构的maven项目

    1. 拆分工程 1)将表现层工程独立出来: e3-manager-web 2)将原来的e3-manager改为如下结构 e3-manager |--e3-manager-dao |--e3-manag ...

  7. 分布式服务框架Dubbo入门案例和项目源码

    本项目源代码:http://download.csdn.net/detail/fansunion/9498406 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案, 是 ...

  8. dubbo zk 分布式服务项目搭建与配置

    1. 项目 jar  -----提供接口 2. 项目 jar  -----接口实现   provider启动zk main方法启动 start applicationContext.xml <b ...

  9. Dubbo开发,利用项目模拟提供者和消费者之间的调用--初学

    开发工具:IDEA,虚拟机 VMware Workstation 预备工作:安装好zookeeper的虚拟机,电脑jdk更换为1.7,本地tomcat启动,能够访问以下页面即可进行开发 2.建立以下s ...

随机推荐

  1. Java基础学习总结一(Java语言发展历史、JDK下载安装以及配置环境变量)

    最近一段时间计划复习一下java基础知识,使用的视频课程是尚学堂高淇老师的,上课过程中的心得体会直接总结一下,方便以后复习. 一:计算机语言的发展 1:机器语言,最原始的语言,主要有“01”构成,最早 ...

  2. Generic/Template Programming in Flink

    Generic/Template Programming in Flink SourceFunction<T> @Public public interface SourceFunctio ...

  3. windows定位dll的搜索顺序

    原文:http://blog.csdn.net/zzxian/article/details/6429293 Visual C++ Windows 用来定位 DLL 的搜索路径 通过隐式和显式链接,W ...

  4. Genymotion模拟器拖入文件报An error occured while deploying the file的错误

    今天需要用到资源文件,需要将资源文件拖拽到sd卡中,但老是出现这个问题: 资源文件拖不进去genymotion.查看了sd的DownLoad目录,确实没有成功拖拽进去. 遇到这种问题的,我按下面的思路 ...

  5. arcgis 线段合并

    对于上面这种这种有一个字段相同的 线段,使用 使用后生成的矢量如下

  6. git revert .vs. git reset .vs. git rebase

    1. git rervert的工作方式是:将一个老的commit的改动完全找出来,并且在新的tip处运行反操作,最终清除老commit的改动: git revert的应用场景多在对public rep ...

  7. 微软发布Azure Stack第一个技术预览版

    为了提升商业灵敏度和加快创新步伐,各个企业都在迅速地转向云服务.在微软,我们已经见到微软智能云Azure的飞速发展和使用,每月我们都有近十万的新增订阅量.然而,我们也了解到还有很多企业在完全移到公有云 ...

  8. Redis的系统级命令

    文章建立一个统一的认识就是Redis的版本是3.2.8 1:BGREWRITEAOF(bgrewriteaof) 执行一个 AOF文件 重写操作.重写会创建一个当前 AOF 文件的体积优化版本. 即使 ...

  9. ESP32D0WDQ6 灯泡 黑客

    这个黑客表现得如何聪明 灯泡 可能泄漏您的Wi-Fi密码O网页链接破解者博客详文 Pwn the LIFX Mini white O网页链接ESP32D0WDQ6, a SoC from ESPRES ...

  10. Hyperledger Fabric 1.0 学习搭建 (三)--- 运行测试e2e-Fabric

    3.1.运行fabric-samples的问题说明 该问题说明能够解决6.1.平台特定使用的二进制文件配置第一步的问题.可以选择继续阅读该说明,或者等参考到6.1小节时再反向阅读本说明,具体在6.1中 ...