【发布时间】:2022-02-21 19:26:41
【问题描述】:
我在两个项目中工作,一个 Laravel 8 项目和一个 Laravel Lumen 8 项目,这两个项目都使用 Guzzle 发出 HTTP 请求,它们在以下域上运行:
- Laravel 8 http://localhost:8000
- Laravel Lumen 8 http://localhost:8001
我正在使用 MAMP Pro(Apache 和 MySQL)。
我的 Laravel 8 项目向我的 Lumen 项目发出 HTTP 请求,它向我的 Laravel 8 项目发出 HTTP 请求,我面临的问题是我的 Laravel 项目的第一个请求总是超时,然后请求从 Lumen 项目开始。
这不是我想要的,我需要第一个请求来立即在我的 Lumen 项目中启动请求并让它返回对第一个请求的响应。
我的 HTTP 请求结构中缺少什么以允许这种情况发生?我曾尝试在数据库中使用会话,因为我认为这可能是会话阻塞或请求排队:
Laravel 8 项目的请求(首发)
/**
* Route the microservice
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function microservice(Request $request, $service)
{
Log::debug("HUB Microservice");
// TODO: this is always timing out regardless of what timeout I set
// seems to only start the Lumen request when this finishes but I need
// the response from the Lumen's request to be here
$response = Http::timeout(20)->get('http://localhost:8001/api/reports');
// the response from the microservice on the Lumen project
return response()->json($response->json(), $response->status());
}
Laravel Lumen 8 项目的请求(第二个,需要将响应返回给第一个)
<?php
namespace App\Http\Middleware;
use Closure;
use GuzzleHttp\Client;
class BeforeMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $ability)
{
$client = new Client([
'base_uri' => 'http://localhost:8000',
'timeout' => 60
]);
// TODO: this request only ever starts when my Laravel (first) request
// times out, is it domain related?
$res = $client->request('POST', '/api/hub/login', [
'form_params' => [
'key' => 'value'
]
]);
// Post-Middleware Action
return $next($request);
}
}
我错过了什么
【问题讨论】:
标签: php laravel apache lumen guzzle