添加Prometheus/Grafana/Loki监控配置、Promtail配置、业务与监控服务器docker-compose文件以及Bash/PowerShell服务重启脚本
100 lines
4.0 KiB
PowerShell
100 lines
4.0 KiB
PowerShell
# Restart script for wangpan microservices
|
|
# Usage: .\restart-services.ps1
|
|
|
|
$JASYPT_PASSWORD = "wangpan-secret-key-2024"
|
|
$BASE_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
|
|
$services = @(
|
|
@{ Name = "user-service"; Port = 8081; Jar = "wangpan-user-service\target\wangpan-user-service-1.0-SNAPSHOT.jar" },
|
|
@{ Name = "drive-service"; Port = 8082; Jar = "wangpan-drive-service\target\wangpan-drive-service-1.0-SNAPSHOT.jar" },
|
|
@{ Name = "tmdb-service"; Port = 8083; Jar = "wangpan-tmdb-service\target\wangpan-tmdb-service-1.0-SNAPSHOT.jar" },
|
|
@{ Name = "gateway"; Port = 8888; Jar = "wangpan-gateway\target\wangpan-gateway-1.0-SNAPSHOT.jar" }
|
|
)
|
|
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host " Wangpan Microservices Restart" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
|
|
$env:JASYPT_ENCRYPTOR_PASSWORD = $JASYPT_PASSWORD
|
|
|
|
Write-Host "[Step 1] Checking port usage..." -ForegroundColor Yellow
|
|
foreach ($service in $services) {
|
|
$port = $service.Port
|
|
$name = $service.Name
|
|
$connections = netstat -ano | Select-String ":$port\s" | Select-String "LISTENING"
|
|
if ($connections) {
|
|
$pids = $connections | ForEach-Object { ($_ -split '\s+')[-1] } | Select-Object -Unique
|
|
foreach ($procId in $pids) {
|
|
if ($procId -match '^\d+$' -and $procId -ne '0') {
|
|
try {
|
|
$process = Get-Process -Id $procId -ErrorAction SilentlyContinue
|
|
if ($process) {
|
|
Write-Host " Killing process on port $port (PID: $procId)..." -ForegroundColor Red
|
|
Stop-Process -Id $procId -Force -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 1
|
|
}
|
|
} catch {
|
|
Write-Host " Failed to kill PID: $procId" -ForegroundColor Red
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
Write-Host " Port $port ($name) is free" -ForegroundColor Green
|
|
}
|
|
}
|
|
|
|
Write-Host "[Step 2] Waiting for ports to release..." -ForegroundColor Yellow
|
|
Start-Sleep -Seconds 3
|
|
|
|
Write-Host "[Step 3] Starting services..." -ForegroundColor Yellow
|
|
foreach ($service in $services) {
|
|
$name = $service.Name
|
|
$port = $service.Port
|
|
$jarPath = Join-Path $BASE_DIR $service.Jar
|
|
Write-Host "Starting $name on port $port..." -ForegroundColor Cyan
|
|
if (-not (Test-Path $jarPath)) {
|
|
Write-Host " ERROR: Jar not found: $jarPath" -ForegroundColor Red
|
|
continue
|
|
}
|
|
$process = Start-Process -FilePath "java" -ArgumentList "-jar", $jarPath -WindowStyle Hidden -PassThru
|
|
if ($process) {
|
|
Write-Host " Started $name (PID: $($process.Id))" -ForegroundColor Green
|
|
} else {
|
|
Write-Host " Failed to start $name" -ForegroundColor Red
|
|
}
|
|
if ($service -ne $services[-1]) {
|
|
Start-Sleep -Seconds 5
|
|
}
|
|
}
|
|
|
|
Write-Host "[Step 4] Verifying services..." -ForegroundColor Yellow
|
|
$allRunning = $true
|
|
$maxRetries = 6
|
|
foreach ($service in $services) {
|
|
$port = $service.Port
|
|
$name = $service.Name
|
|
$started = $false
|
|
for ($i = 1; $i -le $maxRetries; $i++) {
|
|
$connections = netstat -ano | Select-String ":$port\s" | Select-String "LISTENING"
|
|
if ($connections) {
|
|
Write-Host " [OK] $name (port $port)" -ForegroundColor Green
|
|
$started = $true
|
|
break
|
|
}
|
|
Start-Sleep -Seconds 5
|
|
}
|
|
if (-not $started) {
|
|
Write-Host " [FAIL] $name (port $port) - not started after $($maxRetries * 5)s" -ForegroundColor Red
|
|
$allRunning = $false
|
|
}
|
|
}
|
|
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
if ($allRunning) {
|
|
Write-Host "All services started successfully!" -ForegroundColor Green
|
|
Write-Host "Access: http://localhost:8888" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "Some services failed to start. Check logs." -ForegroundColor Red
|
|
}
|
|
Write-Host "========================================" -ForegroundColor Cyan
|