1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| <?php declare(strict_types=1);
namespace app\service;
use think\facade\Cache; use think\facade\Log;
class HttpClient {
private $serviceRegistry = [ 'user' => 'http://user-service:8001', 'order' => 'http://order-service:8002', 'product' => 'http://product-service:8003', 'payment' => 'http://payment-service:8004', 'notification' => 'http://notification-service:8005', ];
public function request(string $service, string $endpoint, array $data = [], string $method = 'POST', array $headers = []): array { $url = $this->getServiceUrl($service) . $endpoint; $defaultHeaders = [ 'Content-Type: application/json', 'Accept: application/json', 'X-Request-ID: ' . $this->generateRequestId(), 'X-Service-Name: ' . config('app.name', 'unknown'), ]; $headers = array_merge($defaultHeaders, $headers); $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_HTTPHEADER => $headers, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, ]); switch (strtoupper($method)) { case 'POST': curl_setopt($ch, CURLOPT_POST, true); if (!empty($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } break; case 'PUT': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); if (!empty($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } break; case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case 'GET': default: if (!empty($data)) { $url .= '?' . http_build_query($data); curl_setopt($ch, CURLOPT_URL, $url); } break; } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); $this->logRequest($service, $endpoint, $method, $data, $httpCode, $response); if ($error) { throw new \Exception("HTTP请求失败: {$error}"); } if ($httpCode >= 400) { throw new \Exception("HTTP请求错误: HTTP {$httpCode}"); } $result = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('响应数据格式错误: ' . json_last_error_msg()); } return $result; }
private function getServiceUrl(string $service): string { if (!isset($this->serviceRegistry[$service])) { throw new \Exception("未知的服务: {$service}"); } return $this->serviceRegistry[$service]; }
private function generateRequestId(): string { return uniqid('req_', true); }
private function logRequest(string $service, string $endpoint, string $method, array $data, int $httpCode, string $response): void { $logData = [ 'service' => $service, 'endpoint' => $endpoint, 'method' => $method, 'request_data' => $data, 'http_code' => $httpCode, 'response' => $response, 'timestamp' => date('Y-m-d H:i:s'), ]; Log::info('微服务HTTP请求', $logData); } }
|