Components in Moleculer
- Services: Core units of functionality, containing actions and event handlers.
- Actions: Methods exposed by services for direct invocation.
- Events: Mechanism for decoupled communication between services.
- Event Bus: Centralized system for managing events.
- API Gateway: Entry point for external clients, exposing REST/GraphQL endpoints.
- Transporter: Handles communication between nodes in a distributed setup.
- Middleware: Custom logic that can be applied to actions or events.
- Broker: The core of Moleculer, managing services, events, and communication.
Example Flow Diagram
Below is an example flow of a Moleculer-based application:
sequenceDiagram participant Client participant Gateway as API Gateway participant ServiceA as Service A participant ServiceB as Service B participant EventBus as Event Bus Client->>Gateway: HTTP Request (REST/GraphQL) Gateway->>ServiceA: Call Action (e.g., `getUser`) ServiceA->>ServiceB: Call Action (e.g., `getUserDetails`) ServiceB-->>ServiceA: Response (User Details) ServiceA-->>Gateway: Response (User Data) Gateway-->>Client: HTTP Response ServiceA->>EventBus: Emit Event (e.g., `user.updated`) EventBus->>ServiceB: Broadcast Event (e.g., `user.updated`) EventBus->>ServiceC: Broadcast Event (e.g., `user.updated`)
Explanation of the Flow
-
Client Interaction:
- The client sends an HTTP request to the API Gateway.
- The Gateway forwards the request to the appropriate service (
Service A).
-
Service Communication:
Service Acalls an action inService Bto fetch additional data.Service Bprocesses the request and returns the response toService A.
-
Event Emission:
- After processing,
Service Aemits an event (user.updated) to the Event Bus. - The Event Bus broadcasts the event to all subscribed services (
Service B,Service C).
- After processing,
-
Response to Client:
- The final response is sent back to the client via the API Gateway.