bvtterfly / laravel-circuit-breaker
此包已被弃用且不再维护。未建议替代包。
Laravel 断路器包
0.1.0
2022-03-03 16:34 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-03-05 14:36:46 UTC
README
🚨 此包已被弃用 🚨
我不再使用 Laravel,无法证明维护此包所需的时间。这就是我选择弃用的原因。您可以随意分支我的代码并维护自己的副本。
Laravel 断路器
此包是对 Laravel 断路器模式的简单实现。它保护您的应用程序免受服务依赖项失败的影响。
有关断路器模式的资源
- https://martinfowler.com.cn/bliki/CircuitBreaker.html
- https://github.com/Netflix/Hystrix/wiki/How-it-Works#CircuitBreaker
安装
您可以通过 composer 安装此包
composer require bvtterfly/laravel-circuit-breaker
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="circuit-breaker-config"
这是发布配置文件的内容
return [ // Here you may specify which of your cache stores you wish to use as your default store. 'store' => config('cache.default'), // length of interval (in seconds) over which it calculates the error rate 'time_window' => 60, // the number of errors to encounter within a given timespan before opening the circuit 'error_threshold' => 10, // the amount of time until the circuit breaker will try to query the resource again 'error_timeout' => 300, // the timeout for the circuit when it is in the half-open state 'half_open_timeout' => 150, // the amount of consecutive successes for the circuit to close again 'success_threshold' => 1, ];
用法
您的应用程序可能有多个服务,因此您将为每个服务获取一个断路器
use Bvtterfly\LaravelCircuitBreaker\Facades\CircuitBreaker; $circuit = CircuitBreaker::service('my-service'); // or you can override default configuration: $circuit = CircuitBreaker::service('my-service', [ 'time_window' => 120, 'success_threshold' => 3, ]);
断路器的三种状态
然后,您可以确定服务是否可用。
// Check circuit status for service if (! $circuit->isAvailable()) { // Service isn't available }
如果服务处于 CLOSED 或 HALF_OPEN 状态,则服务可用。然后,您应根据响应调用您的服务。您可以将其标记为成功或失败以更新断路器状态。
try { callAPI(); $circuit->markSuccess(); } catch (\Exception $e) { // If an error occurred, it must be recorded as failed. $circuit->markFailed(); }
测试
composer test
更新日志
请参阅 更新日志 了解最近的变化。
贡献
请参阅 贡献指南 了解详情。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件。