114 lines
5.2 KiB
PowerShell
114 lines
5.2 KiB
PowerShell
|
|
# Whisper 服务连接测试脚本
|
||
|
|
# 用于在服务器上快速诊断 Whisper 连接问题
|
||
|
|
# 运行: .\test_whisper_connection.ps1
|
||
|
|
|
||
|
|
Write-Host "======================================" -ForegroundColor Cyan
|
||
|
|
Write-Host " Whisper 服务连接诊断工具" -ForegroundColor Cyan
|
||
|
|
Write-Host "======================================" -ForegroundColor Cyan
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
$whisperUrl = "http://localhost:5001"
|
||
|
|
$healthUrl = "$whisperUrl/health"
|
||
|
|
|
||
|
|
# 测试 1: 检查端口是否被监听
|
||
|
|
Write-Host "[测试 1] 检查端口 5001 是否被监听..." -ForegroundColor Yellow
|
||
|
|
$portCheck = Get-NetTCPConnection -LocalPort 5001 -ErrorAction SilentlyContinue
|
||
|
|
if ($portCheck) {
|
||
|
|
Write-Host "✅ 端口 5001 已被监听" -ForegroundColor Green
|
||
|
|
Write-Host " 进程ID: $($portCheck.OwningProcess)" -ForegroundColor Gray
|
||
|
|
$process = Get-Process -Id $portCheck.OwningProcess -ErrorAction SilentlyContinue
|
||
|
|
if ($process) {
|
||
|
|
Write-Host " 进程名: $($process.ProcessName)" -ForegroundColor Gray
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
Write-Host "❌ 端口 5001 未被监听" -ForegroundColor Red
|
||
|
|
Write-Host " → Whisper 服务可能未启动" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# 测试 2: 测试 HTTP 连接
|
||
|
|
Write-Host "[测试 2] 测试 HTTP 连接到 $healthUrl ..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$response = Invoke-WebRequest -Uri $healthUrl -TimeoutSec 5 -UseBasicParsing
|
||
|
|
if ($response.StatusCode -eq 200) {
|
||
|
|
Write-Host "✅ HTTP 连接成功!" -ForegroundColor Green
|
||
|
|
Write-Host " 状态码: $($response.StatusCode)" -ForegroundColor Gray
|
||
|
|
Write-Host " 响应内容: $($response.Content)" -ForegroundColor Gray
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
Write-Host "❌ HTTP 连接失败" -ForegroundColor Red
|
||
|
|
Write-Host " 错误: $($_.Exception.Message)" -ForegroundColor Red
|
||
|
|
|
||
|
|
# 提供详细错误信息
|
||
|
|
if ($_.Exception.Message -like "*拒绝连接*" -or $_.Exception.Message -like "*refused*") {
|
||
|
|
Write-Host " → 可能原因: Whisper 服务未启动" -ForegroundColor Yellow
|
||
|
|
} elseif ($_.Exception.Message -like "*超时*" -or $_.Exception.Message -like "*timeout*") {
|
||
|
|
Write-Host " → 可能原因: 服务响应超时,检查服务是否正常" -ForegroundColor Yellow
|
||
|
|
} elseif ($_.Exception.Message -like "*404*") {
|
||
|
|
Write-Host " → 可能原因: /health 接口不存在" -ForegroundColor Yellow
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# 测试 3: 使用 curl 测试(如果可用)
|
||
|
|
Write-Host "[测试 3] 使用 curl 测试连接..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$curlResult = curl.exe -s -o NUL -w "%{http_code}" $healthUrl
|
||
|
|
if ($curlResult -eq "200") {
|
||
|
|
Write-Host "✅ curl 测试成功" -ForegroundColor Green
|
||
|
|
} else {
|
||
|
|
Write-Host "⚠️ curl 返回状态码: $curlResult" -ForegroundColor Yellow
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
Write-Host "⚠️ curl 命令不可用,跳过此测试" -ForegroundColor Gray
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# 测试 4: 检查 Java 应用是否能访问 localhost
|
||
|
|
Write-Host "[测试 4] 测试 localhost 解析..." -ForegroundColor Yellow
|
||
|
|
$pingResult = Test-Connection -ComputerName localhost -Count 1 -Quiet
|
||
|
|
if ($pingResult) {
|
||
|
|
Write-Host "✅ localhost 解析正常" -ForegroundColor Green
|
||
|
|
} else {
|
||
|
|
Write-Host "❌ localhost 解析失败" -ForegroundColor Red
|
||
|
|
Write-Host " → 尝试使用 127.0.0.1 替代" -ForegroundColor Yellow
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# 测试 5: 检查防火墙规则
|
||
|
|
Write-Host "[测试 5] 检查防火墙规则(需要管理员权限)..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$firewallRules = Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True" -and $_.Direction -eq "Inbound"} | Select-Object -First 5
|
||
|
|
Write-Host "✅ 防火墙规则检查完成" -ForegroundColor Green
|
||
|
|
Write-Host " (如果连接失败,请检查端口 5001 是否被防火墙阻止)" -ForegroundColor Gray
|
||
|
|
} catch {
|
||
|
|
Write-Host "⚠️ 无法检查防火墙(可能需要管理员权限)" -ForegroundColor Yellow
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# 总结和建议
|
||
|
|
Write-Host "======================================" -ForegroundColor Cyan
|
||
|
|
Write-Host " 诊断总结" -ForegroundColor Cyan
|
||
|
|
Write-Host "======================================" -ForegroundColor Cyan
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "如果上述测试都失败,请按以下步骤排查:" -ForegroundColor Yellow
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "1. 启动 Whisper 服务:" -ForegroundColor White
|
||
|
|
Write-Host " cd <whisper_service_directory>" -ForegroundColor Gray
|
||
|
|
Write-Host " python app.py" -ForegroundColor Gray
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "2. 检查 Whisper 服务日志,确认端口和监听地址" -ForegroundColor White
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "3. 确认 Whisper 配置:" -ForegroundColor White
|
||
|
|
Write-Host " - 监听地址: 0.0.0.0 或 localhost" -ForegroundColor Gray
|
||
|
|
Write-Host " - 监听端口: 5001" -ForegroundColor Gray
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "4. 如果 Whisper 运行在不同端口,需要修改:" -ForegroundColor White
|
||
|
|
Write-Host " LocalWhisperService.java 中的 WHISPER_URL" -ForegroundColor Gray
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "5. 测试完成后,使用以下接口进行进一步测试:" -ForegroundColor White
|
||
|
|
Write-Host " GET http://<your-server>/test/whisper/check" -ForegroundColor Gray
|
||
|
|
Write-Host " GET http://<your-server>/test/whisper/info" -ForegroundColor Gray
|
||
|
|
Write-Host " POST http://<your-server>/test/whisper/recognize" -ForegroundColor Gray
|
||
|
|
Write-Host ""
|