yevhenlisovenko/nano-service 问题修复 & 功能扩展

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

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

yevhenlisovenko/nano-service

最新稳定版本:v4.0.0

Composer 安装命令:

composer require yevhenlisovenko/nano-service

包简介

nano-service

README 文档

README

Introduction

Nano-Service is a PHP package designed for building event-driven microservices using RabbitMQ as a message broker. This package provides an abstraction layer for publishing and consuming events, facilitating communication between microservices in a loosely coupled architecture.

Features

  • Publish and consume messages through RabbitMQ
  • Event-driven architecture for microservices
  • Message standardization using NanoServiceMessage
  • Scalable & decoupled services with message queues
  • Flexible integration with any PHP project

Installation

To install the package via Composer, run:

composer require yevhenlisovenko/nano-service

Ensure that RabbitMQ is installed and running. You can use Docker to start RabbitMQ quickly:

docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management

Access RabbitMQ at http://localhost:15672 (username: guest, password: guest).

Usage

1. Creating a Publisher (Event Emitter)

The NanoPublisher class allows you to send messages to a specific queue.

require 'vendor/autoload.php';

use NanoService\NanoPublisher;

$publisher = new NanoPublisher('amqp://guest:guest@localhost:5672');
$publisher->publish('order.created', ['order_id' => 123, 'amount' => 500]);

2. Creating a Consumer (Event Listener)

The NanoConsumer class listens for incoming messages and processes them.

require 'vendor/autoload.php';

use NanoService\NanoConsumer;

$consumer = new NanoConsumer('amqp://guest:guest@localhost:5672', 'order.created');
$consumer->consume(function ($message) {
    echo "Received event: ", json_encode($message), "\n";
});

3. Standardizing Messages

The NanoServiceMessage class ensures consistent message formatting across services.

use NanoService\NanoServiceMessage;

$message = new NanoServiceMessage('order.created', ['order_id' => 123, 'amount' => 500]);
echo $message->toJson();

4. Implementing a Microservice

The NanoServiceClass helps build services that listen for messages and respond accordingly.

use NanoService\NanoServiceClass;

class OrderService extends NanoServiceClass {
    public function handleMessage($message) {
        echo "Processing Order: ", json_encode($message), "\n";
    }
}

$orderService = new OrderService('amqp://guest:guest@localhost:5672', 'order.created');
$orderService->start();

Configuration

You can configure RabbitMQ settings via environment variables:

RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASS=guest

Deployment

To deploy your microservices, you can use Docker:

FROM php:8.1-cli
WORKDIR /app
COPY . .
RUN composer install
CMD ["php", "consumer.php"]

Then build and run the service:

docker build -t order-service .
docker run -d order-service

Future Enhancements

  • Support for Kafka, Redis Pub/Sub, and Google Pub/Sub
  • More examples and tutorials
  • Automatic reconnection to RabbitMQ in case of failure

License

Nano-Service is open-source and licensed under the MIT License.

Contributing

Feel free to open an issue or submit a pull request to improve the package!

统计信息

  • 总下载量: 3.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-20