manomano-tech/correlation-ids-guzzle

此软件包已被放弃,不再维护。作者建议使用不再维护的软件包。

Guzzle 插件,用于 manomano-tech/correlation-ids 软件包

1.0.0 2018-09-29 18:20 UTC

This package is auto-updated.

Last update: 2022-07-18 15:01:27 UTC


README

📢 注意: 此存储库不再维护。

Guzzle 请求关联

在 Guzzle 请求中注入请求关联头

安装

composer require manomano-tech/correlation-ids-guzzle

用法

首先,生成一个 CorrelationIdContainer

use ManoManoTech\CorrelationId\Factory\CorrelationIdContainerFactory;
use ManoManoTech\CorrelationId\Generator\RamseyUuidGenerator;

// We specify which generator will be responsible for generating the
// identification of the current process
$generator = new RamseyUuidGenerator();

$factory = new CorrelationIdContainerFactory($generator);
$correlationIdContainer = $factory->create(
    '3fc044d9-90fa-4b50-b6d9-3423f567155f',
    '3b5263fa-1644-4750-8f11-aaf61e58cd9e'
);

然后,您有两种选择

将中间件添加到您的 Guzzle 客户端

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use ManoManoTech\CorrelationIdGuzzle\CorrelationIdMiddleware;

// create the middleware
$correlationIdMiddleware = new CorrelationIdMiddleware($correlationIdContainer);

$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest($correlationIdMiddleware));

$client = new Client(['handler' => $stack]);

使用工厂创建 Guzzle 客户端

use ManoManoTech\CorrelationIdGuzzle\GuzzleClientFactory;
use ManoManoTech\CorrelationIdGuzzle\CorrelationIdMiddleware;

// create the middleware
$correlationIdMiddleware = new CorrelationIdMiddleware($correlationIdContainer);

$factory = new GuzzleClientFactory($correlationIdMiddleware);
// return an instance of GuzzleHttp\Client
$client = $factory->create();

自定义头部名称

默认情况下,请求头将类似于以下内容

GET / HTTP/1.1
Host: example.com
parent-correlation-id: 3fc044d9-90fa-4b50-b6d9-3423f567155f
root-correlation-id: 3b5263fa-1644-4750-8f11-aaf61e58cd9e

您可以通过向构造函数提供第二个参数来更改此设置

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use ManoManoTech\CorrelationId\CorrelationEntryName;
use ManoManoTech\CorrelationIdGuzzle\CorrelationIdMiddleware;

$correlationHeaderName = new CorrelationEntryName(
    'current-id', // not used in this context
    'parent-id',
    'root-id'
);
$correlationIdMiddleware = new CorrelationIdMiddleware(
    $correlationIdContainer,
    $correlationHeaderName
);

$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest($correlationIdMiddleware));

$client = new Client(['handler' => $stack]);

将生成以下内容

GET / HTTP/1.1
Host: example.com
parent-id: 3fc044d9-90fa-4b50-b6d9-3423f567155f
root-id: 3b5263fa-1644-4750-8f11-aaf61e58cd9e