Skip to content

Commit

Permalink
fix: 完善部署脚本 (#49)
Browse files Browse the repository at this point in the history
* feat: 新增部署脚本

* feat: 新增部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本

* fix: 完善部署脚本
  • Loading branch information
BanTanger committed Aug 3, 2023
1 parent 9907298 commit 5d52647
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 152 deletions.
72 changes: 69 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,76 @@ im-system-whale-shark
* [x] 采用读扩散实现单聊、群聊离线消息拉取

## 快速开始
### 数据库环境

### 服务器方式部署

> 采用 docker compose 实现快速部署
首先要确保您的服务器上有 git、docker 以及 docker-compose

可参考自行下载: [服务器下载 Git、Docker、Docker-Compose](docker/docker.md)

1. 克隆本项目

```bash
git clone https://github.com/BanTanger/im-whale-shark.git
```
2. 项目打包

```bash
mvn clean package
```

此过程可能有点长,请耐心等待

3. 为脚本执行赋予权限并执行

```bash
chmod +x *.sh
```
```bash
./copy.sh
```
- 将各个模块的 jar 包和 Dockerfile 移动到 docker/build 包下

4. 部署项目

```bash
sh deploy.sh base
```
- 将基础组件部署到 docker
```bash
sh deploy.sh serives
```
- 将后端的三个模块部署到 docker

5. 开启防火墙
```bash
./open_port.sh
```

停用、删除:
```bash
sh deploy.sh stop
```
- 将 docker 的所有容器停用

```bash
sh deploy.sh rm
```
- 将 docker 的所有容器删除

清空 build 文件夹:
```bash
./clean.sh
```

### 本地方式部署

#### 数据库环境
导入 `whale-shark/assert/sql/im_core.sql` 文件

### Docker 环境部署
#### Docker 环境部署
**如果是部署到服务端,注意防火墙是否拦截端口**

redis:
Expand All @@ -73,7 +139,7 @@ docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq
```
+ 其中 15672 端口是连接 web 端页面的, 5672 端口是 Java 后端程序访问 rabbitmq 的

### 后端启动
#### 后端启动
后端有三个服务需要开启, 分别为:
+ im-tcp 包下的 Starter 程序 `com.bantanger.im.tcp.Starter`。它用于构建 TCP 网关服务, WebSocket、Socket 的连接, 消息发送, 回调以及路由等等基层操作。socket 的端口号是 `9001`, websocket 的端口号是 `19001`
+ im-domain 包下的 Application 程序 `com.bantanger.im.domain.Application`。它用于构建业务逻辑服务, 如用户、好友、群组的创建, 更改, 删除, 与数据库、缓存进行逻辑交互。端口号为 `8000`
Expand Down
271 changes: 135 additions & 136 deletions assert/sql/im_core.sql

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

echo "::: Welcome to IM-WhaleShark :::"

# 删除 build 文件夹
echo "开始清理 build 文件夹 .."
rm -rf docker/build/
echo "清理完成"
1 change: 1 addition & 0 deletions copy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

echo "::: Welcome to IM-WhaleShark :::"
# 创建目标目录
mkdir -p docker/build/domain/jar
mkdir -p docker/build/message-store/jar
Expand Down
29 changes: 25 additions & 4 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,49 @@

# 使用说明,用来提示输入参数
usage(){
echo "::: Welcome to IM-WhaleShark :::"
echo "Usage: sh 执行脚本.sh [base|services|stop|rm]"
exit 1
}

# Check if Docker is installed
if ! command -v docker >/dev/null 2>&1; then
echo "Error: Docker is not installed. Please install Docker before running this script."
exit 1
fi

# Check if Docker Compose is installed and set the appropriate command
if command -v docker-compose &> /dev/null
then
COMPOSE_COMMAND="docker-compose"
else
if command -v docker compose &> /dev/null
then
COMPOSE_COMMAND="docker compose"
else
echo "Error: Docker Compose is not installed. Please install Docker Compose before running this script."
exit 1
fi
fi

# 启动基础环境(必须)
base(){
docker compose up -d im-mysql im-redis im-zookeeper im-rabbitmq
$COMPOSE_COMMAND up -d im-mysql im-redis im-zookeeper im-rabbitmq
}

# 启动程序模块(必须)
services(){
docker compose up -d im-domain im-message-store im-tcp
$COMPOSE_COMMAND up -d im-domain im-message-store im-tcp
}

# 关闭所有环境/模块
stop(){
docker compose stop
$COMPOSE_COMMAND stop
}

# 删除所有环境/模块
rm(){
docker compose rm
$COMPOSE_COMMAND rm
}

# 根据输入参数,选择执行对应方法,不输入则执行使用说明
Expand Down
12 changes: 7 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ services:
container_name: im-mysql
image: mysql:8.0.11
build:
context: ./build/mysql
context: ./mysql
restart: always
privileged: true
volumes:
- ./docker/mysql/conf:/etc/mysql/conf.d
- ./docker/mysql/logs:/logs
Expand All @@ -29,10 +30,10 @@ services:
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
timeout: 20s
retries: 3
ports:
- "3306:3306"
- "13306:3306"
networks:
- im-network

Expand All @@ -41,13 +42,14 @@ services:
container_name: im-zookeeper
image: zookeeper:latest
restart: always
privileged: true
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=localhost:2888:3888
healthcheck:
test: ["CMD-SHELL", "zkOk=$(echo stat | nc localhost 2181 | grep Mode); if [ -z \"$zkOk\" ]; then exit 1; fi"]
interval: 10s
timeout: 5s
timeout: 20s
retries: 3
ports:
- "2181:2181"
Expand Down Expand Up @@ -75,7 +77,7 @@ services:
container_name: im-redis
image: redis:6.0.8
build:
context: ./build/redis
context: ./redis
restart: always
ports:
- "6379:6379"
Expand Down
53 changes: 53 additions & 0 deletions docker/docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 1. docker
## 安装
```bash
curl -sSL https://get.daocloud.io/docker | sh
```
或者
```bash
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
```
## 启动、停止

### systemctl 方式

重启docker服务

```
sudo systemctl restart docker
```

关闭docker

```
sudo systemctl stop docker
```

### service 方式

重启docker服务

```bash
sudo service docker restart
```

关闭docker

```bash
sudo service docker stop
```

# 2. docker-compose安装

```bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```

```bash
chmod +x /usr/local/bin/docker-compose
```

# 3. 安装git
```
ubuntu系统上执行 apt install git
```
8 changes: 4 additions & 4 deletions docker/mysql/db/im_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ CREATE TABLE im_message_body

CREATE TABLE im_message_history
(
app_id INT(20) NOT NULL COMMENT 'APP_ID',
from_id VARCHAR(50) NOT NULL COMMENT 'FROM_ID',
to_id VARCHAR(50) NOT NULL COMMENT 'TO_ID',
owner_id VARCHAR(50) NOT NULL COMMENT 'OWNER_ID',
app_id INT(20) NOT NULL COMMENT 'app_id',
from_id VARCHAR(50) NOT NULL COMMENT 'from_id',
to_id VARCHAR(50) NOT NULL COMMENT 'to_id',
owner_id VARCHAR(50) NOT NULL COMMENT 'owner_id',
message_key BIGINT(50) NOT NULL COMMENT 'messageBodyId',
create_time BIGINT NULL,
sequence BIGINT NULL,
Expand Down
52 changes: 52 additions & 0 deletions open_port.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

# 检查是否为root用户
if [ "$(id -u)" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi

echo "::: Welcome to IM-WhaleShark :::"

# 定义要开启的端口列表
ports=(3306 13306 6379 18000 18001 19001 19002 2181 2888 3888 5672 15672)

# 开启端口
open_ports() {
for port in "${ports[@]}"; do
echo "Opening port $port..."
firewall-cmd --zone=public --add-port=$port/tcp --permanent
done
echo "Reloading firewall..."
firewall-cmd --reload
echo "Ports opened successfully!"
}

# 关闭端口
close_ports() {
for port in "${ports[@]}"; do
echo "Closing port $port..."
firewall-cmd --zone=public --remove-port=$port/tcp --permanent
done
echo "Reloading firewall..."
firewall-cmd --reload
echo "Ports closed successfully!"
}

# 判断是否传入参数
if [[ $# -eq 0 ]]; then
echo "Usage: $0 [open|close]"
exit 1
fi

# 根据传入的参数调用相应的函数
if [[ $1 == "open" ]]; then
open_ports
elif [[ $1 == "close" ]]; then
close_ports
else
echo "Usage: $0 [open|close]"
exit 1
fi

exit 0

0 comments on commit 5d52647

Please sign in to comment.