前言:

  之前将各层都拆分出去, 作为一个独立的可替换的子模块. 感觉比以前确实是灵活了一些.

  不管是电商项目, 还是现在公司做的项目, 其中, 有很多的业务逻辑, 都是一样的, 但是由于不在一个系统中, 大家需要进行重复的工作. 有的拷贝还好, 但是有的, 没法直接拷贝. 相当的蛋疼. 能不能, 将业务逻辑独立出去, 供所有的展示层调用呢?

  是不是可以使用前一篇介绍的 dubbo 呢?

框架改造:

  前一篇, 通过多模块, 将各层拆分出去, 各自变成了一个独立的项目. 这里, 先将之前的稍微修改了下, 主要是改了项目名称, 加了一个common项目, 放一些共用的东西.

  common我也是用 springboot 建的, 只不过将入口那里注释掉了. 这里, 除了web有入口程序, 其他的入口全都注释掉了.

  

改造后的目录结构:

  

这里要注意开启service中的入口程序, 创建application.yml文件, 将之前在web中, 对mybatis的配置部分, 全部迁移到service中, 包括 MybatisConfig 和 MybatisMapperScannerConfig.

迁移哦, 不是复制. web端不需要这些东西了.

具体的改造过程, 就不贴了, 代码我会上传到码云中. 地址: https://gitee.com/elvinle/bookshop/tree/master/bookshop

集成dubbo zookeeper:

1. 业务层改造 -- 服务端

pom.xml 中加入:

<!--dubbo相关-->
<!-- https://mvnrepository.com/artifact/com.gitee.reger/spring-boot-starter-dubbo -->
<dependency>
<groupId>com.gitee.reger</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>

application.yml:

spring:
dubbo:
application: bookshop_provider
registry:
address: 192.168.153.129
protocol: zookeeper
port: 2181
protocol:
name: dubbo
port: 20880
base-package: cn.elvinle.manager.simpl

application : 名称自己随便取一个都可以, 不重复就好.

address : 前面zookeeper部署的电脑ip.

port: zookeeper服务的端口号

registry.protocol : 注册协议, zookeeper注册中心, 默认2181端口

dubbo.protocol : 服务协议, 使用 dubbo

dubbo.port : 服务端口, 默认20880

timeout : 调用超时, 默认为1000ms.

base-package : 客户端被扫描的包

这里使用的是单机部署, 没有使用zookeeper集群. 后面有机会的话, 会使用到.

代码改造:

package cn.elvinle.manager.simpl;

import cn.elvinle.manager.dao.mapper.UserMapper;
import cn.elvinle.manager.pojo.User;
import cn.elvinle.manager.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired; import java.util.List; @Service
public class UserSImpl implements UserService { @Autowired
private UserMapper userMapper; @Override
public List<User> getAll() { List<User> list = userMapper.getAll(); System.out.println("服务端读取到数据:" + list); return list;
}
}

这里貌似与之前的没什么不同, 但是, 这里的Service 注解, 不是之前的那个了, 而是 com.alibaba.dubbo.config.annotation.Service

2. web 集成 -- 客户端

pom.xml文件加入引用:

<!--dubbo相关-->
<!-- https://mvnrepository.com/artifact/com.gitee.reger/spring-boot-starter-dubbo -->
<dependency>
<groupId>com.gitee.reger</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>

application.yml  

spring:
dubbo:
application: web_consumer
registry:
address: 192.168.153.129
protocol: zookeeper
port: 2181
base-package: cn.elvinle.manager.web.controller
consumer:
timeout: 10000

代码改造:

package cn.elvinle.web.controller;

import cn.elvinle.manager.pojo.User;
import cn.elvinle.manager.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@RequestMapping("user")
public class UserController { @Reference
private UserService userService; @RequestMapping("index")
public List<User> index(){ List<User> all = userService.getAll(); System.out.println("客户端读取到数据: " + all); return all;
}
}

这里, 可以看到, 不再是使用Autowire了, 而是使用Reference注解, 是dubbo里面的.

 3. 注意:

  这里有个需要注意的地方, 就是传输的类, 必须实现  Serializable 接口, 表示支持序列化, 否则会报错的.

结果展示:

从浏览器上看, 确实访问到了数据. 再来看一下, 后台可打印了我要的信息.

再来看一下注册监测中心:

看看提供者:

消费者:

  

springboot 多模块 -- 将web拆分出去 - 流动计算架构的更多相关文章

  1. SpringBoot多模块项目打包问题

    项目结构图如下: 在SpringBoot多模块项目打包时遇见如下错误: 1.repackage failed: Unable to find main class -> [Help 1] 解决步 ...

  2. springboot多模块开发以及整合dubbo\zookeeper进行服务管理

    之前研究了springboot单工程的使用,参考git地址:https://github.com/qiao-zhi/springboot-ssm 下面研究springboot多模块开发的过程. 1.模 ...

  3. 使用IDEA构建Spring-boot多模块项目配置流程

    使用IDEA构建Spring-boot多模块项目配置流程 1.创建项目 点击Create New Project 在左侧选中Spring Initializer,保持默认配置,点击下一步. 在Grou ...

  4. HT for Web中3D流动效果的实现与应用

    流动效果在3D领域有着广泛的应用场景,如上图中医学领域可通过3D的流动直观的观察人体血液的流动,燃气领域可用于监控管道内流动的液体或气体的流向.流速和温度等指标. 如今企业数据中心机房普遍面临着设备散 ...

  5. 使用nodejs的http模块创建web服务器

    使用nodejs的http模块创建web服务器 laiqun@msn.cn Contents 1. web服务器基础知识 2. Node.js的Web 服务器 3. 代码实现 1. web服务器基础知 ...

  6. SpringBoot的第一个web项目

    这一节主要是讲springboot搭建简单的web项目. 首先pom文件新增spring-boot-starter-web依赖,pom文件如下所示 <?xml version="1.0 ...

  7. eclipse中创建多模块maven web项目

    本文讲述在eclipse中创建分模块maven web项目. 暂时将一个项目分为controller:service:dao以及父类模块四部分. 1.创建父类模块. 创建一个简单的maven proj ...

  8. 用 requests 模块从 Web 下载文件

    用 requests 模块从 Web 下载文件 requests 模块让你很容易从 Web 下载文件,不必担心一些复杂的问题,诸如网络错误.连接问题和数据压缩.requests 模块不是 Python ...

  9. springboot 之 使用jetty web容器

    springboot 中默认的web容器是tomcat. 在maven 的pom 文件中加入如下依赖,便可使用tomcat 容器. <dependency> <groupId> ...

随机推荐

  1. How to fix "http error 403.14 - forbidden" in IIS7

    If you encounter the following error: "http error 403.14 - forbidden. The Web server is configu ...

  2. (01背包 第k优解) Bone Collector II(hdu 2639)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem i ...

  3. Installing Apache Hadoop Single Node

    转载请注明出处:http://www.cnblogs.com/wubdut/p/4681286.html platform: Ubuntu 14.04 LTS hadoop 1.2.1 1. inst ...

  4. 在Win环境下配置java的环境进行开发步骤

    1.下载官方JDK,网址如下 http://www.oracle.com/technetwork/java/javase/downloads/index.html

  5. ETL化的IOTA架构

    经过这么多年的发展,已经从大数据1.0的BI/Datawarehouse时代,经过大数据2.0的Web/APP过渡,进入到了IOT的大数据3.0时代,而随之而来的是数据架构的变化. ▌Lambda架构 ...

  6. hdu 5054

    http://acm.hdu.edu.cn/showproblem.php?pid=5054 确定是否矩形中点 这都能hack成功,无语 #include <cstdio> #includ ...

  7. 七种bond模式说明

    第一种模式:mod=0 ,即:(balance-rr) Round-robin policy(平衡抡循环策略) 特点:传输数据包顺序是依次传输(即:第1个包走eth0,下一个包就走eth1….一直循环 ...

  8. Android x86模拟器Intel Atom x86 System Image配置与使用方法

    Android x86模拟器Intel Atom x86 System Image配置与使用方法      前言:      大家现在开发使用的Android 模拟器模拟的是 ARM 的体系结构(ar ...

  9. spring mvc 的请求流程

    SpringMVC核心处理流程: 1.DispatcherServlet前端控制器接收发过来的请求,交给HandlerMapping处理器映射器 2.HandlerMapping处理器映射器,根据请求 ...

  10. 第21件事 资源支持离不开RACI表

    十步法的第九步寻求资源支持.资源主要包括人力资源.物力资源和财力资源.人力资源,即需要多少人:物力资源,即需要多少软硬件设备:财力资源,即需要多少预算.根据产品或项目目标,资源估算时要考虑需要什么样的 ...