【问题标题】:Handle multiple requests in Guzzle between two Laravel projects at same time在 Guzzle 中同时处理两个 Laravel 项目之间的多个请求
【发布时间】: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


    【解决方案1】:

    这是因为您的服务器配置。在 Windows 上 php-cgi 不能同时处理多个连接。所以在开始第二个脚本之前,php-cgi.exe 等到第一个结束。

    您可以使用内置 php 服务器 (php -S),它允许您分别处理与 80008001 的连接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多