承接 syastrebov/circuit-breaker-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

syastrebov/circuit-breaker-bundle

最新稳定版本:v0.1

Composer 安装命令:

composer require syastrebov/circuit-breaker-bundle

包简介

Symfony bundle for circuit breaker wrapper.

README 文档

README

PHP Circuit Breaker implementation for microservices and API calls.

A Symfony bundle for the https://github.com/syastrebov/circuit-breaker library.

Install

If you're using flex add to your composer.json

{
  ...
  
  "extra": {
    "symfony": {
      "endpoint": [
        "https://api.github.com/repos/syastrebov/recipies/contents/index.json",
        "flex://defaults"
      ]
    }
  }
}

Install via composer

composer require syastrebov/circuit-breaker-bundle

Config

Supported Providers

Database

config/packages/doctrine.yaml

doctrine:
  dbal:
    default_connection: default
    connections:
      default:
        driver: pdo_sqlite
        path: ':memory:'
      secondary:
        driver: pdo_mysql
        host: mysql
        user: user
        password: password
        dbname: database

config/packages/circuit_breaker.yaml

circuit_breaker:
  driver: database
  connections:
    database:
      connection: secondary

Memcached

config/packages/circuit_breaker.yaml

circuit_breaker:
  driver: memcached
  connections:
    memcached:
      servers:
        # Uses default port
        - host: memcached-1
        # Explicitly define port
        - host: memcached-2
          port: 11211

Redis

Uses redis extension or predis as fallback.

config/packages/circuit_breaker.yaml

circuit_breaker:
  driver: redis
  connections:
    redis:
      host: redis
      port: 6379
      username: username
      password: password

Redis Cluster

Uses redis extension or predis as fallback.

config/packages/circuit_breaker.yaml

circuit_breaker:
  driver: redis
  connections:
    redis:
      nodes:
        # Uses default port
        - host: redis-node-1
        - host: redis-node-2
        # Explicitly define port
        - host: redis-node-3
          port: 6379
      password: password

Predis

To force Predis usage despite the redis extension being installed:

config/packages/circuit_breaker.yaml

circuit_breaker:
  driver: predis

Memory

config/packages/circuit_breaker.yaml

circuit_breaker:
  driver: memory

Multiple Instances

config/packages/circuit_breaker.yaml

circuit_breaker:
  configurations:
    default:
      retries: 3
      closed_threshold: 3
      half_open_threshold: 3
      retry_interval: 1000
      open_timeout: 60
      fallback_or_null: false
    api:
      retries: 2
      closed_threshold: 5
      half_open_threshold: 10
      retry_interval: 2000
      open_timeout: 120
      fallback_or_null: true
use CircuitBreaker\Contracts\CircuitBreakerInterface;

public function requestDefault(
    #[Autowire(service: 'circuit_breaker.default')] 
    CircuitBreakerInterface $circuit
): string {
    // handle request
}

public function requestApi(
    #[Autowire(service: 'circuit_breaker.api')] 
    CircuitBreakerInterface $circuit
): string {
    // handle request
}

Logger

circuit_breaker:
  logger: monolog.logger.circuit_breaker

Usage

Simple example:

use CircuitBreaker\Contracts\CircuitBreakerInterface;

public function request(CircuitBreakerInterface $circuit): string
{
    try {
        return $circuit->run('test', function () {
            return '{"response": "data"}';
        });
    } catch (UnableToProcessException $e) {
        // handle exception
    }
}

Use custom config:

use CircuitBreaker\Contracts\CircuitBreakerInterface;

public function request(
    #[Autowire(service: 'circuit_breaker.api')] 
    CircuitBreakerInterface $circuit
): string {
    try {
        return $circuit->run('test', function () {
            return '{"response": "data"}';
        });
    } catch (UnableToProcessException $e) {
        // handle exception
    }
}

Stub response:

use CircuitBreaker\Contracts\CircuitBreakerInterface;

public function request(
    #[Autowire(service: 'circuit_breaker.api')] 
    CircuitBreakerInterface $circuit
): string {
    return $circuit->run(
        '{endpoint}',
        static function () {
            return (string) (new Client)->get('https://domain/api/{endpoint}')->getBody();
        },
        static function () {
            return json_encode([
                'data' => [
                    'key' => 'default value',
                ],
            ]);
        }
    );
}

Cacheable response:

Configure a cache pool to be able to use cacheable circuit breaker.

config/packages/framework.yaml

framework:
  cache:
    pools:
      circuit_breaker.pool:
        adapter: cache.adapter.filesystem

config/packages/circuit_breaker.yaml

circuit_breaker:
  provider: memory
  cache_pool: circuit_breaker.pool

  configurations:
    api:
      retries: 2
      closed_threshold: 5
      half_open_threshold: 10
      retry_interval: 3000
      open_timeout: 120
      fallback_or_null: true

Use .cacheable suffix to use CacheableCircuitBreaker.

use CircuitBreaker\Contracts\CircuitBreakerInterface;

public function request(
    #[Autowire(service: 'circuit_breaker.api.cacheable')] 
    CircuitBreakerInterface $circuit
): string {
    return $circuit->run('{endpoint}', static function () {
        return (string) (new Client)->get('https://domain/api/{endpoint}')->getBody();
    });
}

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固