Anser-EDA is a hybrid framework that combines orchestration and choreography patterns for event-driven architecture. It helps developers smoothly transition from traditional API architecture to asynchronous event-driven architecture while maintaining existing API structures. The framework integrates Saga patterns with RabbitMQ to implement distributed transactions.
Anser-EDA/
│
├── Events/ # Event Classes
│ ├── InventoryDeductedEvent.php
│ ├── OrderCompletedEvent.php
│ ├── OrderCreateRequestedEvent.php
│ ├── OrderCreatedEvent.php
│ ├── PaymentProcessedEvent.php
│ ├── RollbackInventoryEvent.php
│ └── RollbackOrderEvent.php
│
├── Filters/ # Event Filters
│ ├── FailHandlerFilter.php
│ └── JsonDoneHandlerFilter.php
│
├── Logs/ # Application Logs
│
├── Sagas/ # Saga Process Coordination
│ └── OrderSaga.php
│
├── Services/ # Microservice Abstractions
│ ├── Models/
│ │ ├── ModifyProduct.php
│ │ └── OrderProductDetail.php
│ ├── OrderService.php
│ ├── ProductionService.php
│ └── UserService.php
│
├── scripts/ # Auto-generated Scripts (Cross-platform)
│ ├── run_all_events.bat # Windows startup script
│ ├── run_all_events.sh # Linux/Mac startup script
│ ├── check_status.sh # Status checker (Linux/Mac)
│ ├── stop_all.sh # Stop all listeners (Linux/Mac)
│ └── README.md # Scripts documentation
│
├── src/ # Anser Framework Components
│ ├── Attributes/
│ │ └── EventHandler.php
│ ├── EventBus.php
│ ├── EventStore/
│ │ └── EventStoreDB.php
│ ├── HandlerScanner.php # Auto-scanning & script generation
│ ├── HandlerScannerInterface.php
│ ├── Interfaces/
│ ├── MessageQueue/
│ │ ├── Consumer.php
│ │ ├── MessageBus.php
│ │ └── RabbitMQConnection.php
│ └── Saga.php
│
├── tmp/ # Temporary Files (auto-created)
│ ├── pids/ # Process PID files
│ └── logs/ # Event listener logs
│
├── vendor/ # Composer Dependencies
│
├── clear_queue.php # Queue clearing script
├── composer.json # Composer configuration
├── consumer.php # Event consumer main program
├── docker-compose.yml # Docker configuration
├── generate_script.php # Auto-generate cross-platform scripts
├── initialization.php # RabbitMQ initialization
├── init.php # Application initialization
└── publisher.php # Event publisher
-
Install Composer dependencies:
composer install
-
Start RabbitMQ and EventStore services:
docker-compose up -d
-
Initialize RabbitMQ queues:
php initialization.php
-
Generate cross-platform scripts:
php generate_script.php
The framework automatically scans #[EventHandler] annotations in your Saga files and generates cross-platform scripts:
# Generate all scripts automatically
php generate_script.phpThis will create:
scripts/run_all_events.bat(Windows)scripts/run_all_events.sh(Linux/Mac)scripts/check_status.sh(Linux/Mac)scripts/stop_all.sh(Linux/Mac)
# Run all event listeners (opens separate windows)
scripts\run_all_events.bat# Start all listeners in background
./scripts/run_all_events.sh
# Check listener status
./scripts/check_status.sh
# Stop all listeners
./scripts/stop_all.sh
# View logs
tail -f tmp/logs/*.logYou can also run individual event consumers manually:
php consumer.php OrderCreateRequestedEvent
php consumer.php OrderCreatedEvent
php consumer.php InventoryDeductedEvent
php consumer.php PaymentProcessedEvent
php consumer.php RollbackInventoryEvent
php consumer.php RollbackOrderEventThe system implements a complete order processing saga with the following events:
- OrderCreateRequestedEvent - Receives order creation request
- OrderCreatedEvent - Order successfully created
- InventoryDeductedEvent - Inventory deduction completed
- PaymentProcessedEvent - Payment processing completed
- RollbackInventoryEvent - Inventory rollback (compensation)
- RollbackOrderEvent - Order cancellation (compensation)
The OrderSaga class orchestrates the entire order process:
- Success Path: OrderCreateRequested → OrderCreated → InventoryDeducted → PaymentProcessed → OrderCompleted
- Failure Path: Any failure triggers compensation events (RollbackInventory → RollbackOrder)
- Automatically scans
#[EventHandler]annotations - Generates corresponding RabbitMQ queues
- Creates cross-platform management scripts
- Windows: Batch files with separate CMD windows
- Linux/Mac: Shell scripts with background execution and PID management
- Process status monitoring
- Centralized log management
- Graceful shutdown capabilities
-
Add New Event Handler:
#[EventHandler] public function onNewEvent(NewEvent $event) { // Your logic here }
-
Regenerate Scripts:
php generate_script.php
-
Restart Listeners:
- Windows: Re-run
scripts\run_all_events.bat - Linux/Mac:
./scripts/stop_all.sh && ./scripts/run_all_events.sh
- Windows: Re-run
- Hybrid Pattern: Combines orchestration (Saga) with choreography (Event-driven)
- Event Sourcing: Integration with EventStore
- Message Queuing: RabbitMQ for reliable message delivery
- Saga Coordination: Centralized transaction management with compensation
- Auto-scaling Ready: Each event type runs in separate processes
# Linux/Mac
tail -f tmp/logs/*.log
# Windows
# Check individual CMD windows or log files in tmp/logs/# Linux/Mac only
./scripts/check_status.sh# Stop all listeners
./scripts/stop_all.sh # Linux/Mac
# Or close all CMD windows on Windows
# Clear queues
php clear_queue.php