Files
java-template/docker-manager.sh
T
2025-11-28 13:49:45 +08:00

279 lines
7.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# LJWX Docker Compose 管理脚本
# 提供便捷的服务管理命令
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查 Docker 和 Docker Compose
check_requirements() {
if ! command -v docker &> /dev/null; then
print_error "Docker 未安装,请先安装 Docker"
exit 1
fi
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
print_error "Docker Compose 未安装,请先安装 Docker Compose"
exit 1
fi
}
# 检查 .env 文件
check_env_file() {
if [ ! -f .env ]; then
print_warning ".env 文件不存在"
read -p "是否从 .env.example 创建 .env 文件? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
cp .env.example .env
print_success ".env 文件已创建,请根据需要修改配置"
print_info "编辑命令: vim .env 或 nano .env"
exit 0
else
print_warning "将使用默认配置启动服务"
fi
fi
}
# 启动所有服务
start_all() {
print_info "启动所有服务..."
docker-compose up -d
print_success "所有服务已启动"
show_status
}
# 启动基础设施服务
start_infra() {
print_info "启动基础设施服务(MySQL + Redis)..."
docker-compose up -d ljwx-mysql ljwx-redis
print_success "基础设施服务已启动"
}
# 启动业务应用服务
start_apps() {
print_info "启动业务应用服务..."
docker-compose up -d ljwx-boot ljwx-admin ljwx-bigscreen
print_success "业务应用服务已启动"
}
# 启动监控服务
start_monitoring() {
print_info "启动监控系统服务..."
docker-compose up -d ljwx-prometheus ljwx-alertmanager ljwx-loki ljwx-promtail ljwx-grafana ljwx-node-exporter
print_success "监控系统服务已启动"
}
# 停止所有服务
stop_all() {
print_info "停止所有服务..."
docker-compose stop
print_success "所有服务已停止"
}
# 停止并删除容器
down() {
print_warning "停止并删除所有容器..."
read -p "确认删除所有容器? 数据卷将保留 (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
docker-compose down
print_success "所有容器已删除,数据卷已保留"
else
print_info "操作已取消"
fi
}
# 完全清理(包括数据卷)
cleanup() {
print_error "警告:此操作将删除所有容器和数据卷!"
read -p "确认删除所有容器和数据? 此操作不可恢复! (yes/no) " -r
echo
if [[ $REPLY == "yes" ]]; then
docker-compose down -v
print_success "所有容器和数据卷已删除"
else
print_info "操作已取消"
fi
}
# 查看服务状态
show_status() {
print_info "服务状态:"
docker-compose ps
}
# 查看服务日志
show_logs() {
if [ -z "$1" ]; then
print_info "显示所有服务日志(Ctrl+C 退出)..."
docker-compose logs -f
else
print_info "显示 $1 服务日志(Ctrl+C 退出)..."
docker-compose logs -f "$1"
fi
}
# 重启服务
restart_service() {
if [ -z "$1" ]; then
print_info "重启所有服务..."
docker-compose restart
print_success "所有服务已重启"
else
print_info "重启 $1 服务..."
docker-compose restart "$1"
print_success "$1 服务已重启"
fi
}
# 显示访问地址
show_urls() {
print_info "服务访问地址:"
echo ""
echo "业务应用:"
echo " - 后端API: http://localhost:9998"
echo " - 管理界面: http://localhost:5173"
echo " - 大屏服务: http://localhost:5001"
echo ""
echo "基础设施:"
echo " - MySQL: localhost:3306"
echo " - Redis: localhost:6379"
echo ""
echo "监控系统:"
echo " - Grafana: http://localhost:3001 (admin/admin123)"
echo " - Prometheus: http://localhost:9091"
echo " - Alertmanager: http://localhost:9094"
echo " - Loki: http://localhost:3100"
echo " - Node Exporter: http://localhost:9101"
echo ""
}
# 健康检查
health_check() {
print_info "执行健康检查..."
services=("ljwx-mysql" "ljwx-redis" "ljwx-boot" "ljwx-admin" "ljwx-bigscreen")
for service in "${services[@]}"; do
if docker-compose ps | grep -q "$service.*Up"; then
print_success "$service: 运行中"
else
print_error "$service: 未运行"
fi
done
}
# 显示帮助信息
show_help() {
echo "LJWX Docker Compose 管理脚本"
echo ""
echo "用法: ./docker-manager.sh [命令] [选项]"
echo ""
echo "命令:"
echo " start 启动所有服务"
echo " start-infra 启动基础设施服务(MySQL + Redis)"
echo " start-apps 启动业务应用服务"
echo " start-monitor 启动监控系统服务"
echo " stop 停止所有服务"
echo " down 停止并删除所有容器(保留数据)"
echo " cleanup 完全清理(删除容器和数据卷)"
echo " restart [name] 重启服务(不指定则重启所有)"
echo " status 查看服务状态"
echo " logs [name] 查看服务日志(不指定则查看所有)"
echo " urls 显示服务访问地址"
echo " health 健康检查"
echo " help 显示此帮助信息"
echo ""
echo "示例:"
echo " ./docker-manager.sh start # 启动所有服务"
echo " ./docker-manager.sh logs ljwx-boot # 查看 ljwx-boot 日志"
echo " ./docker-manager.sh restart ljwx-admin # 重启 ljwx-admin 服务"
echo ""
}
# 主函数
main() {
check_requirements
case "${1:-help}" in
start)
check_env_file
start_all
show_urls
;;
start-infra)
check_env_file
start_infra
;;
start-apps)
check_env_file
start_apps
;;
start-monitor)
check_env_file
start_monitoring
;;
stop)
stop_all
;;
down)
down
;;
cleanup)
cleanup
;;
restart)
restart_service "$2"
;;
status)
show_status
;;
logs)
show_logs "$2"
;;
urls)
show_urls
;;
health)
health_check
;;
help|--help|-h)
show_help
;;
*)
print_error "未知命令: $1"
echo ""
show_help
exit 1
;;
esac
}
# 执行主函数
main "$@"