## 新增K8s配置文件 **Prometheus Operator集成**: - servicemonitor.yml: 自动发现ljwx-boot服务,30秒采集间隔 - prometheusrule.yml: 18条告警规则,覆盖P0-3/P0-4所有场景 - grafana-dashboard-configmap.yml: Dashboard导入配置 **应用配置**: - configmap.yml: 添加P0-3 Redis去重和P0-4缓存失效配置 - application.yml: 更新P0功能配置和Actuator暴露设置 **部署工具**: - deploy-p0.sh: 一键部署脚本,支持dry-run和健康检查 - P0-K8S-DEPLOYMENT.md: 完整K8s部署指南(18KB) ## Docker Compose开发环境 **混合模式配置**: - docker-compose-p0-dev.yml: 基础设施Docker化(MySQL/Redis/Prometheus/Grafana) - prometheus-p0-dev.yml: 使用host.docker.internal访问本地ljwx-boot - .env.p0: 环境变量配置模板 - verify-p0-dev.sh: 开发环境验证脚本 **完整模式配置**: - docker-compose-p0.yml: 包含ljwx-boot在内的完整Docker部署 - prometheus-p0.yml: Docker网络内部服务发现 ## 文档 **快速开始指南**: - K8S-P0-QUICK-START.md: K8s部署5分钟快速指南 - P0-DEV-QUICK-START.md: 本地开发环境快速启动指南 **监控Dashboard**: - grafana-dashboard-p0.json: 修复Dashboard JSON格式问题 ## 核心功能 ✅ **P0-3 Redis去重**: 分布式去重,24小时TTL ✅ **P0-4 缓存失效**: 事件驱动自动失效,1秒延迟 ✅ **完整监控**: 14个Grafana面板,18条告警规则 ✅ **多环境支持**: 本地开发、Docker Compose、K8s生产环境 ## 告警规则(6组18条) 1. 服务可用性 (2条): 服务不可用、高错误率 2. P0-3 Redis去重 (3条): Redis连接失败、内存告警、Key激增 3. P0-4 缓存失效 (3条): 功能禁用、失败率高、响应慢 4. JVM资源 (2条): 内存不足、GC时间长 5. 数据库连接池 (2条): 连接池耗尽、无空闲连接 6. 应用性能 (2条): API响应慢、并发数高 7. Pod资源 (3条): CPU/内存高、频繁重启 ## 部署方式 **K8s生产环境**: ```bash cd ljwx-boot/k8s ./deploy-p0.sh ``` **本地开发环境**: ```bash docker-compose -f docker-compose-p0-dev.yml up -d ./ljwx-boot/run-local.sh ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
273 lines
8.4 KiB
YAML
273 lines
8.4 KiB
YAML
# ============================================================================
|
|
# P0 监控完整环境 Docker Compose 配置
|
|
# 包含业务应用(ljwx-boot, ljwx-admin)和完整监控栈(Prometheus, Grafana)
|
|
# ============================================================================
|
|
version: '3.8'
|
|
|
|
networks:
|
|
ljwx-network:
|
|
driver: bridge
|
|
|
|
volumes:
|
|
mysql_data:
|
|
redis_data:
|
|
prometheus_data:
|
|
grafana_data:
|
|
|
|
services:
|
|
# ============================================================================
|
|
# 基础设施服务
|
|
# ============================================================================
|
|
|
|
# MySQL 数据库
|
|
ljwx-mysql:
|
|
image: mysql:8.0
|
|
container_name: ljwx-mysql
|
|
environment:
|
|
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
|
|
- MYSQL_DATABASE=${MYSQL_DATABASE}
|
|
- TZ=${TZ}
|
|
volumes:
|
|
- mysql_data:/var/lib/mysql
|
|
- ./sql:/docker-entrypoint-initdb.d:ro
|
|
ports:
|
|
- "${MYSQL_PORT}:3306"
|
|
networks:
|
|
- ljwx-network
|
|
restart: unless-stopped
|
|
healthcheck:
|
|
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
start_period: 30s
|
|
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
|
|
|
|
# Redis 缓存 (用于去重和缓存)
|
|
ljwx-redis:
|
|
image: redis:7-alpine
|
|
container_name: ljwx-redis
|
|
command: >
|
|
redis-server
|
|
--requirepass ${REDIS_PASSWORD}
|
|
--maxmemory ${REDIS_MAXMEMORY}
|
|
--maxmemory-policy allkeys-lru
|
|
--appendonly yes
|
|
volumes:
|
|
- redis_data:/data
|
|
ports:
|
|
- "${REDIS_PORT}:6379"
|
|
networks:
|
|
- ljwx-network
|
|
restart: unless-stopped
|
|
healthcheck:
|
|
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
|
|
interval: 10s
|
|
timeout: 3s
|
|
retries: 5
|
|
|
|
# Redis Exporter (导出Redis指标到Prometheus)
|
|
redis-exporter:
|
|
image: oliver006/redis_exporter:latest
|
|
container_name: redis-exporter
|
|
command:
|
|
- '--redis.addr=ljwx-redis:6379'
|
|
- '--redis.password=${REDIS_PASSWORD}'
|
|
ports:
|
|
- "${REDIS_EXPORTER_PORT}:9121"
|
|
networks:
|
|
- ljwx-network
|
|
depends_on:
|
|
ljwx-redis:
|
|
condition: service_healthy
|
|
restart: unless-stopped
|
|
|
|
# ============================================================================
|
|
# 业务应用服务
|
|
# ============================================================================
|
|
|
|
# ljwx-boot 后端服务
|
|
ljwx-boot:
|
|
build:
|
|
context: ./ljwx-boot
|
|
dockerfile: ljwx-boot-admin/Dockerfile
|
|
image: ljwx/boot:p0-latest
|
|
container_name: ljwx-boot
|
|
environment:
|
|
# Spring Boot 配置
|
|
- SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE}
|
|
- TZ=${TZ}
|
|
|
|
# MySQL 配置
|
|
- MYSQL_HOST=ljwx-mysql
|
|
- MYSQL_PORT=3306
|
|
- MYSQL_DATABASE=${MYSQL_DATABASE}
|
|
- MYSQL_USER=${MYSQL_USER}
|
|
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
|
|
|
|
# Redis 配置
|
|
- REDIS_HOST=ljwx-redis
|
|
- REDIS_PORT=6379
|
|
- REDIS_PASSWORD=${REDIS_PASSWORD}
|
|
- REDIS_DATABASE=${REDIS_DATABASE}
|
|
|
|
# P0-3 Redis去重配置
|
|
- HEALTH_DEDUP_USE_REDIS=${HEALTH_DEDUP_USE_REDIS}
|
|
- HEALTH_DEDUP_TTL_HOURS=${HEALTH_DEDUP_TTL_HOURS}
|
|
- HEALTH_DEDUP_USE_CONTENT_HASH=${HEALTH_DEDUP_USE_CONTENT_HASH}
|
|
|
|
# P0-4 缓存失效配置
|
|
- HEALTH_CACHE_AUTO_INVALIDATION=${HEALTH_CACHE_AUTO_INVALIDATION}
|
|
- HEALTH_CACHE_INVALIDATION_DELAY_MS=${HEALTH_CACHE_INVALIDATION_DELAY_MS}
|
|
|
|
# Prometheus Metrics 配置
|
|
- MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=${MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE}
|
|
- MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED=${MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED}
|
|
ports:
|
|
- "${LJWX_BOOT_PORT}:9998"
|
|
depends_on:
|
|
ljwx-mysql:
|
|
condition: service_healthy
|
|
ljwx-redis:
|
|
condition: service_healthy
|
|
networks:
|
|
- ljwx-network
|
|
restart: unless-stopped
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-q", "--spider", "http://localhost:9998/actuator/health"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 5
|
|
start_period: 120s
|
|
|
|
# ljwx-admin 前端管理界面
|
|
ljwx-admin:
|
|
build:
|
|
context: ./ljwx-admin
|
|
dockerfile: Dockerfile
|
|
args:
|
|
- VITE_API_BASE_URL=http://ljwx-boot:9998
|
|
image: ljwx/admin:p0-latest
|
|
container_name: ljwx-admin
|
|
environment:
|
|
- NODE_ENV=${NODE_ENV}
|
|
- VITE_USER_NODE_ENV=${VITE_USER_NODE_ENV}
|
|
- TZ=${TZ}
|
|
ports:
|
|
- "${LJWX_ADMIN_PORT}:5173"
|
|
depends_on:
|
|
ljwx-boot:
|
|
condition: service_healthy
|
|
networks:
|
|
- ljwx-network
|
|
restart: unless-stopped
|
|
|
|
# ============================================================================
|
|
# 监控系统服务
|
|
# ============================================================================
|
|
|
|
# Prometheus 指标收集和存储
|
|
prometheus:
|
|
image: prom/prometheus:latest
|
|
container_name: prometheus
|
|
volumes:
|
|
- ./ljwx-boot/monitoring/prometheus-p0.yml:/etc/prometheus/prometheus.yml:ro
|
|
- ./ljwx-boot/monitoring/prometheus-alerts-p0.yml:/etc/prometheus/alerts/p0-alerts.yml:ro
|
|
- prometheus_data:/prometheus
|
|
command:
|
|
- '--config.file=/etc/prometheus/prometheus.yml'
|
|
- '--storage.tsdb.path=/prometheus'
|
|
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
|
|
- '--web.console.templates=/usr/share/prometheus/consoles'
|
|
- '--web.enable-lifecycle'
|
|
- '--storage.tsdb.retention.time=30d'
|
|
ports:
|
|
- "${PROMETHEUS_PORT}:9090"
|
|
networks:
|
|
- ljwx-network
|
|
depends_on:
|
|
ljwx-boot:
|
|
condition: service_healthy
|
|
redis-exporter:
|
|
condition: service_started
|
|
restart: unless-stopped
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-q", "--spider", "http://localhost:9090/-/healthy"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
|
|
# Grafana 可视化面板
|
|
grafana:
|
|
image: grafana/grafana:latest
|
|
container_name: grafana
|
|
volumes:
|
|
- ./ljwx-boot/monitoring/grafana/provisioning:/etc/grafana/provisioning:ro
|
|
- ./ljwx-boot/monitoring:/var/lib/grafana/dashboards:ro
|
|
- grafana_data:/var/lib/grafana
|
|
environment:
|
|
- GF_SECURITY_ADMIN_USER=${GRAFANA_ADMIN_USER}
|
|
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD}
|
|
- GF_USERS_ALLOW_SIGN_UP=false
|
|
- GF_SERVER_ROOT_URL=http://localhost:${GRAFANA_PORT}
|
|
- GF_INSTALL_PLUGINS=grafana-piechart-panel
|
|
- TZ=${TZ}
|
|
ports:
|
|
- "${GRAFANA_PORT}:3000"
|
|
networks:
|
|
- ljwx-network
|
|
depends_on:
|
|
prometheus:
|
|
condition: service_healthy
|
|
restart: unless-stopped
|
|
|
|
# ============================================================================
|
|
# 使用说明
|
|
# ============================================================================
|
|
#
|
|
# 1. 配置环境变量:
|
|
# cp .env.p0 .env
|
|
# # 根据需要修改 .env 中的端口配置
|
|
#
|
|
# 2. 构建并启动所有服务:
|
|
# docker-compose -f docker-compose-p0.yml up -d --build
|
|
#
|
|
# 3. 查看服务状态:
|
|
# docker-compose -f docker-compose-p0.yml ps
|
|
#
|
|
# 4. 查看服务日志:
|
|
# docker-compose -f docker-compose-p0.yml logs -f [service-name]
|
|
#
|
|
# 5. 快速验证:
|
|
# # 等待所有服务启动完成 (约2-3分钟)
|
|
# docker-compose -f docker-compose-p0.yml exec ljwx-boot wget -q -O- http://localhost:9998/actuator/health
|
|
#
|
|
# 6. 停止所有服务:
|
|
# docker-compose -f docker-compose-p0.yml down
|
|
#
|
|
# 7. 停止并删除数据卷 (⚠️ 会删除所有数据):
|
|
# docker-compose -f docker-compose-p0.yml down -v
|
|
#
|
|
# ============================================================================
|
|
# 服务访问地址 (默认端口)
|
|
# ============================================================================
|
|
#
|
|
# 业务应用:
|
|
# - ljwx-boot 后端API: http://localhost:9998
|
|
# - ljwx-boot API文档: http://localhost:9998/doc.html
|
|
# - ljwx-boot 健康检查: http://localhost:9998/actuator/health
|
|
# - ljwx-boot Metrics: http://localhost:9998/actuator/prometheus
|
|
# - ljwx-admin 前端: http://localhost:5173
|
|
#
|
|
# 监控系统:
|
|
# - Grafana Dashboard: http://localhost:3000 (admin/admin123)
|
|
# - Prometheus: http://localhost:9090
|
|
# - Prometheus Targets: http://localhost:9090/targets
|
|
# - Prometheus Alerts: http://localhost:9090/alerts
|
|
#
|
|
# 基础设施:
|
|
# - MySQL: localhost:3306 (root/123456, database: test)
|
|
# - Redis: localhost:6379 (password: 123456, db: 1)
|
|
# - Redis Exporter: http://localhost:9121/metrics
|
|
#
|