niladalia / rabbit-messenger-bundle
A simple Symfony bundle that says 'Hello, World!'
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.2
- symfony/amqp-messenger: ^5.0|^6.0|^7.0|^8.0
- symfony/framework-bundle: ^5.0|^6.0|^7.0|^8.0
- symfony/messenger: ^5.0|^6.0|^7.0|^8.0
- symfony/monolog-bundle: ^3.10
This package is not auto-updated.
Last update: 2024-11-13 11:48:05 UTC
README
This repository contains a simple template for building event-driven microservices using the Rabbit Messenger Bundle.
Requirements
You need to have a running RabbitMQ configuration or a Docker setup to use this bundle effectively.
Installation
To integrate the Rabbit Messenger Bundle into your project, run the following command:
composer update niladalia/rabbit-messenger-bundle
Usage
Domain Events
Create Domain Events
Your domain event classes should extend the DomainEvent
class provided by the bundle:
namespace RabbitMessengerBundle\Domain\Event; class YourDomainEvent extends DomainEvent { // Your event properties and methods }
Publishing Events
To publish events, inject the DomainEventPublisherInterface
into the classes where you need to publish events. Call the publish
method as shown below:
use RabbitMessengerBundle\Domain\Event\DomainEventPublisherInterface; class YourService { private $eventPublisher; public function __construct(DomainEventPublisherInterface $eventPublisher) { $this->eventPublisher = $eventPublisher; } public function someMethod() { $event = new YourDomainEvent(/* event data */); $this->eventPublisher->publish($event); } }
Configuration
Messenger Configuration
In the messenger.yaml
file of both your sender application and listener, specify the serializer as follows:
serializer: RabbitMessengerBundle\Infrastructure\Serializer\MessengerSerializer
Event Listener Configuration
Implement the Domain Event Mapper
Your listener or receiver micro should implement the DomainEventMapperInterface
.
This interface is responsible for mapping event types to the corresponding event classes in your microservice.
Your implementation must return an array with the following keys:
$map = [ 'aggregateId' 'eventId' 'attributes' 'occurredOn' ];
It will also need to return a string with the full path for the domain event class of the recieved event from Rabbit.
Service Configuration
In the services.yaml
file, specify the implementation for the DomainEventMapperInterface
and ensure it is public:
App\Shared\Infrastructure\Event\DomainEventMapper: public: true # Ensure the mapper is accessible RabbitMessengerBundle\Domain\Event\Mapper\DomainEventMapperInterface: '@App\Shared\Infrastructure\Event\DomainEventMapper'