mcrm/laravel-connection-storage
Composer 安装命令:
composer require mcrm/laravel-connection-storage
包简介
Flexible Laravel package for managing external database connections with Redis cache and HTTP API fallback. Supports customizable DTOs and multiple connection types for microservices architecture.
README 文档
README
Flexible Laravel package for managing external database connections with Redis cache and HTTP API fallback. Supports customizable DTOs and multiple connection types for microservices architecture.
Features
- ✅ Redis Caching - automatic connection data caching
- ✅ HTTP API Integration - external API data retrieval
- ✅ Connection Validation - database availability checks
- ✅ Notification System - automatic error notifications
- ✅ Connection Blocking - temporary blocking after failures
- ✅ Flexible DTOs - customizable data classes for different projects
- ✅ Multi-service Architecture - service-specific data extraction support
Installation
1. Install Package
composer require mcrm/laravel-connection-storage
2. Publish Configuration (REQUIRED)
php artisan vendor:publish --tag=connection-storage-config
This command will copy the configuration file to config/connection-storage.php in your project.
3. Environment Variables Setup
Add to your .env file:
# Cache settings CONNECTION_STORAGE_CACHE_STORE=redis CONNECTION_STORAGE_CACHE_TTL=86400 CONNECTION_STORAGE_CACHE_PREFIX=connection_data_ # External API settings CONNECTION_STORAGE_API_URL=http://your-api-server.com CONNECTION_STORAGE_API_TIMEOUT=10 CONNECTION_STORAGE_NOTIFICATION_URL=http://your-api-server.com/api/connection-issues CONNECTION_STORAGE_API_TOKEN=your-api-token # DTO settings (if using custom DTO) CONNECTION_STORAGE_DTO_CLASS=App\\DTOs\\YourCustomConnectionDTO # Service settings CONNECTION_STORAGE_SERVICE_KEY=your_service_name CONNECTION_STORAGE_SERVICE_NAME=your-service # Validation settings CONNECTION_STORAGE_VALIDATION_ENABLED=true CONNECTION_STORAGE_VALIDATION_TIMEOUT=5 CONNECTION_STORAGE_BLOCK_DURATION=1800 CONNECTION_STORAGE_BLOCK_PREFIX=connection_block_ # Notification settings CONNECTION_STORAGE_NOTIFICATIONS_ENABLED=true CONNECTION_STORAGE_NOTIFICATIONS_TIMEOUT=10 CONNECTION_STORAGE_NOTIFICATIONS_RETRY=false
Basic Usage
Getting Connection Data
use Mcrm\LaravelConnectionStorage\Interfaces\ConnectionDataProviderInterface; class DatabaseConnectionManager { public function __construct( private ConnectionDataProviderInterface $connectionProvider ) {} public function getConnection(string $boxName): ?array { $connectionData = $this->connectionProvider->getConnectionData($boxName); if ($connectionData === null) { return null; } return $connectionData->getConnectionConfig('db'); } }
Manual Data Caching
use Mcrm\LaravelConnectionStorage\Interfaces\ConnectionDataProviderInterface; use App\DTOs\YourCustomConnectionDTO; $connectionData = YourCustomConnectionDTO::fromArray([ 'db' => [ 'host' => 'localhost', 'database' => 'your_db', 'username' => 'user', 'password' => 'pass' ] ]); $this->connectionProvider->cacheConnectionData('box-name', $connectionData);
Creating Custom DTO
1. Create Your DTO Class
<?php namespace App\DTOs; use Mcrm\LaravelConnectionStorage\DTO\BaseConnectionDataDTO; class YourCustomConnectionDTO extends BaseConnectionDataDTO { public function __construct( array $db = [], array $rabbitmq = [], array $elasticsearch = [], // Add your custom fields public readonly array $customField = [] ) { parent::__construct($db, $rabbitmq); } public static function fromArray(array $data): ?static { return new static( db: $data['db'] ?? [], rabbitmq: $data['rabbitmq'] ?? [], elasticsearch: $data['elasticsearch'] ?? [], customField: $data['custom_field'] ?? [] ); } public function toArray(): array { return [ 'db' => $this->db, 'rabbitmq' => $this->rabbitmq, 'elasticsearch' => $this->elasticsearch, 'custom_field' => $this->customField, ]; } public function getConnectionConfig(string $type): ?array { return match ($type) { 'db' => $this->db, 'rabbitmq' => $this->rabbitmq, 'elasticsearch' => $this->elasticsearch, 'custom' => $this->customField, default => null, }; } }
2. Update Configuration
In config/connection-storage.php:
'dto' => [ 'class' => App\DTOs\YourCustomConnectionDTO::class, ],
Data Architecture
External API Data Format
The package expects the external API to return data in the following format:
{
"service1": {
"db": {
"host": "localhost",
"database": "service1_db",
"username": "user",
"password": "pass"
},
"rabbitmq": {
"host": "rabbitmq-host",
"port": 5672,
"username": "rabbit_user",
"password": "rabbit_pass"
}
},
"service2": {
"db": {
"host": "localhost",
"database": "service2_db",
"username": "user2",
"password": "pass2"
}
}
}
The package will automatically extract data for your service based on CONNECTION_STORAGE_SERVICE_KEY.
Validation and Notifications
Automatic Validation
The package automatically checks:
- Database connection availability
- Database existence
On errors:
- HTTP notifications are sent to
CONNECTION_STORAGE_NOTIFICATION_URL - Connection is temporarily blocked for
CONNECTION_STORAGE_BLOCK_DURATION
Disabling Validation
CONNECTION_STORAGE_VALIDATION_ENABLED=false
Interfaces
ConnectionDataProviderInterface
Main interface for working with connection data:
interface ConnectionDataProviderInterface { public function getConnectionData(string $identifier): ?BaseConnectionDataDTO; public function cacheConnectionData(string $identifier, BaseConnectionDataDTO $connectionData): bool; }
ConnectionCacheServiceInterface
Interface for cache operations:
interface ConnectionCacheServiceInterface { public function getConnectionData(string $url): ?BaseConnectionDataDTO; public function getRawConnectionData(string $url): ?array; public function cacheConnectionData(string $url, BaseConnectionDataDTO $connectionData): bool; public function set(string $url, BaseConnectionDataDTO $connectionData, ?int $ttl = null): bool; public function forget(string $url): bool; public function remember(string $url, callable $callback, ?int $ttl = null): ?BaseConnectionDataDTO; }
License
MIT License
Support
For questions and support, contact the MCRM development team.
mcrm/laravel-connection-storage 适用场景与选型建议
mcrm/laravel-connection-storage 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 269 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「database」 「cache」 「laravel」 「dto」 「microservices」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mcrm/laravel-connection-storage 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mcrm/laravel-connection-storage 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mcrm/laravel-connection-storage 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
repository php library
Microservice RPC through message queues.
Store your language lines in the database, yaml or other sources
The CodeIgniter Redis package
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 269
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-09