本项目源于某培训机构的宜立方商城(淘淘商城)项目,重新利用 SpringBoot 2.0.4
框架替代原始的SSM三大框架进行重构项目,采用 Docker
容器替代原本的虚拟机来进行项目的部署。
利用IntellJ IDEA基于SSM框架实现淘淘商城代码仓库地址:https://github.com/MrSorrow/taotao
重构项目仓库地址:https://github.com/MrSorrow/e3-springboot
重构项目使用环境及版本如下:
- Java-1.8
- SpringBoot-2.0.4
- Dubbo-2.6.2
- ZooKeeper-3.4.13
- Solr-7.4
- ActiveMQ-5.14.3
- Thymeleaf-3.0.9
- FastDFS-5.05
- MySQL-5.7.23
- Redis-3.2
- Tomcat-8.5
- Maven-3.3.9
- IntellJ IDEA-2017.2
- Windows-10
- CentOS-7.4
- Docker-18.06CE
I. 导入工程
克隆代码仓库
git clone https://github.com/MrSorrow/e3-springboot.git
IDEA导入工程
打开IDEA,选择导入项目,选中克隆的仓库本地所在目录;
选择Maven进行导入;
利用Maven导入项目前进行相关配置;
然后选中所有模块进行导入即可,导入成功后可以浏览 Maven Projects 视图。
搭建运行环境
- CentOS系统下安装 Docker;
- 利用 Docker 安装MySQL、ZooKeeper、FastDFS、Redis、Solr 和 ActiveMQ。
注:具体参考项目教程部分。
修改配置文件
- 修改项目所有的 ip 地址;
- 修改数据库连接用户名、密码。
启动所有模块
启动所有容器实例;
启动所有Dubbo服务;
服务 模块 guo.ping.e3mall.manager.ManagerServiceApplicationStarter 后台 guo.ping.e3mall.content.ContentServiceApplicationStarter CMS guo.ping.e3mall.search.SearchServiceApplicationStarter 搜索 guo.ping.e3mall.sso.SsoServiceApplicationStarter 单点登录 guo.ping.e3mall.cart.CartServiceApplicationStarter 购物车 guo.ping.e3mall.order.OrderServiceApplicationStarter 订单 启动所有Web应用。
Web 模块 地址 guo.ping.e3mall.manager.ManagerWebApplicationStarter 后台 http://localhost:8081/ guo.ping.e3mall.portal.PortalWebApplicationStarter 首页 http://localhost:8082/ guo.ping.e3mall.search.SearchWebApplicationStarter 搜索 http://localhost:8083/ guo.ping.e3mall.item.ItemWebApplicationStarter 商品详情 http://localhost:8084/ guo.ping.e3mall.sso.SsoWebApplicationStarter 单点登录 http://localhost:8085/ guo.ping.e3mall.cart.CartWebApplicationStarter 购物车 http://localhost:8086/ guo.ping.e3mall.order.OrderWebApplicationStarter 订单 http://localhost:8087/ com.alibaba.dubboadmin.DubboAdminApplication Dubbo监控 http://localhost:7006/
II. 项目教程
搭建工程
Intellj IDEA创建空项目(New Project) e3-springboot;
创建其他模块(New Module);
模块名 打包方式 e3-parent pom e3-common jar e3-manager pom e3-manager-pojo jar e3-manager-dao jar e3-manager-interface jar e3-manager-service jar e3-manager-web jar 除 e3-parent 外其他所有模块父工程都为 e3-parent, e3-parent 的父工程为 spring-boot-starter-parent。由于使用spring boot框架,web模块打包方式也为jar。
注:后面引入Dubbo时会进行变动。
e3-manager-web 模块的maven插件配置利用:
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
其他模块的maven插件配置利用:
<plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins>
安装MySQL
-
# SET UP THE REPOSITORY AND INSTALL DOCKER CE sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo yum install -y docker-ce sudo systemctl enable docker sudo systemctl start docker # Manage Docker as a non-root user sudo groupadd docker sudo usermod -aG docker $USER # log out and log back in
-
docker pull mysql:5.7.23
启动MySQL容器(设置密码、端口映射);
docker run --name 实例名称 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=密码 -d mysql:5.7.23 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
开启3306端口防火墙;
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload
客户端(Navicat)连接MySQL容器。
整合MyBatis
e3-parent 定义相关依赖;
<!-- Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.starter.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId>