A high-performance service distribution system built with ReactPHP, designed to distribute service calls across multiple master nodes efficiently and evenly.
- Even distribution of service calls across available master nodes
- Real-time load balancing based on call counts
- Input validation for service calls
- JSON-based API interface
- Built with ReactPHP for high performance and non-blocking I/O
Install via Composer:
composer require reactphp-x/micro-service -vvv
- PHP 8.0 or higher
- ReactPHP HTTP ^1.11
- ReactPHP X Register Center ^1.0
use ReactphpX\MicroService\ServerMiddleware;
use ReactphpX\RegisterCenter\Register;
use React\Http\HttpServer;
use React\Socket\SocketServer;
// Initialize the register center
$register = new Register(8010);
// Create the server middleware
$middleware = new ServerMiddleware($register);
// Setup HTTP server
$server = new HttpServer($middleware);
// Listen on port 8080
$socket = new SocketServer('127.0.0.1:8080');
$server->listen($socket);
Send a POST request to your server with the following JSON structure:
{
"server_calls": {
"service_name1": [
{
"method": "methodName",
"params": ["param1", "param2"]
},
{
"method": "anotherMethod",
"params": []
}
],
"service_name2": [
{
"method": "someMethod",
"params": {"key": "value"}
}
]
}
}
For successful requests, you'll receive a response with the distributed calls:
{
"service_name1": [
"methodName" : result,
"anotherMethod" : result
],
"service_name2": [
"someMethod" : result
]
}
For invalid requests, you'll receive a 400 Bad Request response:
error result
{
"code": 1,
"errorCode": 500,
"msg": "Error description here",
"data": null
}
success result
{
"code": 0,
"errorCode": 0,
"msg": "Success",
"data": {
}
}
The service validates all incoming requests and will return appropriate error messages for:
- Invalid server_calls format (must be an array)
- Invalid service names (must be strings)
- Invalid call format (must be arrays with method and params)
- Missing or invalid method names
- Missing or invalid parameters
The service automatically distributes calls across available master nodes using the following strategy:
- Maintains a count of calls per master node
- Assigns new calls to the least loaded node
- Ensures even distribution of load across all available nodes
MIT
wpjscc [email protected]
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request