<# .SYNOPSIS 风七六平台快速启动脚本 .DESCRIPTION 提供常用的一键操作: - 启动开发环境 - 停止所有服务 - 查看服务状态 - 查看日志 .PARAMETER Action 操作类型:start/stop/status/logs/clean .PARAMETER Environment 环境:dev/test/prod .EXAMPLE # 启动开发环境 .\start.ps1 start # 停止所有服务 .\start.ps1 stop # 查看服务状态 .\start.ps1 status # 查看网关日志 .\start.ps1 logs api-gateway #> param( [string]$Action = "start", [string]$Environment = "dev" ) # 环境映射表 - 支持本地和阿里云环境 # 命名规范: local-dev(本地开发), aliyun-dev(阿里云开发), test(测试), prod(生产) $envMapping = @{ "local-dev" = @{ ComposeFile = "podman-compose.local-dev.yml"; Name = "本地开发环境" } "aliyun-dev" = @{ ComposeFile = "podman-compose.aliyun-dev.yml"; Name = "阿里云开发环境" } "test" = @{ ComposeFile = "podman-compose.test.yml"; Name = "测试环境" } "prod" = @{ ComposeFile = "podman-compose.prod.yml"; Name = "生产环境" } } $projectRoot = $PWD.Path function Start-Environment { if (-not $envMapping.ContainsKey($Environment)) { Write-Host "❌ 不支持的环境: $Environment" -ForegroundColor Red Write-Host "可用环境: $($envMapping.Keys -join ', ')" -ForegroundColor Yellow exit 1 } $envConfig = $envMapping[$Environment] Write-Host "🚀 启动 $($envConfig.Name)..." -ForegroundColor Cyan $composePath = Join-Path $projectRoot "infrastructure" "compose" $composeFile = Join-Path $composePath $envConfig.ComposeFile if (Test-Path $composeFile) { # 保持在项目根目录执行,确保compose中的相对路径正确 Set-Location $projectRoot $env:COMPOSE_FILE = $composeFile # 加载对应环境的环境变量文件 $envFile = Join-Path $composePath ".env.$Environment" if (Test-Path $envFile) { Write-Host "📄 加载环境变量: $envFile" -ForegroundColor Yellow foreach ($line in Get-Content $envFile) { if ($line -match '^([^#=]+)=(.+)$') { [Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } } } podman-compose -f $composeFile up -d if ($LASTEXITCODE -eq 0) { Write-Host "✅ $($envConfig.Name)启动成功" -ForegroundColor Green Write-Host "`n服务启动中,请稍等..." -ForegroundColor Yellow Start-Sleep -Seconds 15 Show-Status } else { Write-Host "❌ $($envConfig.Name)启动失败" -ForegroundColor Red exit 1 } } else { Write-Host "❌ compose 文件不存在: $composeFile" -ForegroundColor Red exit 1 } } function Stop-Environment { if (-not $envMapping.ContainsKey($Environment)) { Write-Host "❌ 不支持的环境: $Environment" -ForegroundColor Red exit 1 } $envConfig = $envMapping[$Environment] Write-Host "⏹️ 停止 $($envConfig.Name)..." -ForegroundColor Cyan $composePath = Join-Path $projectRoot "infrastructure" "compose" $composeFile = $envConfig.ComposeFile if (Test-Path (Join-Path $composePath $composeFile)) { Set-Location $composePath $env:COMPOSE_FILE = $composeFile podman-compose down if ($LASTEXITCODE -eq 0) { Write-Host "✅ $($envConfig.Name)停止成功" -ForegroundColor Green } else { Write-Host "❌ $($envConfig.Name)停止失败" -ForegroundColor Red exit 1 } } else { Write-Host "❌ compose 文件不存在: $composeFile" -ForegroundColor Red exit 1 } } function Show-Status { Write-Host "`n📊 服务状态:" -ForegroundColor Cyan # 检查容器状态 Write-Host "`n[容器状态]" -ForegroundColor Yellow podman ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" # 检查Nacos注册 Write-Host "`n[Nacos 服务注册]" -ForegroundColor Yellow try { $response = Invoke-WebRequest -Uri "http://localhost:8848/nacos/v1/ns/service/list?pageNo=1&pageSize=20" -Method Get -TimeoutSec 5 $services = ($response.Content | ConvertFrom-Json).data.serviceList if ($services) { foreach ($service in $services) { Write-Host "✅ $($service.name) - 实例数: $($service.clusters)" -ForegroundColor Green } } else { Write-Host "⚠️ 暂无服务注册" -ForegroundColor Yellow } } catch { Write-Host "⚠️ Nacos 服务未就绪" -ForegroundColor Yellow } # 检查API网关 Write-Host "`n[API Gateway]" -ForegroundColor Yellow try { $response = Invoke-WebRequest -Uri "http://localhost:8089/api/health" -Method Get -TimeoutSec 5 if ($response.StatusCode -eq 200) { Write-Host "✅ API Gateway 健康" -ForegroundColor Green } } catch { Write-Host "⚠️ API Gateway 未就绪" -ForegroundColor Yellow } } function Show-Logs { param([string]$ServiceName) Write-Host "📝 查看 $ServiceName 日志..." -ForegroundColor Cyan podman logs -f $ServiceName } function Clean-Environment { if (-not $envMapping.ContainsKey($Environment)) { Write-Host "❌ 不支持的环境: $Environment" -ForegroundColor Red exit 1 } $envConfig = $envMapping[$Environment] Write-Host "🧹 清理 $($envConfig.Name)..." -ForegroundColor Cyan # 停止容器 $composePath = Join-Path $projectRoot "infrastructure" "compose" $composeFile = $envConfig.ComposeFile if (Test-Path (Join-Path $composePath $composeFile)) { Set-Location $composePath $env:COMPOSE_FILE = $composeFile podman-compose down -v # 清理未使用的镜像 podman image prune -f Write-Host "✅ $($envConfig.Name)清理完成" -ForegroundColor Green } else { Write-Host "❌ compose 文件不存在: $composeFile" -ForegroundColor Red exit 1 } } switch ($Action.ToLower()) { "start" { Start-Environment } "stop" { Stop-Environment } "status" { Show-Status } "logs" { Show-Logs -ServiceName $Environment } "clean" { Clean-Environment } default { Write-Host "❌ 未知操作: $Action" -ForegroundColor Red Write-Host "可用操作: start, stop, status, logs, clean" -ForegroundColor Yellow exit 1 } }