syastrebov/circuit-breaker-bundle
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(); }); }
syastrebov/circuit-breaker-bundle 适用场景与选型建议
syastrebov/circuit-breaker-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 syastrebov/circuit-breaker-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 syastrebov/circuit-breaker-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-02