HealthCheckController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 健康检查控制器
  9. * 用于检查依赖服务状态
  10. */
  11. class HealthCheckController extends Controller
  12. {
  13. /**
  14. * 检查所有依赖服务状态
  15. */
  16. public function index(): JsonResponse
  17. {
  18. $checks = [
  19. 'question_bank_api' => $this->checkQuestionBankService(),
  20. 'learning_analytics' => $this->checkLearningAnalyticsService(),
  21. 'database' => $this->checkDatabase(),
  22. ];
  23. $allHealthy = collect($checks)->every(fn($check) => $check['status'] === 'healthy');
  24. return response()->json([
  25. 'success' => true,
  26. 'overall_status' => $allHealthy ? 'healthy' : 'unhealthy',
  27. 'checks' => $checks,
  28. 'timestamp' => now()->toISOString(),
  29. ]);
  30. }
  31. /**
  32. * 检查题库服务
  33. */
  34. private function checkQuestionBankService(): array
  35. {
  36. try {
  37. $baseUrl = config('services.question_bank.base_url', 'http://localhost:5015');
  38. $response = Http::timeout(5)->get($baseUrl . '/health');
  39. return [
  40. 'status' => $response->successful() ? 'healthy' : 'unhealthy',
  41. 'message' => $response->body(),
  42. 'response_time' => $response->transferStats ? $response->transferStats->getTotalTime() : null,
  43. ];
  44. } catch (\Exception $e) {
  45. Log::error('题库服务健康检查失败', ['error' => $e->getMessage()]);
  46. return [
  47. 'status' => 'unhealthy',
  48. 'message' => $e->getMessage(),
  49. 'error_code' => $e->getCode(),
  50. ];
  51. }
  52. }
  53. /**
  54. * 检查学习分析服务
  55. */
  56. private function checkLearningAnalyticsService(): array
  57. {
  58. try {
  59. $baseUrl = config('services.learning_analytics.base_url', 'http://localhost:5016');
  60. $response = Http::timeout(5)->get($baseUrl . '/health');
  61. return [
  62. 'status' => $response->successful() ? 'healthy' : 'unhealthy',
  63. 'message' => $response->body(),
  64. 'response_time' => $response->transferStats ? $response->transferStats->getTotalTime() : null,
  65. ];
  66. } catch (\Exception $e) {
  67. Log::error('学习分析服务健康检查失败', ['error' => $e->getMessage()]);
  68. return [
  69. 'status' => 'unhealthy',
  70. 'message' => $e->getMessage(),
  71. 'error_code' => $e->getCode(),
  72. ];
  73. }
  74. }
  75. /**
  76. * 检查数据库连接
  77. */
  78. private function checkDatabase(): array
  79. {
  80. try {
  81. // 检查 MySQL 主库
  82. $mysqlResult = \DB::connection('mysql')->select('SELECT 1 as test');
  83. // 检查题库 PostgreSQL(如果存在)
  84. $pgResult = null;
  85. try {
  86. $pgResult = \DB::connection('pgsql')->select('SELECT 1 as test');
  87. } catch (\Exception $e) {
  88. // PostgreSQL 连接失败但不影响主流程
  89. }
  90. return [
  91. 'status' => 'healthy',
  92. 'message' => 'MySQL连接正常',
  93. 'mysql' => 'healthy',
  94. 'postgresql' => $pgResult ? 'healthy' : 'not_configured',
  95. ];
  96. } catch (\Exception $e) {
  97. Log::error('数据库健康检查失败', ['error' => $e->getMessage()]);
  98. return [
  99. 'status' => 'unhealthy',
  100. 'message' => $e->getMessage(),
  101. 'error_code' => $e->getCode(),
  102. ];
  103. }
  104. }
  105. }