param( [string]$NacosUrl = "http://localhost:8848", [string]$NacosNamespace = "dev" ) $ErrorActionPreference = "SilentlyContinue" function Write-Status { param( [string]$ServiceName, [string]$Status, [string]$Detail ) $color = switch ($Status) { "RUNNING" { "Green" } "UP" { "Green" } "HEALTHY" { "Green" } "STOPPED" { "Red" } "DOWN" { "Red" } "UNHEALTHY" { "Red" } "WARNING" { "Yellow" } default { "Gray" } } Write-Host "[$ServiceName] $Status" -ForegroundColor $color if ($Detail) { Write-Host " $Detail" -ForegroundColor Gray } } function Get-ContainerStatus { Write-Host "`n=== 容器运行状态 ===" -ForegroundColor Cyan $containers = podman ps --format "{{.Names}}|{{.Status}}|{{.Ports}}" 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "❌ Podman 不可用" -ForegroundColor Red return } $containerList = @() foreach ($line in $containers) { if ($line -match "^(.+)\|(.+)\|(.+)") { $name = $matches[1].Trim() $status = $matches[2].Trim() $ports = $matches[3].Trim() if ($status -match "Up") { $statusColor = "RUNNING" } else { $statusColor = "STOPPED" } $containerList += [PSCustomObject]@{ Name = $name Status = $statusColor Detail = $status } } } if ($containerList.Count -eq 0) { Write-Host "⚠️ 没有运行中的容器" -ForegroundColor Yellow } else { foreach ($c in $containerList) { Write-Status $c.Name $c.Status $c.Detail } } return $containerList } function Get-NacosServices { Write-Host "`n=== Nacos 服务注册状态 ===" -ForegroundColor Cyan try { $response = Invoke-RestMethod -Uri "$NacosUrl/nacos/v1/ns/service/list?namespaceId=$NacosNamespace" -Method Get -TimeoutSec 10 $services = $response.doms if (-not $services -or $services.Count -eq 0) { Write-Host "⚠️ Nacos 中没有注册的服务" -ForegroundColor Yellow return @() } $serviceList = @() foreach ($service in $services) { $instances = Invoke-RestMethod -Uri "$NacosUrl/nacos/v1/ns/instance/list?serviceName=$service&namespaceId=$NacosNamespace" -Method Get -TimeoutSec 10 $healthyCount = ($instances.hosts | Where-Object { $_.healthy -eq $true }).Count $totalCount = $instances.hosts.Count if ($healthyCount -eq $totalCount -and $totalCount -gt 0) { $status = "HEALTHY" } elseif ($healthyCount -gt 0) { $status = "WARNING" } else { $status = "UNHEALTHY" } $serviceList += [PSCustomObject]@{ Name = $service Status = $status Detail = "$healthyCount/$totalCount 实例健康" } } foreach ($s in $serviceList) { Write-Status $s.Name $s.Status $s.Detail } return $serviceList } catch { Write-Host "❌ 无法连接 Nacos: $_" -ForegroundColor Red return @() } } function Get-ServiceHealth { Write-Host "`n=== 服务健康检查 ===" -ForegroundColor Cyan $healthChecks = @( @{ Name = "API Gateway"; Url = "http://localhost:8089/api/health" }, @{ Name = "User Service"; Url = "http://localhost:8081/actuator/health" }, @{ Name = "Order Service"; Url = "http://localhost:8082/actuator/health" }, @{ Name = "Merchant Service"; Url = "http://localhost:8083/actuator/health" }, @{ Name = "Product Service"; Url = "http://localhost:8084/actuator/health" }, @{ Name = "Auth Service"; Url = "http://localhost:8085/actuator/health" } ) $healthList = @() foreach ($check in $healthChecks) { try { $response = Invoke-RestMethod -Uri $check.Url -Method Get -TimeoutSec 5 if ($response.status -eq "UP" -or $response.code -eq 200) { $status = "UP" $detail = "响应正常" } else { $status = "WARNING" $detail = "状态异常" } } catch { $status = "DOWN" $detail = "连接失败" } $healthList += [PSCustomObject]@{ Name = $check.Name Status = $status Detail = $detail } Write-Status $check.Name $status $detail } return $healthList } function Get-DatabaseStatus { Write-Host "`n=== 数据库连接状态 ===" -ForegroundColor Cyan try { $result = Invoke-Expression "mysql -h localhost -P 3306 -u root -e 'SELECT 1' 2>&1" if ($LASTEXITCODE -eq 0) { Write-Status "MariaDB" "UP" "连接正常" return @{ Name = "MariaDB"; Status = "UP"; Detail = "连接正常" } } else { Write-Status "MariaDB" "DOWN" "连接失败" return @{ Name = "MariaDB"; Status = "DOWN"; Detail = "连接失败" } } } catch { Write-Status "MariaDB" "DOWN" "检查失败: $_" return @{ Name = "MariaDB"; Status = "DOWN"; Detail = "检查失败" } } } function Get-RedisStatus { Write-Host "`n=== Redis 状态 ===" -ForegroundColor Cyan try { $result = redis-cli -h localhost -p 6379 PING 2>&1 if ($result -eq "PONG") { Write-Status "Redis" "UP" "响应正常" return @{ Name = "Redis"; Status = "UP"; Detail = "响应正常" } } else { Write-Status "Redis" "DOWN" "无响应" return @{ Name = "Redis"; Status = "DOWN"; Detail = "无响应" } } } catch { Write-Status "Redis" "DOWN" "检查失败: $_" return @{ Name = "Redis"; Status = "DOWN"; Detail = "检查失败" } } } function Export-StatusJson { param( $Containers, $Services, $Health, $Database, $Redis ) $status = [PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Containers = $Containers NacosServices = $Services ServiceHealth = $Health Database = $Database Redis = $Redis Summary = [PSCustomObject]@{ TotalContainers = $Containers.Count RunningContainers = ($Containers | Where-Object { $_.Status -eq "RUNNING" }).Count TotalServices = $Services.Count HealthyServices = ($Services | Where-Object { $_.Status -eq "HEALTHY" }).Count TotalHealthChecks = $Health.Count UpHealthChecks = ($Health | Where-Object { $_.Status -eq "UP" }).Count } } $outputPath = Join-Path $PSScriptRoot "..\status\status.json" $outputDir = Split-Path $outputPath -Parent if (-not (Test-Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir | Out-Null } $status | ConvertTo-Json -Depth 10 | Out-File $outputPath -Encoding utf8 Write-Host "`n✅ 状态已导出到: $outputPath" -ForegroundColor Green } Write-Host "==========================================" -ForegroundColor Cyan Write-Host " 风七六平台 - 环境状态检查脚本" -ForegroundColor Cyan Write-Host "==========================================" -ForegroundColor Cyan Write-Host "检查时间: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")" -ForegroundColor Gray $containers = Get-ContainerStatus $services = Get-NacosServices $health = Get-ServiceHealth $db = Get-DatabaseStatus $redis = Get-RedisStatus Export-StatusJson -Containers $containers -Services $services -Health $health -Database $db -Redis $redis Write-Host "`n==========================================" -ForegroundColor Cyan Write-Host " 检查完成" -ForegroundColor Cyan Write-Host "==========================================" -ForegroundColor Cyan