<# .SYNOPSIS 启动阿里云开发环境服务 - 符合大厂标准的启动脚本 .DESCRIPTION 功能: 1. 加载环境配置文件 2. 从环境变量读取敏感配置(符合安全规范) 3. 分阶段启动基础设施和业务服务 4. 支持健康检查验证 .PARAMETER EnvFile 环境配置文件路径 .PARAMETER SkipLogin 是否跳过镜像仓库登录 .EXAMPLE # 使用默认配置启动 .\scripts\start-aliyun-env.ps1 # 指定配置文件 .\scripts\start-aliyun-env.ps1 -EnvFile .\infrastructure\compose\.env.aliyun # 跳过登录(已登录状态) .\scripts\start-aliyun-env.ps1 -SkipLogin #> param( [string]$EnvFile = "$PSScriptRoot\..\infrastructure\compose\.env.aliyun", [switch]$SkipLogin = $false ) # 初始化颜色输出 $cyan = [ConsoleColor]::Cyan $yellow = [ConsoleColor]::Yellow $green = [ConsoleColor]::Green $red = [ConsoleColor]::Red function Write-Info { param([string]$Message) Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $Message" -ForegroundColor $cyan } function Write-Step { param([string]$Message) Write-Host "`n$Message" -ForegroundColor $yellow } function Write-Success { param([string]$Message) Write-Host "✅ $Message" -ForegroundColor $green } function Write-Error { param([string]$Message) Write-Host "❌ $Message" -ForegroundColor $red } function Test-Command { param([string]$Command) return Get-Command $Command -ErrorAction SilentlyContinue } #region === 前置检查 === Write-Info "=== 阿里云开发环境启动脚本 ===" Write-Step "1. 前置检查" # 检查必要工具 $requiredTools = @("podman", "podman-compose") foreach ($tool in $requiredTools) { if (-not (Test-Command $tool)) { Write-Error "$tool 未安装或不在PATH中" exit 1 } } Write-Success "所有必要工具已就绪" # 加载环境配置 if (Test-Path $EnvFile) { Write-Info "加载环境配置: $EnvFile" $configContent = Get-Content $EnvFile -Raw $configContent | Select-String -Pattern '^[A-Z_]+=' | ForEach-Object { $line = $_.Line.Trim() if ($line -match '^([A-Z_]+)=(.*)$') { $name = $matches[1] $value = $matches[2] -replace '"', '' if (-not (Get-Variable -Name "env:$name" -ErrorAction SilentlyContinue)) { Set-Variable -Name "env:$name" -Value $value } } } Write-Success "环境配置加载完成" } else { Write-Error "环境配置文件不存在: $EnvFile" exit 1 } #endregion #region === 登录镜像仓库 === if (-not $SkipLogin) { Write-Step "2. 登录私有镜像仓库" $registryHost = $env:REGISTRY_HOST ? $env:REGISTRY_HOST : "registry.fengzhongxing.com:5000" $registryUser = $env:REGISTRY_USERNAME ? $env:REGISTRY_USERNAME : "fzx" $registryPass = $env:REGISTRY_PASSWORD if (-not $registryPass) { Write-Error "镜像仓库密码未配置(REGISTRY_PASSWORD)" exit 1 } Write-Info "登录 $registryHost..." podman login --tls-verify=false -u $registryUser -p $registryPass $registryHost if ($LASTEXITCODE -eq 0) { Write-Success "镜像仓库登录成功" } else { Write-Error "镜像仓库登录失败" exit 1 } } #endregion #region === 启动基础设施服务 === Write-Step "3. 启动基础设施服务" $networkName = $env:NETWORK_NAME ? $env:NETWORK_NAME : "fzx-aliyun-dev-network" # 检查网络是否存在 if (-not (podman network ls | Select-String $networkName)) { Write-Info "创建网络: $networkName" podman network create $networkName } # 启动基础设施服务 $infraServices = @("mariadb", "redis", "nacos") foreach ($service in $infraServices) { Write-Info "启动 $service..." $composeCmd = "podman-compose --env-file $EnvFile -f infrastructure/compose/podman-compose.aliyun-dev.yml up -d $service" Invoke-Expression $composeCmd if ($LASTEXITCODE -eq 0) { Write-Success "$service 启动成功" } else { Write-Error "$service 启动失败" } } #endregion #region === 等待基础设施就绪 === Write-Step "4. 等待基础设施就绪" $waitSeconds = 45 Write-Info "等待 ${waitSeconds}秒..." Start-Sleep -Seconds $waitSeconds # 检查基础设施状态 Write-Info "检查基础设施健康状态..." $infraContainers = @("fzx-aliyun-mariadb", "fzx-aliyun-redis", "fzx-aliyun-nacos") $allReady = $true foreach ($container in $infraContainers) { $status = podman inspect $container --format '{{.State.Status}}' 2>&1 if ($status -eq "running") { Write-Success "$container 运行中" } else { Write-Error "$container 未正常运行: $status" $allReady = $false } } if (-not $allReady) { Write-Warning "部分基础设施服务未就绪,继续启动业务服务..." } #endregion #region === 启动业务服务 === Write-Step "5. 启动业务服务" $businessServices = @("api-gateway", "user-service", "auth-service", "merchant-service", "order-service", "product-service", "rider-service", "map-service", "marketing-service", "upload-service") foreach ($service in $businessServices) { Write-Info "启动 $service..." $composeCmd = "podman-compose --env-file $EnvFile -f infrastructure/compose/podman-compose.aliyun-dev.yml up -d $service" Invoke-Expression $composeCmd if ($LASTEXITCODE -eq 0) { Write-Success "$service 启动成功" } else { Write-Error "$service 启动失败" } } #endregion #region === 等待服务启动 === Write-Step "6. 等待服务启动" $waitSeconds = 60 Write-Info "等待 ${waitSeconds}秒..." Start-Sleep -Seconds $waitSeconds #endregion #region === 状态汇总 === Write-Step "7. 服务状态汇总" Write-Info "运行中的容器:" podman ps --filter "name=fzx-aliyun" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" # 检查健康检查端点 Write-Info "`n健康检查:" $healthChecks = @( @{Name = "API Gateway"; Url = "http://localhost:8080/api/health"}, @{Name = "Nacos"; Url = "http://localhost:8848/nacos/v1/ns/health"} ) foreach ($check in $healthChecks) { try { $response = Invoke-WebRequest -Uri $check.Url -Method Get -TimeoutSec 5 -ErrorAction Stop if ($response.StatusCode -eq 200) { Write-Success "$($check.Name): 健康" } else { Write-Error "$($check.Name): 异常 (HTTP $($response.StatusCode))" } } catch { Write-Error "$($check.Name): 无法访问 - $_" } } Write-Host "`n=== 启动完成 ===" -ForegroundColor $green Write-Info "使用 .\start.ps1 status 查看详细状态" #endregion