ingatlancom/php-circuit-breaker

PHP 断路器组件

1.0.3 2019-02-18 14:31 UTC

This package is auto-updated.

Last update: 2022-09-25 19:00:38 UTC


README

Build Status

一个帮助您优雅地处理外部服务(通常是远程的第三方服务)中断和超时的组件。

这是一个提供极其易于使用断路器组件的库。它不需要外部依赖,并为 APC、Memcached 或 Redis 提供默认存储实现,但可以以多种方式扩展。

框架支持

此库不需要任何特定的 PHP 框架,您只需要 PHP 5.3 或更高版本。

Symfony 2

如果您正在使用 Symfony 2 框架,您应该使用 php-circuit-breaker-bundle。这是一个我创建的捆绑包,用于封装 php-circuit-breaker 并将其与 Symfony 2 组件和依赖注入集成。

其他框架

如果您正在使用其他框架并且希望使用 php-circuit-breaker,请告诉我,我们可以尝试构建一个类似于为 Symfony 2 的开源集成。

动机 & 优点

  • 允许应用程序在无需人工干预的情况下检测失败并调整其行为。
  • 通过向模块中添加安全功能来提高服务的鲁棒性。

安装

您可以从源代码下载并使用它们与您的自动加载器,或者您可以使用 composer,在这种情况下,您只需要一个类似于这样的 require

"require": {
    "ingatlancom/php-circuit-breaker": "*"
},

之后,您应该更新 composer 依赖项,然后您就可以开始了。

用例 - 非关键功能

  • 您的应用程序有一个非关键功能,如:用户跟踪、统计、推荐等。
  • 此可选功能使用远程服务,导致您的应用程序中断。
  • 您希望在“非关键功能”失败时保持应用程序和核心流程可用。

您的应用程序代码可能如下所示

    $factory = new Ejsmont\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

    $userProfile = null;
    if( $circuitBreaker->isAvailable("UserProfileService") ){
        try{
            $userProfile = $userProfileService->loadProfileOrWhatever();
            $circuitBreaker->reportSuccess("UserProfileService");
        }catch( UserProfileServiceConnectionException $e ){
            // network failed - report it as failure
            $circuitBreaker->reportFailure("UserProfileService");
        }catch( Exception $e ){
            // something went wrong but it is not service's fault, dont report as failure
        }
    }
    if( $userProfile === null ){
        // for example, show 'System maintenance, you cant login now.' message
        // but still let people buy as logged out customers.
    }

用例 - 支付网关

  • Web 应用程序依赖于第三方服务(例如支付网关)。
  • Web 应用程序需要跟踪第三方服务何时不可用。
  • 应用程序不能变慢/不可用,它必须告诉用户功能受限或隐藏它们。
  • 应用程序在渲染结账页面之前使用断路器,如果特定的支付网关不可用,则从用户那里隐藏支付选项。

如您所见,这是一个在运行时选择性地禁用功能但仍然允许核心业务流程不间断的非常强大的概念。

后端与支付服务通信可能如下所示

    $factory = new Ejsmont\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

    try{
        // try to process the payment
        // then tell circuit breaker that it went well
        $circuitBreaker->reportSuccess("PaymentOptionOne");
    }catch( SomePaymentConnectionException $e ){
        // If you get network error report it as failure
        $circuitBreaker->reportFailure("PaymentOptionOne");
    }catch( Exception $e ){
        // in case of your own error handle it however it makes sense but
        // dont tell circuit breaker it was 3rd party service failure
    }

由于您正在记录成功和失败的操作,现在您也可以在前端使用它们来隐藏失败的支付选项。

前端渲染的可用支付选项可能如下所示

    $factory = new Ejsmont\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

    if ($circuitBreaker->isAvailable("PaymentOptionOne")) {
        // display the option
    }

功能

  • 通过单个断路器实例跟踪多个服务。
  • 可插拔的后端适配器,默认提供APC和Memcached。
  • 可自定义的服务阈值。您可以为服务被认为是失败所需多少次失败定义。
  • 可自定义的重试超时。您不希望永久禁用服务。在提供的超时后,断路器将允许单个进程尝试

性能影响

断路器的开销可以忽略不计。

APC实现执行isAvailable()大约需要0.0002秒,然后报告成功或报告失败。

Memcache适配器在与本地memcached进程通信时大约在0.0005秒范围内。

唯一可能影响性能的是网络连接时间。如果您选择使用远程memcached服务器或实现您自己的自定义StorageAdapter。

运行测试

测试通过PHPUnit运行。您可以通过以下方式运行所有测试

phpunit //or vendor/bin/phpunit

您可以通过运行以下命令来运行所选的测试用例

cd tests
phpunit Unit/Ejsmont/CircuitBreaker/Storage/Adapter/DummyAdapterTest.php

详细信息

在文档更新之前,您可以在我的博客上了解更多关于断路器及其实现的概念 http://artur.ejsmont.org/blog/circuit-breaker

一些实现细节已更改,但核心逻辑仍然是相同的。

作者