Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yuana1 master #321

Merged
merged 8 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

/litemall/litemall.jar
/db/init-sql/litemall.sql
106 changes: 106 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
## docker

### 项目打包

1. 在服务器或者开发机打包项目到docker;
```
cd litemall
cat ./litemall-db/sql/litemall_schema.sql > ./docker/db/init-sql/litemall.sql
cat ./litemall-db/sql/litemall_table.sql >> ./docker/db/init-sql/litemall.sql
cat ./litemall-db/sql/litemall_data.sql >> ./docker/db/init-sql/litemall.sql

cd ./litemall-admin
cnpm install
cnpm run build:dep

cd ..
mvn clean package
cp -f ./litemall-all/target/litemall-all-*-exec.jar ./docker/litemall/litemall.jar
```
这里的工作是:
1. 把数据库文件拷贝到docker/db文件夹
2. 编译litemall-admin项目
3. 编译litemall-all模块,同时把litemall-admin编译得到的静态文件拷贝到
litemall-all模块的static目录

2. 修改litemall文件夹下面的*.yml外部配置文件,当litemall-all模块启动时会
加载外部配置文件,而覆盖默认jar包内部的配置文件。
例如,配置文件中一些地方需要设置成远程服务器的IP地址

此时docker部署包结构如下:

* bin

存放远程服务器运行的脚本,包括deploy.sh脚本和reset.sh脚本

* db

存放litemall数据库文件

* litemall

存放远程服务器运行的代码,包括litemall-all二进制可执行包和litemall外部配置文件

* util

存放开发服务器运行的脚本,包括package.sh脚本和lazy.sh脚本。
由于是本地开发服务器运行,因此开发者可以不用上传到远程服务器。

* docker-compose.yml

docker-compose配置脚本,运行docker-compose命令会

### 项目部署

1. 云服务器环境安装docker和docker-compose(MySQL和JDK1.8无需安装,因为使用docker自动安装)。
此外请确保云服务器的安全组已经允许相应的端口。

2. 运行docker-compose
```bash
cd /home/ubuntu/decker
sudo docker-compose
```

3. 测试是否部署成功(xxx.xxx.xxx.xxx是云服务器IP):
```
http://xxx.xxx.xxx.xxx:8080/wx/index/index
http://xxx.xxx.xxx.xxx:8080/admin/index/index
http://xxx.xxx.xxx.xxx:8080/#/login
```

### 项目辅助脚本

在前面的项目打包和项目部署中都是采用手动命令来部署。
这里可以写一些脚本简化:

* util/packet.sh

在开发服务器运行可以自动项目打包

* util/lazy.sh

在开发服务器运行可以自动项目打包、项目上传远程服务器、自动登录系统执行项目部署脚本。

注意:
> 1. 开发者需要在util/lazy.sh中设置相应的远程服务器登录账号和密钥文件路径。
> 2. 开发者需要在bin/reset.sh设置远程服务器的MySQL的root登录账户。

* bin/deploy.sh

在远程服务器运行可以自动部署服务

* bin/reset.sh

在远程服务器运行可以自动项目导入数据、删除本地上传图片、再执行bin/deploy.sh部署服务。

注意:
> 开发者需要在bin/reset.sh设置远程服务器的MySQL的root登录账户。

总结,当开发者设置好配置信息以后,可以在本地运行lazy.sh脚本自动一键部署:
```bash
cd litemall
./docker/util/lazy.sh
```

不过由于需要设置的信息会包含敏感安全信息,强烈建议开发者参考这里的docker文件夹,
然后实现自己的docker文件夹,妥善处置外部配置文件和脚本中的敏感安全信息!!!
6 changes: 6 additions & 0 deletions docker/db/conf.d/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mysqld]
wait_timeout=1814400
max_allowed_packet = 100M
default-time_zone = '+8:00'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
Empty file added docker/db/data/.gitkeep
Empty file.
Empty file added docker/db/init-sql/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3'
services:
mysql57:
image: mysql:5.7
container_name: mysql
ports:
- "3306:3306"
command:
--character-set-server=utf8
--collation-server=utf8_general_ci
--default-authentication-plugin=mysql_native_password
volumes:
- ./db/conf.d:/etc/mysql/conf.d
- ./db/data:/var/lib/mysql
- ./db/init-sql:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: litemall123456
restart: always
litemall:
build:
context: litemall
dockerfile: Dockerfile
container_name: litemall
ports:
- "8080:8080"
volumes:
- ./litemall/storage:/storage
- ./litemall/logs:/logs
- /etc/localtime:/etc/localtime
depends_on:
- mysql57
restart: always
Empty file added docker/litemall/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions docker/litemall/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:8-jre
COPY application.yml /litemall/application.yml
COPY litemall.jar /litemall/litemall.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","litemall.jar"]
145 changes: 145 additions & 0 deletions docker/litemall/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
spring:
profiles:
active: none
message:
encoding: UTF-8
datasource:
druid:
url: jdbc:mysql://mysql:3306/litemall?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: litemall
password: litemall123456
initial-size: 10
max-active: 50
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 1 FROM DUAL
test-on-borrow: false
test-on-return: false
test-while-idle: true
time-between-eviction-runs-millis: 60000
filters: stat,wall

server:
port: 8080

logging:
config: classpath:logback-spring.xml

pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql

litemall:
# 开发者应该设置成自己的wx相关信息
wx:
app-id: wxa5b486c6b918ecfb
app-secret: e04004829d4c383b4db7769d88dfbca1
mch-id: 111111
mch-key: xxxxxx
notify-url: http://122.51.199.160:8080/wx/order/pay-notify
# 商户证书文件路径
# 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
key-path: xxxxx

#通知相关配置
notify:
mail:
# 邮件通知配置,邮箱一般用于接收业务通知例如收到新的订单,sendto 定义邮件接收者,通常为商城运营人员
enable: false
host: smtp.exmail.qq.com
username: [email protected]
password: XXXXXXXXXXXXX
sendfrom: [email protected]
sendto: [email protected]
port: 465

# 短消息模版通知配置
# 短信息用于通知客户,例如发货短信通知,注意配置格式;template-name,template-templateId 请参考 NotifyType 枚举值
sms:
enable: false
# 如果是腾讯云短信,则设置active的值tencent
# 如果是阿里云短信,则设置active的值aliyun
active: tencent
sign: litemall
template:
- name: paySucceed
templateId: 156349
- name: captcha
templateId: 156433
- name: ship
templateId: 158002
- name: refund
templateId: 159447
tencent:
appid: 111111111
appkey: xxxxxxxxxxxxxx
aliyun:
regionId: xxx
accessKeyId: xxx
accessKeySecret: xxx

# 快鸟物流查询配置
express:
enable: false
appId: "XXXXXXXXX"
appKey: "XXXXXXXXXXXXXXXXXXXXXXXXX"
vendors:
- code: "ZTO"
name: "中通快递"
- code: "YTO"
name: "圆通速递"
- code: "YD"
name: "韵达速递"
- code: "YZPY"
name: "邮政快递包裹"
- code: "EMS"
name: "EMS"
- code: "DBL"
name: "德邦快递"
- code: "FAST"
name: "快捷快递"
- code: "ZJS"
name: "宅急送"
- code: "TNT"
name: "TNT快递"
- code: "UPS"
name: "UPS"
- code: "DHL"
name: "DHL"
- code: "FEDEX"
name: "FEDEX联邦(国内件)"
- code: "FEDEX_GJ"
name: "FEDEX联邦(国际件)"

# 对象存储配置
storage:
# 当前工作的对象存储模式,分别是local、aliyun、tencent
active: local
# 本地对象存储配置信息
local:
storagePath: storage
address: http://122.51.199.160:8080/wx/storage/fetch/
# 阿里云对象存储配置信息
aliyun:
endpoint: oss-cn-shenzhen.aliyuncs.com
accessKeyId: 111111
accessKeySecret: xxxxxx
bucketName: xxxxxx
# 腾讯对象存储配置信息
# 请参考 https://cloud.tencent.com/document/product/436/6249
tencent:
secretId: 111111
secretKey: xxxxxx
region: xxxxxx
bucketName: xxxxxx
# 七牛云对象存储配置信息
qiniu:
endpoint: http://pd5cb6ulu.bkt.clouddn.com
accessKey: 111111
secretKey: xxxxxx
bucketName: litemall
45 changes: 45 additions & 0 deletions docker/util/lazy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# 本脚本的作用是
# 1. 项目打包
# 2. 上传云服务器
# 3. 远程登录云服务器并执行reset脚本

# 请设置云服务器的IP地址和账户
# 例如 [email protected]
REMOTE=
# 请设置本地SSH私钥文件id_rsa路径
# 例如 /home/litemall/id_rsa
ID_RSA=

if test -z "$REMOTE"
then
echo "请设置云服务器登录IP地址和账户"
exit 1
fi

if test -z "$ID_RSA"
then
echo "请设置云服务器登录IP地址和账户"
exit 1
fi

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
cd $DIR/../.. || exit 2
LITEMALL_HOME=$PWD
echo "LITEMALL_HOME $LITEMALL_HOME"

# 项目打包
cd $LITEMALL_HOME || exit 2
./docker/util/package.sh

# 上传云服务器
cd $LITEMALL_HOME || exit 2
scp -i $ID_RSA -r ./docker $REMOTE:/home/ubuntu/

# 远程登录云服务器并执行reset脚本
#ssh $REMOTE -i $ID_RSA << eeooff
#cd /home/ubuntu
#sudo ./docker/bin/reset.sh
#exit
#eeooff
28 changes: 28 additions & 0 deletions docker/util/package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# 请注意
# 本脚本的作用是把本项目编译的结果保存到deploy文件夹中
# 1. 把项目数据库文件拷贝到docker/db/init-sql
# 2. 编译litemall-admin
# 3. 编译litemall-all模块,然后拷贝到docker/litemall

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
cd $DIR/../..
LITEMALL_HOME=$PWD
echo "LITEMALL_HOME $LITEMALL_HOME"

# 复制数据库
cat $LITEMALL_HOME/litemall-db/sql/litemall_schema.sql > $LITEMALL_HOME/docker/db/init-sql/litemall.sql
cat $LITEMALL_HOME/litemall-db/sql/litemall_table.sql >> $LITEMALL_HOME/docker/db/init-sql/litemall.sql
cat $LITEMALL_HOME/litemall-db/sql/litemall_data.sql >> $LITEMALL_HOME/docker/db/init-sql/litemall.sql

cd $LITEMALL_HOME/litemall-admin
# 安装阿里node镜像工具
npm install -g cnpm --registry=https://registry.npm.taobao.org
# 安装node项目依赖环境
cnpm install
cnpm run build:dep

cd $LITEMALL_HOME
mvn clean package
cp -f $LITEMALL_HOME/litemall-all/target/litemall-all-*-exec.jar $LITEMALL_HOME/docker/litemall/litemall.jar