PHP code example of webman-micro / php-breaker

1. Go to this page and download the library: Download webman-micro/php-breaker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webman-micro / php-breaker example snippets


// config/breaker.php

return = [
    // 型
    'server' => [
        'name' => 'saas', // 当前服务名称
        'uuid' => '00a5ca80-4fe9-11eb-875c-c3e67d5eac81' // 随机码
    ],
    // optional
    'time_window' => 20, // 开路窗口时间 默认值 20s
    'failure_rate_threshold' => 15, // 故障率阈值 默认值 15%
    'interval_to_half_open' => 10 // 半开路窗口时间 默认值 10s
];

// config/breaker.php

return = [
   // 型
    'services' => ['im'], // 访问服务列表
    // optional
    'time_window' => 10, // 滑动窗口时间 默认值 10s
    'buckets' => 40, // 小的窗口数量  默认 40 (默认每个小窗口的时间跨度为250ms)
    'k' => 1.5 // 倍值 默认1.5
];

 $circuitConfig = [
      // 熔断器类型
    'server' => [
        'name' => 'saas', // 当前服务名称
        'uuid' => '00a5ca80-4fe9-11eb-875c-c3e67d5eac81' // 随机码
    ],
    // optional
    'time_window' => 20, // 开路窗口时间 默认值 20s
    'failure_rate_threshold' => 15, // 故障率阈值 默认值 15%
    'interval_to_half_open' => 10 // 半开路窗口时间 默认值 10s
];


$googleConfig = [
    // 

use WebmanMicro\PhpBreaker\BreakerFactory;
use Yurun\Util\HttpRequest; // 这个非必须

// im 为当前请求服务名
$service = 'im';

if (BreakerFactory::isAvailable($service)) { 
    // …… 此处省略服务调用过程
    
    // 用 http 调用举例 使用 Yurun\Util\HttpRequest
    $httpRequest = new HttpRequest;
    $res = $httpRequest->timeout(30000, 500)->get('http://www.baidu.com');
    
    if ($res->getStatusCode() === 200) {
       // 请求成功设置
       BreakerFactory::success($service);
    } else {
       // 请求失败设置
       BreakerFactory::failure($service);
    }
}

composer