leocarmo/circuit-breaker-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

leocarmo/circuit-breaker-php

Composer 安装命令:

composer require leocarmo/circuit-breaker-php

包简介

Circuit Breaker for PHP

README 文档

README

Build Status Scrutinizer Code Quality Code Intelligence Status Total Downloads

For more information about this pattern see this.

Starting with composer

composer require leocarmo/circuit-breaker-php

Adapters

Redis Adapter

The first argument is a redis connection, the second is your product name, for redis namespace avoid key conflicts with another product using the same redis.

use LeoCarmo\CircuitBreaker\CircuitBreaker;
use LeoCarmo\CircuitBreaker\Adapters\RedisAdapter;

// Connect to redis
$redis = new \Redis();
$redis->connect('localhost', 6379);

$adapter = new RedisAdapter($redis, 'my-product');

// Set redis adapter for CB
$circuit = new CircuitBreaker($adapter, 'my-service');

See this for full example

Redis Cluster Adapter

Without use of multi command. The first argument is a redis connection, the second is your product name, for redis namespace avoid key conflicts with another product using the same redis.

use LeoCarmo\CircuitBreaker\CircuitBreaker;
use LeoCarmo\CircuitBreaker\Adapters\RedisClusterAdapter;

// Connect to redis
$redis = new \Redis();
$redis->connect('localhost', 6379);

$adapter = new RedisClusterAdapter($redis, 'my-product');

// Set redis adapter for CB
$circuit = new CircuitBreaker($adapter, 'my-service');

See this for full example

SwooleTable Adapter

use LeoCarmo\CircuitBreaker\CircuitBreaker;

$circuit = new CircuitBreaker(new SwooleTableAdapter(), 'my-service');

Guzzle Middleware

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use LeoCarmo\CircuitBreaker\GuzzleMiddleware;

$handler = new GuzzleMiddleware($circuit);

$handlers = HandlerStack::create();
$handlers->push($handler);

$client = new Client(['handler' => $handlers]);

$response = $client->get('leocarmo.dev');

Important: all status code between 200 and 299 will be recorded as a success, and other status will be recorded as a failure.

See this for full example

Customize success status code

If you need to specify a custom status code that is not a failure, you can use:

$handler = new GuzzleMiddleware($circuit);
$handler->setCustomSuccessCodes([400]);

Important: this configuration will record a success when a status code 400 is returned

See this for full example

Ignore status code

If you want to ignore the status code returned and not record a success or failure, use this:

$handler = new GuzzleMiddleware($circuit);
$handler->setCustomIgnoreCodes([412]);

Important

To use Customize success status code or Ignore status code you must set the Guzzle client config http_errors = false because the Guzzle Client throws a ClientException or ServerException when the status code is not in range >200 && <300.

Set circuit break settings

This is not required, default values will be set

$circuit->setSettings([
    'timeWindow' => 60, // Time for an open circuit (seconds)
    'failureRateThreshold' => 50, // Fail rate for open the circuit
    'intervalToHalfOpen' => 30,  // Half open time (seconds)
]);

Check if circuit is available (closed)

Each check is for a specific service. So you can have multiple services in the same application, and when one circuit is open, the other works normally.

// Check circuit status for service
if (! $circuit->isAvailable()) {
    die('Circuit is not available!');
}

Record success and failure

// Usage example for success and failure  
try {
    myService();
    $circuit->success();
} catch (RuntimeException $e) {
    // If an error occurred, it must be recorded as failure.
    $circuit->failure();
}

Development

Setup

make setup

Tests

make test 

Contributors

leocarmo/circuit-breaker-php 适用场景与选型建议

leocarmo/circuit-breaker-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 479.39k 次下载、GitHub Stars 达 300, 最近一次更新时间为 2019 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 leocarmo/circuit-breaker-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 leocarmo/circuit-breaker-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 leocarmo/circuit-breaker-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 479.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 300
  • 点击次数: 15
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 300
  • Watchers: 2
  • Forks: 29
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-30