mrthito/microservice
Composer 安装命令:
composer require mrthito/microservice
包简介
Lightweight PHP microservice foundation: env, logging, Redis queue, signed events, and HTTP health checks.
README 文档
README
Lightweight PHP foundation for event-driven microservices that share a Laravel database and Redis queue — with zero framework dependencies.
Requirements
- PHP 8.4+
- Extensions:
json,openssl,pdo
Installation
composer require mrthito/microservice
Features
.envloading without external dependencies- Secure stdout logging with sensitive field redaction
- PDO MySQL connection helper
- Laravel
APP_KEYpayload decryption - Minimal Redis RESP client (BRPOP)
- HMAC-SHA256 signed queue event verification
- Minimal HTTP router with built-in
/healthroute by default Http\Serverfor one-line HTTP entrypointsboot.jsonservice manifest reader
Quick start
1. Implement service config
use MrThito\MicroService\Contracts\MicroServiceConfig; use MrThito\MicroService\Support\Env; use MrThito\MicroService\Support\Manifest; final readonly class Config implements MicroServiceConfig { public static function fromEnvironment(string $basePath): self { $manifest = Manifest::load($basePath); return new self( serviceName: $manifest['name'], // ... map Env::getString(), Env::getInt(), etc. ); } // Implement MicroServiceConfig methods... }
2. Wire the queue worker
use MrThito\MicroService\Bootstrap; use MrThito\MicroService\Queue\RedisQueueListener; use MrThito\MicroService\Security\SignedEventVerifier; use MrThito\MicroService\Support\BaseConfigValidator; use MrThito\MicroService\Support\Logger; $config = Bootstrap::boot('/path/to/service', fn () => Config::fromEnvironment('/path/to/service')); BaseConfigValidator::validate($config); $logger = new Logger($config->logLevel()); $verifier = new SignedEventVerifier( expectedEvent: 'order.created', signingSecret: $config->signingSecret(), eventMaxAgeSeconds: $config->eventMaxAgeSeconds(), payloadValidator: static fn (array $payload): array => [ 'order_id' => (int) $payload['order_id'], ], ); $listener = new RedisQueueListener( redisConfig: $config->redis(), verifier: $verifier, processor: $orderProcessor, logger: $logger, ); $listener->listen();
3. Expose the HTTP server
Health routes (/health and /) are registered automatically.
use MrThito\MicroService\Http\Server; $config = Bootstrap::boot('/path/to/service', fn () => Config::fromEnvironment('/path/to/service')); (new Server($config))->run();
Add custom routes before serving:
$server = new Server($config); $server->router()->get('/metrics', static fn (): array => ['uptime' => time()]); $server->run();
Service manifest
Each microservice should include a boot.json file in its project root:
{
"name": "my_microservice",
"license": "MIT",
"scopes": ["orders.process"],
"health": "/health"
}
Load it with Manifest::load($basePath). The health path is exposed via MicroServiceConfig::healthPath() and used by the default router.
Security
- Sign queue payloads with
EventSigner::sign($payload, $secret)before publishing. - Verify events with
SignedEventVerifierbefore processing. - Set
REQUIRE_REDIS_PASSWORD=truein production when Redis uses AUTH. - Use signing secrets of at least 32 characters.
Testing
composer test
Publishing to Packagist
- Push this repository to GitHub (for example
github.com/mrthito/microservice). - Create a release tag:
git tag v1.0.0 && git push origin v1.0.0. - Submit the repository URL at packagist.org.
- Enable the Packagist GitHub hook for automatic updates on new tags.
License
The MIT License (MIT). Please see LICENSE for more information.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-31