proemergotech/correlate-php-guzzle

为Guzzle请求添加关联ID。

dev-master 2017-06-20 13:36 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:08:53 UTC


README

概述

当我们与微服务一起工作时,很难追踪整个系统中的请求。为此,我们提出了一个解决方案。我们为每个请求和每个服务生成一个唯一的版本4 UUID,然后每个服务通过请求头将此ID传递给其他服务。我们称之为关联ID

安装

  • 通过composer安装
composer require proemergotech/correlate-php-guzzle

Slim框架的设置

此示例假设您已经使用了proemergotech/correlate-php-psr-7中间件!

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use ProEmergotech\Correlate\Correlate;
use ProEmergotech\Correlate\Guzzle\GuzzleCorrelateMiddleware;
use Monolog\Logger;

$app = new \Slim\App();

$container = $app->getContainer();

$container['logger'] = function($container) {
  return new Logger();
};

$container['httpClient'] = function ($container) {
  $cid = $container['request']->getAttribute(
    Correlate::getParamName()
  );
  $stack = HandlerStack::create(new CurlHandler());
  $stack->push(new GuzzleCorrelateMiddleware($cid));
  return new Client(['handler' => $stack]);
};

// See "proemergotech/correlate-php-psr-7" project
$app->add(new \ProEmergotech\Correlate\Psr7\Psr7CorrelateMiddleware($container['logger']));

/**
 * Example GET route
 *
 * @param  \Psr\Http\Message\ServerRequestInterface $req  PSR7 request
 * @param  \Psr\Http\Message\ResponseInterface      $res  PSR7 response
 * @param  array                                    $args Route parameters
 *
 * @return \Psr\Http\Message\ResponseInterface
 */
$app->get('/foo', function ($req, $res, $args) {

    $httpClient = $this->get('httpClient');
    $httpClient->request('GET', 'http://httpbin.org/');

    // You can override correlation id here
    $httpClient->request('GET', 'http://httpbin.org/', [
      'headers' => [
        Correlate::getHeaderName() => Correlate::id()
      ],
    ]);

    return $res;
});

Laravel 5的设置

编写一个服务提供程序以注册供以后使用的guzzle实例。创建一个处理器堆栈并将中间件推送到它。

此示例假设您已经使用了proemergotech/correlate-php-laravel中间件!

<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use ProEmergotech\Correlate\Correlate;
use ProEmergotech\Correlate\Guzzle\GuzzleCorrelateMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

class GuzzleHttpClientProvider extends ServiceProvider
{
  public function register()
  {
    $this->app->bind('guzzle', function () {

      // Determine correlation id.
      $cid = $this->app['request']->getCorrelationId(); // If you use proemergotech/correlate-php-laravel middleware

      // identical but without macros
      $cid = $this->app['request']->headers->get(Correlate::getHeaderName());

      $stack = HandlerStack::create(new CurlHandler());
      $stack->push(new GuzzleCorrelateMiddleware($cid));

      $config = isset($this->app['config']['guzzle']) ? $this->app['config']['guzzle'] : [];

      $config['handler'] = $stack;

      return new Client($config);
    });
  }
}

将服务提供程序添加到您的Laravel项目中的config/app.php。

// config/app.php

    'providers' => [
        ...

        \App\Providers\GuzzleHttpClientProvider::class,
    ],

Lumen 5的设置

编写一个服务提供程序以注册供以后使用的guzzle实例。创建一个处理器堆栈并将中间件推送到它。

此示例假设您已经使用了proemergotech/correlate-php-laravel中间件!

// bootstrap/app.php

// ...
$app->bind('guzzle', function () use ($app) {

    // Determine correlation id.
    $cid = $app['request']->getCorrelationId(); // If you use proemergotech/correlate-php-laravel middleware

    // identical but without macros
    $cid = $this->app['request']->headers->get(
      \ProEmergotech\Correlate\Correlate::getHeaderName()
    );

    $stack = HandlerStack::create(new CurlHandler());
    $stack->push(new \ProEmergotech\Correlate\Guzzle\GuzzleCorrelateMiddleware($cid));

    return new Client([
      'handler' => $stack
    ]);
  });
// ...

贡献

查看CONTRIBUTING.md文件。

致谢

此包由Soma SzélpálPro Emergotech Ltd.开发。

许可证

此项目根据MIT许可证发布。