bvtterfly/laravel-circuit-breaker

此包已被弃用且不再维护。未建议替代包。

Laravel 断路器包

0.1.0 2022-03-03 16:34 UTC

README

🚨 此包已被弃用 🚨

我不再使用 Laravel,无法证明维护此包所需的时间。这就是我选择弃用的原因。您可以随意分支我的代码并维护自己的副本。

Laravel 断路器

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包是对 Laravel 断路器模式的简单实现。它保护您的应用程序免受服务依赖项失败的影响。

有关断路器模式的资源

安装

您可以通过 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,
]);

断路器的三种状态

53690408-4a7f3d00-3dad-11e9-852c-0e082b7b9636.png

然后,您可以确定服务是否可用。

// 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)。有关更多信息,请参阅 许可文件