luucasfzs / laravel-circuit-breaker
Laravel 框架的断路器模式实现
Requires
- php: >= 7.1
Requires (Dev)
- orchestra/testbench: ^3.6
- phpunit/phpunit: ~7.0
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-09-22 14:19:26 UTC
README
Laravel Framework 5.6 的断路器模式实现。
如果您需要为 Laravel 应用程序轻松实现 断路器模式,您就找到了正确的位置。
注意:此包与 Laravel 5.6 兼容。其他/旧版本尚未测试。
安装
您可以使用 Composer 为您的项目安装此包。
$ composer require luucasfzs/laravel-circuit-breaker
无需担心服务提供者和外观:Laravel 可以无需任何操作自动发现包!
只需记住使用以下命令 发布配置文件
php artisan vendor:publish
用法
您将始终使用单个类(CircuitBreaker
外观或 CircuitBreakerManager
类,如果您想注入它)来使用此包。
以下是方法参考
isAvailable(string $identifier) : bool
如果 $identifier
服务当前可用,则返回 true
。否则返回 false
。
注意:您可以使用任何您想要的作为标识符。如果可能,我喜欢使用 MyClass::class
名称。
reportFailure(string $identifier) : void
报告 $identifier
服务的失败尝试。请参阅下面的配置部分以了解如何管理尝试和失败时间。
reportSuccess(string $identifier) : void
报告 $identifier
服务的成功尝试。您可以使用它将服务标记为可用并从其中删除“失败”状态。
配置
默认值
通过编辑 config/circuit_breaker.php
配置文件内容,您将能够以更适合您需求的方式调整断路器。
在 default
项下有三个值
<?php return [ 'defaults' => [ 'attempts_threshold' => 3, 'attempts_ttl' => 1, 'failure_ttl' => 5 ], // ... ];
- attempts_threshold:使用它来指定在声明服务“失败”之前需要进行多少次尝试 - 默认:3;
- attempts_ttl:用于指定在声明服务“失败”之前进行的尝试的时间窗口(以分钟为单位) - 默认:1;
- failure_ttl:一旦服务被标记为“失败”,它将保持此状态这么多分钟 - 默认:5;
更好的理解:默认情况下,1 分钟内 3 次失败的尝试将导致服务“失败” 5 分钟。
服务映射
调整配置文件很酷,但如果我需要为特定服务具有特定的 ttl 和尝试次数怎么办?没问题:这里的 services
选项就是为了帮助。
正如您在 config/circuit_breaker.php
配置文件中看到的那样,您还有一个 services
项。您可以在其中指定单个服务的设置。以下是一个示例
<?php return [ 'defaults' => [ 'attempts_threshold' => 3, 'attempts_ttl' => 1, 'failure_ttl' => 5 ], 'services' => [ 'my_special_service_identifier' => [ 'attempts_threshold' => 2, 'attempts_ttl' => 1, 'failure_ttl' => 10 ] ] ];
然后,当您调用 CircuitBreaker::reportFailure('my_special_service_identifier')
时,断路器将识别“特殊”服务并使用特定的配置设置、TTL 和尝试次数。
技巧:您还可以在 service
数组中 覆盖 单个服务的单个设置。其他设置将与默认设置合并。
用法示例
假设我们有一个应用程序的支付网关集成。我们将这个类称为 PaymentsGateway
。
现在,假设这是一个第三方服务:有时它可能暂时不可用。然而,我们不想阻止用户购买东西,所以如果 PaymentsGateway
服务不可用,我们希望将订单重定向到名为 DelayedPaymentsGateway
的后备服务,该服务将简单地“排队”延迟订单以在将来处理。
让我们在以下 BuyArticleOperation
类中模拟这个过程。
<?php class BuyArticleOperation { /** @var PaymentsGateway */ private $paymentsGateway; /** @var DelayedPaymentsGateway */ private $delayedPaymentsGateway; public function process(string $orderId) { // doing stuff with my order and then... try { $this->paymentsGateway->attempt($orderId); } catch (PaymentsGatewayException $e) { // something went wrong, let's switch the payment // to the "delayed" queue system $this->delayedPaymentsGateway->queue($orderId); } } }
太好了!现在我们100%确信我们的支付将被处理。有时候这还不够。
你知道吗,可能 PaymentsGateway
至少需要5秒钟才能完成一次尝试,而你的应用程序每分钟接收数百个订单。即使我们在第一次尝试后“知道”它不起作用,反复调用 PaymentsGateway
真的有帮助吗?
嗯,这就是你可以使用这个断路器来编写代码的方法。
<?php use CircuitBreaker; use My\Namespace\PaymentsGateway; use My\Namespace\DelayedPaymentsGateway; class BuyArticleOperation { /** @var PaymentsGateway */ private $paymentsGateway; /** @var DelayedPaymentsGateway */ private $delayedPaymentsGateway; public function process(string $orderId) { if(CircuitBreaker::isAvailable(PaymentsGateway::class)) { try { $this->paymentsGateway->attempt($orderId); } catch (PaymentsGatewayException $e) { // something went wrong, let's switch the payment // to the "delayed" queue system and report that // the default gateway is not working! $this->delayedPaymentsGateway->queue($orderId); CircuitBreaker::reportFailure(PaymentsGateway::class); } // there's nothing we can do here anymore return; } // we already know that the service is disabled, so we // can queue the payment process on the delayed queue // directly, without letting our users wait more $this->delayedPaymentsGateway->queue($orderId); } }
假设我们在10秒内处理100个订单(在不同的进程上)。
- 对于第一个订单,我们请求
PaymentsGateway
处理,但出了点问题; - 我们将支付排队在
DelayedPaymentsGateway
上,并向CircuitBreaker
报告失败; - 第二和第三个订单也是这样;
- 处理完第三个订单后,
CircuitBreaker
决定在3次尝试(且少于1分钟)后,可以宣布PaymentsGateway
失败,并且我们可以暂时直接使用我们的DelayedPaymentsGateway
回退(5分钟); - 剩余的97个订单被排队并成功处理,没有浪费我们在一个我们相当确定不会起作用的服务上的时间(97 * 5 = 485处理秒);
酷吧? :)
测试
你可以轻松地使用以下命令执行测试
$ vendor/bin/phpunit
即将推出
- 指数退避失败TTLs;
- 设置小于1分钟的TTLs;
- 使底层存储实现可定制;
贡献
请参阅CONTRIBUTING和CODE_OF_CONDUCT获取详细信息。
安全
如果你发现任何与安全相关的问题,请通过dweedlez@icloud.com发送电子邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。