paymaxi/circuit-breaker

PHP 电路断路器组件

v0.1.1 2017-03-09 21:33 UTC

This package is auto-updated.

Last update: 2024-09-11 17:20:09 UTC


README

Build Status Quality Score Coverage Status Latest Version on Packagist Total Downloads Software License

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

这是一个提供非常易于使用电路断路器组件的库。它不需要外部依赖,并为APC和Memcached提供默认存储实现,但可以以多种方式进行扩展。详见 更多

框架支持

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

动机与好处

  • 允许应用程序检测故障并适应其行为,而无需人工干预。
  • 通过向模块中添加安全功能来提高服务的健壮性。

安装

您可以从源代码下载并使用它们与您的自动加载器,或者您可以使用Composer,在这种情况下,您只需要如下require:

"require": {
    "paymaxi\circuit-breaker": "*"
},

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

用例 - 非关键功能

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

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

    $factory = new Paymaxi\Component\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 Paymaxi\Component\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 Paymaxi\Component\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

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

特性

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

性能影响

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

APC实现执行isAvailable()并报告Success()或报告Failure()大约需要0.0002秒。

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

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

许可证

MIT许可证(MIT)。请参阅 许可证文件 了解更多信息。