@techzenith-technologies/event-orchestrator
v1.0.3
Published
Un package offrant un moyen simple et structuré de créer, d'émettre et d'écouter des événements, facilitant la communication entre les différents composants du système.
Downloads
485
Readme
Orchestrateur d'Événements (Kafka & RabbitMQ)
@techzenith-technologies/event-orchestrator est une solution d'orchestration d'événements de classe mondiale, entièrement pilotée par la configuration. Ce package abstrait les complexités protocolaires de Kafka et RabbitMQ, permettant aux équipes de se concentrer exclusivement sur la logique métier.
Fonctionnalités
- Support hybride multi-broker : Intégrez Kafka et RabbitMQ de manière transparente au sein de la même application.
- Piloté par la configuration : Définissez toute votre infrastructure d'événements dans une configuration unique et déclarative.
- Résilience native : Gestion intégrée des tentatives (retries) avec backoff exponentiel et reconnexion automatique.
- Pipeline de Middleware : Support extensible pour le logging, la validation ou la transformation des messages.
- Abstraction de protocole : API unifiée pour émettre et consommer des événements, quel que soit le broker sous-jacent.
Installation
npm install @techzenith-technologies/event-orchestratorConfiguration
La configuration du système est définie lors de la phase d'initialisation. Vous pouvez configurer un seul broker ou une architecture hybride en utilisant un tableau de définitions.
Exemple Hybride (Kafka & RabbitMQ)
const config = {
brokers: [
{
name: 'kafka-primary',
type: 'kafka',
connection: {
brokers: ['localhost:9092'],
clientId: 'app-service',
groupId: 'order-group'
},
retryOptions: {
retries: 5,
minTimeout: 1000,
factor: 2
},
mappings: [
{ source: 'orders-topic', eventType: 'OrderCreated', actionName: 'processOrder' }
]
},
{
name: 'rabbitmq-local',
type: 'rabbitmq',
connection: {
url: 'amqp://localhost'
},
mappings: [
{ source: 'notifications-queue', eventType: 'user.signup', actionName: 'sendEmail' }
]
}
]
};Usage
1. Define Actions & Directory Structure
Actions are plain JavaScript functions that handle event data. For small projects, you can define them in a single object. For production-grade applications, we recommend organizing them in an actions/ folder.
Recommended Structure
project/
└── src/
└── actions/ # Folder containing your business logic
├── orderActions.js # Exports { processOrder }
└── userActions.js # Exports { sendEmail }Example Action File (actions/orderActions.js)
exports.processOrder = async (data) => {
console.log('Processing order:', data.orderId);
// Business logic here...
};2. Initialize and Inject
The orchestrator can automatically load all actions from a directory. Use the path library to provide an absolute path to your actions folder during initialization.
const path = require('path');
const eventManager = require('@techzenith-technologies/event-orchestrator');
// Add optional middleware
eventManager.use(async (data, event) => {
console.log(`Received ${event}`);
});
async function start() {
const actionsPath = path.join(__dirname, 'actions');
await eventManager.initialize(config, actionsPath);
}3. Emit Events
await eventManager.emit('order.created', { orderId: 123, status: 'pending' });Error Handling & Retries
- Connection: RabbitMQ automatically attempts to reconnect every 5 seconds if the connection drops. KafkaJS uses its internal retry strategy for client operations.
- Actions: If an action (or middleware) fails, the package will retry the execution using exponential backoff based on the
retryOptionsprovided in the config.
