Skip to content

Commit

Permalink
添加docker容器化部署,配置区分dev和prod环境
Browse files Browse the repository at this point in the history
  • Loading branch information
zhh committed Aug 20, 2018
1 parent 7ece991 commit 8cb2050
Show file tree
Hide file tree
Showing 22 changed files with 397 additions and 112 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
Spring Boot | 容器+MVC框架
Spring Security | 认证和授权框架
MyBatis | ORM框架
MyBatisGenerator | 代码生成
MyBatisGenerator | 数据层代码生成
PageHelper | MyBatis物理分页插件
Swagger-UI | 文档生产工具
Hibernator-Validator | 验证框架
Elasticsearch | 搜索引擎
RabbitMq | 消息队列
Redis | 分布式缓存
MongoDb | NoSql数据库
Docker | 应用容器引擎

### 前端技术

Expand Down Expand Up @@ -51,13 +52,14 @@ JTA事务处理 | ✔
集成单元测试 | ✔
OSS上传功能 | ✔
Elasticsearch搜索功能 | ✔
SpringSecurity权限管理功能 |
HTTPS支持 | ✔
日志收集功能 |
数字型ID生成 |
定时任务支持 |
SpringSecurity权限管理功能 |
ELK日志收集功能 |
Redis数字型ID生成 |
SpringTask定时任务支持 |
RestTemplate服务间调用 |
docker容器化部署 |
docker容器化部署 | ✔
配置区分生产和测试环境 | ✔

### 后台功能

Expand Down
20 changes: 19 additions & 1 deletion docker-deploy.md → document/docker/docker-deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,22 @@ docker pull mongo:3.2
###创建实例并运行
docker run -p 27017:27017 --name mongo -v $PWD/db:/data/db -d mongo:3.2
###使用mongo命令进入容器
docker exec -it mongo mongo
docker exec -it mongo mongo

##SpringBoot应用部署
**docker容器间进行连接才能互相访问**
###部署mall-admin
docker run -p 8080:8080 --name mall-admin \
--link mysql:db \
-d mall/mall-admin:0.0.1-SNAPSHOT
###部署mall-search
docker run -p 8081:8081 --name mall-search \
--link elasticsearch:es \
--link mysql:db \
-d mall/mall-search:0.0.1-SNAPSHOT
###部署mall-port
docker run -p 8085:8085 --name mall-portal \
--link mysql:db \
--link redis:redis \
--link mongo:mongo \
-d mall/mall-portal:0.0.1-SNAPSHOT
87 changes: 87 additions & 0 deletions document/docker/docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#Docker笔记

##Docker 镜像常用命令
###搜索镜像
docker search java
###下载镜像
docker pull java:8
docker pull macro/eureka-server:0.0.1
###列出镜像
docker images
###删除镜像
docker rmi java
docker rmi -f java
docker rmi -f $(docker images)

##Docker 容器常用命令
###新建并启动容器
docker run -d -p 91:80 nginx
###列出容器
docker ps
###停止容器
docker stop $ContainerId
###强制停止容器
docker kill $ContainerId
###启动已停止的容器
docker start $ContainerId
###进入容器
docker inspect --format "{{.State.Pid}}" $ContainerId
nsenter --target "$pid" --mount --uts --ipc --net --pid
###删除容器
docker rm $ContainerId
docker rm -f $(docker ps -a -q)

##Docker Registry
###Docker Registry 2.0搭建
docker run -d -p 5000:5000 --restart=always --name registry2 registry:2
###推送到私有仓库
docker push localhost:5000/macro/eureka-server:0.0.1
###修改镜像标签
docker tag macro/eureka-server:0.0.1 localhost:5000/macro/eureka-server:0.0.1

##使用maven构建Docker镜像
###构建镜像
- command:mvn clean package docker:build
- tip:
Linux服务器需要开启远程api:vi /usr/lib/systemd/system/docker.service
修改为:ExecStart=/usr/bin/dockerd -H tcp:https://0.0.0.0:2375 -H unix:https://var/run/docker.sock
###推送镜像到私有仓库
- command:mvn clean package docker:build -DpushImage
- tip:
pom.xml修改<imageName>192.168.1.71:5000/macro/${project.artifactId}:${project.version}</imageName>
- tip:
docker要支持http:echo '{ "insecure-registries":["192.168.1.71:5000"] }' > /etc/docker/daemon.json
###修改Docker镜像存放位置
1. 查看Docker的存放位置:docker info | grep "Docker Root Dir"(默认为/var/lib/docker)
2. 关闭Docker服务:systemctl stop docker
3. 移动目录到目标路径:mv /var/lib/docker /root/data/docker
4. 建立软连接:ln -s /root/data/docker /var/lib/docker

##Docker compose
###安装
1. 下载地址:https://github.com/docker/compose/releases
2. 安装地址:/usr/local/bin/docker-compose
3. 设置为可执行:sudo chmod +x /usr/local/bin/docker-compose
4. 测试是否安装成功:docker-compose --version

###安装命令补全工具
sudo curl -L https://raw.githubusercontent.com/docker/compose/1.22.0/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

###常用命令
- 构建、创建、启动相关容器:docker-compose up
- 列出所有容器:docker-compose ps
- 删除指定服务的容器:docker-compose rm eureka
- 对容器进行动态扩容:docker-compose scale eureka=3
- 停止相关容器:docker-compose stop eureka
- 启动相关容器:docker-compose start eureka

###编排SpringCloud微服务
####所使用到的工程
- eureka-server
- hello-service
- feign-consumer
- api-gateway
####编排模式
1. 编排SpringCloud微服务:见eureka-server/docker-res/docker-compose.yml
2. 简化SpringCloud微服务编排:见eureka-server/docker-res/docker-compose-simple.yml
3. 编排高可用的注册中心:见eureka-server/docker-res/docker-compose-eureka.yml
4 changes: 4 additions & 0 deletions document/docker/host.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
192.168.1.71 db
192.168.1.71 es
192.168.1.71 redis
192.168.1.71 mongo
53 changes: 46 additions & 7 deletions document/sql/mall.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Target Server Type : MYSQL
Target Server Version : 50719
File Encoding : 65001
Date: 2018-06-21 10:11:27
Date: 2018-08-20 13:53:09
*/

SET FOREIGN_KEY_CHECKS=0;
Expand Down Expand Up @@ -138,7 +138,7 @@ CREATE TABLE `cms_subject` (
`content` text,
`forward_count` int(11) DEFAULT NULL COMMENT '转发数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='专题表';
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='专题表';

-- ----------------------------
-- Records of cms_subject
Expand Down Expand Up @@ -278,6 +278,41 @@ CREATE TABLE `cms_topic_comment` (
-- Records of cms_topic_comment
-- ----------------------------

-- ----------------------------
-- Table structure for oms_cart_item
-- ----------------------------
DROP TABLE IF EXISTS `oms_cart_item`;
CREATE TABLE `oms_cart_item` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`product_id` bigint(20) DEFAULT NULL,
`product_sku_id` bigint(20) DEFAULT NULL,
`member_id` bigint(20) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL COMMENT '购买数量',
`price` decimal(10,2) DEFAULT NULL COMMENT '添加到购物车的价格',
`sp1` varchar(200) DEFAULT NULL COMMENT '销售属性1',
`sp2` varchar(200) DEFAULT NULL COMMENT '销售属性2',
`sp3` varchar(200) DEFAULT NULL COMMENT '销售属性3',
`product_pic` varchar(1000) DEFAULT NULL COMMENT '商品主图',
`product_name` varchar(500) DEFAULT NULL COMMENT '商品名称',
`product_sub_title` varchar(500) DEFAULT NULL COMMENT '商品副标题(卖点)',
`product_sku_code` varchar(200) DEFAULT NULL COMMENT '商品sku条码',
`member_nickname` varchar(500) DEFAULT NULL COMMENT '会员昵称',
`create_date` datetime DEFAULT NULL COMMENT '创建时间',
`modify_date` datetime DEFAULT NULL COMMENT '修改时间',
`delete_status` int(1) DEFAULT '0' COMMENT '是否删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='购物车表';

-- ----------------------------
-- Records of oms_cart_item
-- ----------------------------
INSERT INTO `oms_cart_item` VALUES ('3', '26', null, '1', '4', '3788.00', null, null, null, null, '华为 HUAWEI P20', 'AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待', null, 'windir', '2018-08-02 10:23:09', '2018-08-02 10:23:09', '1');
INSERT INTO `oms_cart_item` VALUES ('4', '26', null, '1', '2', '3788.00', '金色', '16G', null, null, '华为 HUAWEI P20', 'AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待', null, 'windir', '2018-08-02 10:23:09', '2018-08-02 10:23:09', '1');
INSERT INTO `oms_cart_item` VALUES ('5', '27', null, '1', '4', '2699.00', null, null, null, null, '小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待', '骁龙845处理器,红外人脸解锁,AI变焦双摄,AI语音助手小米6X低至1299,点击抢购', null, 'windir', '2018-08-02 10:23:09', '2018-08-02 10:23:09', '0');
INSERT INTO `oms_cart_item` VALUES ('6', '26', '86', '1', '2', '3788.00', '金色', '16G', null, null, '华为 HUAWEI P20', 'AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待', '201806070026001', 'windir', '2018-08-02 10:23:09', '2018-08-02 10:23:09', '1');
INSERT INTO `oms_cart_item` VALUES ('7', '26', '89', '1', '3', '3788.00', '银色', '32G', null, null, '华为 HUAWEI P20', 'AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待', '201806070026004', 'windir', '2018-08-02 10:23:09', '2018-08-02 10:23:09', '1');
INSERT INTO `oms_cart_item` VALUES ('8', '26', '88', '1', '4', '3788.00', '银色', '16G', null, null, '华为 HUAWEI P20', 'AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待手机 双卡双待', '201806070026003', 'windir', '2018-08-02 10:23:09', '2018-08-02 10:23:09', '0');

-- ----------------------------
-- Table structure for oms_company_address
-- ----------------------------
Expand Down Expand Up @@ -608,7 +643,7 @@ CREATE TABLE `pms_member_price` (
`member_price` decimal(10,2) DEFAULT NULL COMMENT '会员价格',
`member_level_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=181 DEFAULT CHARSET=utf8 COMMENT='商品会员价格表';
) ENGINE=InnoDB AUTO_INCREMENT=186 DEFAULT CHARSET=utf8 COMMENT='商品会员价格表';

-- ----------------------------
-- Records of pms_member_price
Expand Down Expand Up @@ -816,7 +851,7 @@ CREATE TABLE `pms_product_attribute_category` (
`attribute_count` int(11) DEFAULT '0' COMMENT '属性数量',
`param_count` int(11) DEFAULT '0' COMMENT '参数数量',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='产品属性分类表';
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='产品属性分类表';

-- ----------------------------
-- Records of pms_product_attribute_category
Expand All @@ -837,7 +872,7 @@ CREATE TABLE `pms_product_attribute_value` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`product_id` bigint(20) DEFAULT NULL,
`product_attribute_id` bigint(20) DEFAULT NULL,
`value` varchar(64) DEFAULT NULL COMMENT '存储的值',
`value` varchar(64) DEFAULT NULL COMMENT '手动添加规格或参数的值,参数单值,规格有多个时以逗号隔开',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=203 DEFAULT CHARSET=utf8 COMMENT='存储产品参数信息的表';

Expand Down Expand Up @@ -1489,12 +1524,16 @@ CREATE TABLE `ums_member` (
`growth` int(11) DEFAULT NULL COMMENT '成长值',
`luckey_count` int(11) DEFAULT NULL COMMENT '剩余抽奖次数',
`history_integration` int(11) DEFAULT NULL COMMENT '历史积分数量',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='会员表';
PRIMARY KEY (`id`),
UNIQUE KEY `idx_username` (`username`),
UNIQUE KEY `idx_phone` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='会员表';

-- ----------------------------
-- Records of ums_member
-- ----------------------------
INSERT INTO `ums_member` VALUES ('1', '4', 'test', '202cb962ac59075b964b07152d234b70', 'windir', '18061581849', '1', '2018-08-02 10:35:44', null, '1', '2009-06-01', '上海', '学生', 'test', null, null, null, null, null);
INSERT INTO `ums_member` VALUES ('3', '4', 'test1', '698d51a19d8a121ce581499d7b701668', null, '18061581848', '1', '2018-08-03 16:46:38', null, null, null, null, null, null, null, null, null, null, null);

-- ----------------------------
-- Table structure for ums_member_level
Expand Down
36 changes: 34 additions & 2 deletions mall-admin/pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.macro.mall</groupId>
<artifactId>mall-admin</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mall-admin</name>
<url>https://maven.apache.org</url>
<description>mall-admin project for mall</description>

<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -17,6 +21,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>

<dependencies>
Expand Down Expand Up @@ -87,6 +92,33 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>mall/${project.artifactId}:${project.version}</imageName>
<dockerHost>https://192.168.1.71:2375</dockerHost>
<baseImage>java:8</baseImage>
<entryPoint>["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
5 changes: 5 additions & 0 deletions mall-admin/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#===datasource start===
spring.datasource.url=jdbc:mysql:https://localhost:3306/mall
spring.datasource.username=root
spring.datasource.password=root
#===datasource end===
5 changes: 5 additions & 0 deletions mall-admin/src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#===datasource start===
spring.datasource.url=jdbc:mysql:https://db:3306/mall
spring.datasource.username=root
spring.datasource.password=root
#===datasource end===
7 changes: 2 additions & 5 deletions mall-admin/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#===datasource start===
spring.datasource.url=jdbc:mysql:https://localhost:3306/mall
spring.datasource.username=root
spring.datasource.password=root
#===datasource end===
#默认为开发环境
spring.profiles.active=dev

#===mybatis start===
mybatis.mapper-locations=classpath:dao/*.xml,classpath*:com/**/mapper/*.xml
Expand Down
12 changes: 7 additions & 5 deletions mall-mbg/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mall</artifactId>
<groupId>com.macro</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.macro.mall</groupId>
<artifactId>mall-mbg</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mall-mbg</name>
<description>mall-mbg project for mall</description>

<dependencies>
<!-- MyBatis 生成器 -->
<dependency>
Expand Down
Loading

0 comments on commit 8cb2050

Please sign in to comment.