From ff65cdf3c163cadc17702c4f149267d79dece89a Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 24 Nov 2025 03:26:09 -0800 Subject: [PATCH] . --- .../identity-service/scripts/README.md | 235 ++++++++++++++++++ .../identity-service/scripts/health-check.sh | 29 ++- .../identity-service/scripts/start-all.sh | 61 +++++ 3 files changed, 318 insertions(+), 7 deletions(-) create mode 100644 backend/services/identity-service/scripts/README.md create mode 100644 backend/services/identity-service/scripts/start-all.sh diff --git a/backend/services/identity-service/scripts/README.md b/backend/services/identity-service/scripts/README.md new file mode 100644 index 00000000..73f4ac82 --- /dev/null +++ b/backend/services/identity-service/scripts/README.md @@ -0,0 +1,235 @@ +# 测试和健康检查脚本 + +## 使用流程 + +### 1️⃣ 启动 Redis(如果未启动) + +```bash +# 启动 Redis +redis-server --daemonize yes + +# 或者使用后台启动 +redis-server & +``` + +### 2️⃣ 启动 Identity Service + +```bash +# 在项目根目录 +npm run start:dev +``` + +### 3️⃣ 运行健康检查 + +```bash +# 进入 scripts 目录 +cd scripts + +# 运行健康检查 +./health-check.sh +``` + +**期望输出:** +``` +🏥 开始健康检查... + +=== 数据库服务 === +Checking PostgreSQL ... ✓ OK +=== 缓存服务 === +Checking Redis ... ✓ OK +=== 消息队列服务 === +Checking Kafka ... ✓ OK +=== 应用服务 === +Checking Identity Service ... ✓ OK +=== API 文档 === +Checking Swagger UI ... ✓ OK + +====================================== +健康检查完成! +正常: 5 +异常: 0 +====================================== +✓ 所有服务正常! + +现在可以运行测试: + ./scripts/quick-test.sh +``` + +### 4️⃣ 运行快速功能测试 + +```bash +./quick-test.sh +``` + +这个脚本会自动测试所有核心功能: +- ✅ 账户创建 +- ✅ 个人资料管理 +- ✅ 设备管理 +- ✅ Token 刷新 +- ✅ 助记词恢复 +- ✅ 推荐码查询 +- ✅ KYC 提交 +- ✅ 错误场景处理 + +--- + +## 如果健康检查失败 + +当 `health-check.sh` 显示服务异常时,它会给出修复建议: + +```bash +✗ 存在异常的服务! + +修复建议: + • Redis: redis-server --daemonize yes + • Identity Service: npm run start:dev + +或者运行一键启动脚本: + ./scripts/start-all.sh +``` + +### 手动修复步骤 + +#### Redis 未启动 +```bash +redis-server --daemonize yes +``` + +#### Identity Service 未启动 +```bash +# 在项目根目录 +npm run start:dev +``` + +#### PostgreSQL 未启动 +```bash +# Ubuntu/Debian +sudo systemctl start postgresql + +# macOS +brew services start postgresql + +# Windows +# 使用 PostgreSQL 服务管理器 +``` + +--- + +## 一键启动(推荐) + +如果想一次性启动所有服务: + +```bash +./scripts/start-all.sh +``` + +这个脚本会: +1. ✅ 启动 Redis(如果未运行) +2. ✅ 检查 PostgreSQL 状态 +3. ✅ 检查 Kafka 状态 +4. ✅ 启动 Identity Service +5. ✅ 等待服务就绪 +6. ✅ 自动运行健康检查 + +--- + +## 脚本说明 + +### `health-check.sh` +- **作用**: 检查所有依赖服务是否正常运行 +- **使用场景**: 部署前、调试时 +- **前置条件**: 所有服务需要已启动 + +### `quick-test.sh` +- **作用**: 快速测试所有核心 API 功能 +- **使用场景**: 验证功能完整性、回归测试 +- **前置条件**: `health-check.sh` 通过 + +### `start-all.sh` +- **作用**: 一键启动所有服务 +- **使用场景**: 初次启动、快速启动环境 +- **前置条件**: 依赖已安装(PostgreSQL、Redis、Kafka) + +--- + +## 常见问题 + +### Q: 为什么 `curl: command not found`? +**A:** 安装 curl +```bash +# Ubuntu/Debian +sudo apt-get install curl + +# macOS (通常已安装) +brew install curl +``` + +### Q: 为什么 `jq: command not found`? +**A:** `quick-test.sh` 需要 jq 来解析 JSON +```bash +# Ubuntu/Debian +sudo apt-get install jq + +# macOS +brew install jq +``` + +### Q: Redis 启动失败? +**A:** 检查是否已经在运行 +```bash +# 查看 Redis 进程 +ps aux | grep redis + +# 如果已运行,先停止 +redis-cli shutdown + +# 然后重新启动 +redis-server --daemonize yes +``` + +### Q: Identity Service 启动失败? +**A:** 检查日志 +```bash +# 查看启动日志 +npm run start:dev + +# 常见问题: +# 1. 端口 3000 已被占用 → 修改 PORT 环境变量 +# 2. 数据库连接失败 → 检查 .env 配置 +# 3. Redis 连接失败 → 确保 Redis 已启动 +``` + +--- + +## 完整测试流程 + +```bash +# 1. 进入项目目录 +cd ~/work/rwadurian/backend/services/identity-service + +# 2. 安装依赖(首次) +npm install + +# 3. 启动所有服务 +./scripts/start-all.sh + +# 4. 运行健康检查 +./scripts/health-check.sh + +# 5. 运行快速测试 +./scripts/quick-test.sh + +# 6. 运行完整测试 +npm test +npm run test:e2e +``` + +--- + +## 下一步 + +所有脚本通过后,可以进行: +- 📊 [集成测试](../test/integration-checklist.md) +- 🔒 [安全测试](../test/security-test-checklist.md) +- ⚡ [性能测试](../test/performance-test.md) +- 📝 [手动测试](../test/manual-test-scenarios.md) diff --git a/backend/services/identity-service/scripts/health-check.sh b/backend/services/identity-service/scripts/health-check.sh index d54193b7..976d2015 100644 --- a/backend/services/identity-service/scripts/health-check.sh +++ b/backend/services/identity-service/scripts/health-check.sh @@ -1,24 +1,25 @@ #!/bin/bash # 健康检查脚本 - 检查所有依赖服务是否正常 -set -e - echo "🏥 开始健康检查..." echo "" GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' +BLUE='\033[0;34m' NC='\033[0m' # 检查计数 PASS=0 FAIL=0 +FAILED_SERVICES=() # 检查函数 check_service() { local service_name=$1 local check_command=$2 + local fix_command=$3 echo -n "Checking $service_name ... " @@ -28,28 +29,29 @@ check_service() { else echo -e "${RED}✗ FAIL${NC}" FAIL=$((FAIL + 1)) + FAILED_SERVICES+=("$service_name:$fix_command") fi } # 检查 PostgreSQL echo -e "${YELLOW}=== 数据库服务 ===${NC}" -check_service "PostgreSQL" "pg_isready -h localhost -p 5432" +check_service "PostgreSQL" "pg_isready -h localhost -p 5432" "sudo systemctl start postgresql" # 检查 Redis echo -e "${YELLOW}=== 缓存服务 ===${NC}" -check_service "Redis" "redis-cli -h localhost -p 6379 ping" +check_service "Redis" "redis-cli -h localhost -p 6379 ping" "redis-server --daemonize yes" # 检查 Kafka echo -e "${YELLOW}=== 消息队列服务 ===${NC}" -check_service "Kafka" "nc -zv localhost 9092" +check_service "Kafka" "nc -zv localhost 9092" "启动 Kafka (需要手动启动)" # 检查应用服务 echo -e "${YELLOW}=== 应用服务 ===${NC}" -check_service "Identity Service" "curl -f http://localhost:3000/health" +check_service "Identity Service" "curl -f http://localhost:3000/health" "npm run start:dev" # 检查 Swagger 文档 echo -e "${YELLOW}=== API 文档 ===${NC}" -check_service "Swagger UI" "curl -f http://localhost:3000/api" +check_service "Swagger UI" "curl -f http://localhost:3000/api" "等待 Identity Service 启动" echo "" echo -e "${YELLOW}======================================${NC}" @@ -60,8 +62,21 @@ echo -e "${YELLOW}======================================${NC}" if [ $FAIL -eq 0 ]; then echo -e "${GREEN}✓ 所有服务正常!${NC}" + echo "" + echo -e "${BLUE}现在可以运行测试:${NC}" + echo " ./scripts/quick-test.sh" exit 0 else echo -e "${RED}✗ 存在异常的服务!${NC}" + echo "" + echo -e "${BLUE}修复建议:${NC}" + for service_info in "${FAILED_SERVICES[@]}"; do + service_name="${service_info%%:*}" + fix_command="${service_info#*:}" + echo -e "${YELLOW} • $service_name:${NC} $fix_command" + done + echo "" + echo -e "${BLUE}或者运行一键启动脚本:${NC}" + echo " ./scripts/start-all.sh" exit 1 fi diff --git a/backend/services/identity-service/scripts/start-all.sh b/backend/services/identity-service/scripts/start-all.sh new file mode 100644 index 00000000..5c9ebb7f --- /dev/null +++ b/backend/services/identity-service/scripts/start-all.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# 一键启动所有服务 + +set -e + +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +echo -e "${YELLOW}🚀 启动所有服务...${NC}" +echo "" + +# 1. 启动 Redis +echo -e "${YELLOW}启动 Redis...${NC}" +if ! pgrep -x "redis-server" > /dev/null; then + redis-server --daemonize yes + echo -e "${GREEN}✓ Redis 已启动${NC}" +else + echo -e "${GREEN}✓ Redis 已在运行${NC}" +fi + +# 2. 检查 PostgreSQL +echo -e "${YELLOW}检查 PostgreSQL...${NC}" +if pg_isready -h localhost -p 5432 > /dev/null 2>&1; then + echo -e "${GREEN}✓ PostgreSQL 已在运行${NC}" +else + echo -e "${YELLOW}⚠ PostgreSQL 未运行,请手动启动${NC}" +fi + +# 3. 检查 Kafka +echo -e "${YELLOW}检查 Kafka...${NC}" +if nc -zv localhost 9092 > /dev/null 2>&1; then + echo -e "${GREEN}✓ Kafka 已在运行${NC}" +else + echo -e "${YELLOW}⚠ Kafka 未运行,请手动启动${NC}" +fi + +# 4. 启动 Identity Service +echo -e "${YELLOW}启动 Identity Service...${NC}" +cd "$(dirname "$0")/.." +npm run start:dev & + +# 等待服务启动 +echo "等待服务启动 (最多 30 秒)..." +for i in {1..30}; do + if curl -f http://localhost:3000/health > /dev/null 2>&1; then + echo -e "${GREEN}✓ Identity Service 已启动${NC}" + break + fi + sleep 1 + echo -n "." +done + +echo "" +echo -e "${GREEN}✓ 所有服务已启动!${NC}" +echo "" +echo "运行健康检查:" +echo " ./scripts/health-check.sh" +echo "" +echo "运行快速测试:" +echo " ./scripts/quick-test.sh"