mrthito/microservice 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mrthito/microservice

Composer 安装命令:

composer require mrthito/microservice

包简介

Lightweight PHP microservice foundation: env, logging, Redis queue, signed events, and HTTP health checks.

README 文档

README

Latest Version on Packagist Total Downloads License

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

  • .env loading without external dependencies
  • Secure stdout logging with sensitive field redaction
  • PDO MySQL connection helper
  • Laravel APP_KEY payload decryption
  • Minimal Redis RESP client (BRPOP)
  • HMAC-SHA256 signed queue event verification
  • Minimal HTTP router with built-in /health route by default
  • Http\Server for one-line HTTP entrypoints
  • boot.json service 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 SignedEventVerifier before processing.
  • Set REQUIRE_REDIS_PASSWORD=true in production when Redis uses AUTH.
  • Use signing secrets of at least 32 characters.

Testing

composer test

Publishing to Packagist

  1. Push this repository to GitHub (for example github.com/mrthito/microservice).
  2. Create a release tag: git tag v1.0.0 && git push origin v1.0.0.
  3. Submit the repository URL at packagist.org.
  4. 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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固