使用 Docker Compose 一键安装 novel 项目开发环境


背景

novel 项目的技术栈比较多,安装完全部环境需要花大量时间,甚至有的小伙伴可能嫌麻烦就直接放弃了,为了解决这个问题,今天花了好几个小时整了个一键安装的教程,不仅可以一键安装所有 novel 项目技术栈所需的环境,而且也可以直接用于其它的项目中。为什么搞了这么长时间呢?主要是在 Elasticsearch 8 上面踩了一些坑,下面先看下效果:

安装步骤

  1. Docker Compose 安装。(除了第一步需要根据自己的平台去安装 Docker Compose 以外,其它步骤都一样)

在 Ubuntu 下执行如下的安装命令:

1
sudo apt install docker-compose -y

查看 Docker Compose 和 Docker 的版本信息:

1
2
3
4
5
docker-compose version

docker --version

docker version
  1. 创建 .env 文件,用来设置容器编排的环境变量。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# MYSQL 配置
MYSQL_VERSION=8.0
MYSQL_ROOT_PASSWORD=test123456

# Redis 配置
REDIS_VERSION=7.0
REDIS_PASSWORD=test123456

# RabbitMQ 配置
RABBITMQ_VERSION=3-management
RABBITMQ_DEFAULT_USER=xxyopen
RABBITMQ_DEFAULT_PASS=test123456
RABBITMQ_DEFAULT_VHOST=novel

# Elasticsearch 配置
ELASTIC_VERSION=8.6.2
# 'elastic' 账户的密码 (至少 6 个字符)
ELASTIC_PASSWORD=Fy2JWjJ1hcO2mi1USFL1
# 'kibana_system' 账号的密码 (至少 6 个字符)
KIBANA_PASSWORD=5JbbVsW9TkYcJu9Y9

# Kibana 配置
KIBANA_VERSION=8.6.2

# XXL-JOB 配置
XXLJOB_VERSION=2.3.1
XXLJOB_ACCESSTOKEN=123

  1. 创建 Docker Compose 的容器编排文件 docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
version: '3.9'

services:
novel-mysql:
container_name: novel-mysql
image: mysql:${MYSQL_VERSION}
restart: always
hostname: novel-mysql
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
volumes:
- "/data/docker/mysql/data:/var/lib/mysql"
- "/data/docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql"
command: mysqld --max_allowed_packet=100M
ports:
- "3306:3306"
networks:
- novelnet

novel-redis:
container_name: novel-redis
image: redis:${REDIS_VERSION}
restart: always
hostname: novel-redis
command: redis-server --save 60 1 --loglevel warning --requirepass "${REDIS_PASSWORD}"
ports:
- "6379:6379"
networks:
- novelnet

novel-rabbitmq:
container_name: novel-rabbitmq
image: rabbitmq:${RABBITMQ_VERSION}
restart: always
hostname: novel-rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS}
- RABBITMQ_DEFAULT_VHOST=${RABBITMQ_DEFAULT_VHOST}
ports:
- "15672:15672"
- "5672:5672"
networks:
- novelnet

novel-elasticsearch-setup:
container_name: novel-elasticsearch-setup
image: elasticsearch:${ELASTIC_VERSION}
hostname: novel-elasticsearch-setup
user: "0"
command: >
bash -c '
echo "Waiting for Elasticsearch availability";
until curl -s http://novel-elasticsearch:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
echo "Setting kibana_system password";
until curl -s -X POST -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" http://novel-elasticsearch:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
echo "All done!";
'
networks:
- novelnet

novel-elasticsearch:
container_name: novel-elasticsearch
image: elasticsearch:${ELASTIC_VERSION}
restart: always
hostname: novel-elasticsearch
environment:
- "ES_JAVA_OPTS=-Xms125m -Xmx512m"
- discovery.type=single-node
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
- KIBANA_PASSWORD=${KIBANA_PASSWORD}
- xpack.security.http.ssl.enabled=false
ports:
- "9200:9200"
depends_on:
- novel-elasticsearch-setup
networks:
- novelnet

novel-kibana:
container_name: novel-kibana
image: kibana:${KIBANA_VERSION}
restart: always
hostname: novel-kibana
environment:
- ELASTICSEARCH_HOSTS=http://novel-elasticsearch:9200
- ELASTICSEARCH_USERNAME=kibana_system
- ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
ports:
- "5601:5601"
depends_on:
- novel-elasticsearch
networks:
- novelnet

novel-xxl-job-admin:
container_name: novel-xxl-job-admin
image: xuxueli/xxl-job-admin:${XXLJOB_VERSION}
restart: always
hostname: novel-xxl-job-admin
environment:
- PARAMS=--spring.datasource.url=jdbc:mysql://novel-mysql:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=${MYSQL_ROOT_PASSWORD} --xxl.job.accessToken=${XXLJOB_ACCESSTOKEN}
- JAVA_OPTS=-Xmx512m
volumes:
- /data/docker/xxl-job-admin/data/applogs:/data/applogs
ports:
- "8080:8080"
depends_on:
- novel-mysql
networks:
- novelnet

networks:
novelnet:
driver: bridge

注意:Elasticsearch 容器挂载本地目录或文件时,需要修改目录或文件的读写权限,否则启动不成功。官方原文如下:

If you are bind-mounting a local directory or file, it must be readable by the elasticsearch user. In addition, this user must have write access to the data and log dirs. A good strategy is to grant group access to gid 1000 or 0 for the local directory.

For example, to prepare a local directory for storing data through a bind-mount:

1
2
3
mkdir esdatadir
chmod g+rwx esdatadir
chgrp 1000 esdatadir
  1. 上传数据库初始化文件 novel-cloud/doc/init.sql/data/docker/mysql 目录下。

  2. 在后台运行所有编排文件中的容器。

    1
    sudo docker-compose up -d
  3. 使用 .env 环境文件中配置的 elastic 账号密码来登录 kibana 控制台。

    1
    2
    elastic
    Fy2JWjJ1hcO2mi1USFL1

注意:需要将 xxl-job 的数据库文件导入 MySQL 后,xxl-job-admin 才能正常访问。