chocofamilyme/smart-http

服务间通信的 HTTP 客户端

1.1.1 2022-09-16 09:50 UTC

README

基于 Guzzle 的 HTTP 客户端,具有预置设置。可以执行配置好的请求 - Request 类,也可以自行精细调整请求 - Client

功能

  • 缓存 GET 请求
  • 自动关闭不可用服务(模式:断路器
  • 如果服务在超时或返回非成功状态时不响应,则重试请求
  • 执行多个并行异步请求(模式:API 组合

要求

  • PHP 7.2+
  • guzzlehttp/guzzle 6.0+
  • ejsmont-artur/php-circuit-breaker

安装

$ composer require chocofamilyme/smart-http

示例

简单的 GET 请求

    $request = new Chocofamily\SmartHttp\Http\Request($config, $cache);

    $options = [
        'serviceName' => 'serviceA',
        'cache'       => 3600,
        'data'        => [
            'id' => 1
        ],
    ];

    $response = $request->send('GET', 'http://service/item', $options);

参数 $cache 是可选的。如果不存在,则不缓存请求并自动关闭服务。

包含对象 $config 的参数

$options 参数

缓存响应

可以在参数 cache 指定的时间内缓存请求,缓存键是请求的 url。在基于 url 形成键时,将忽略查询参数 correlation_idspan_id