Moleculer
Moleculer is a fast & powerful microservices framework for NodeJS (>= v6.x).
Website: https://moleculer.services
Documentation: https://moleculer.services/docs
What's included
- Promise-based solution
- request-reply concept
- event bus system
- supports middlewares
- service mixins
- multiple services on a node/server
- built-in caching solution (memory, Redis)
- pluggable transporters (NATS, MQTT, Redis)
- pluggable serializers (JSON, Avro, MsgPack, Protocol Buffer)
- load balanced requests (round-robin, random)
- every nodes are equal, no master/leader node
- auto discovery services
- parameter validation with fastest-validator
- distributed timeout handling with fallback response
- health monitoring, metrics & statistics
- supports versioned services (run different versions of the service)
- official API gateway module
Installation
$ npm install moleculer --save
or
$ yarn add moleculer
Create your first microservice
This example shows you how to create a small service with an add
action which can add two numbers.
const { ServiceBroker } = require("moleculer");
let broker = new ServiceBroker({ logger: console });
broker.createService({
name: "math",
actions: {
add(ctx) {
return Number(ctx.params.a) + Number(ctx.params.b);
}
}
});
broker.start();
broker.call("math.add", { a: 5, b: 3 })
.then(res => console.log("5 + 3 =", res))
.catch(err => console.error(`Error occured! ${err.message}`));
Try it on Runkit
Create a Moleculer project
Use the Moleculer CLI tool to create a new Moleculer based microservices project.
-
Install moleculer-cli globally
$ npm install moleculer-cli -g
-
Create a new project (named first-demo
)
$ moleculer init project-simple first-demo
Press Y on API Gateway & npm install
-
Open project folder
$ cd first-demo
-
Start project
$ npm run dev
-
Open the http://localhost:3000/greeter/welcome?name=world link in your browser. It will call the welcome
action of greeter
service with a name
param via API gateway and returns with the result.
:tada:Congratulations! Your first Moleculer based microservices project is created. Read our documentation to learn more about Moleculer.
Official modules
We have many official modules for Moleculer. Check our list!
Documentation
You can find here the documentation.
Changelog
See CHANGELOG.md.
Roadmap
See ROADMAP.md.
Contributions
We welcome you to join the development of Moleculer. Please read our contribution guide.
License
Moleculer is available under the MIT license.
3rd party licenses
Contact
Copyright (c) 2016-2017 Ice Services
0.10.0 (2017-08-20)
Breaking changes
No more nodeID == null
in local stuff
In all core modules removed the nullable nodeID
. Every places (context, events, $node.* results) the nodeID contains a valid (local or remote) nodeID. On local nodes it equals with broker.nodeID
.
Migration guide
Before:
if (ctx.nodeID == null) { ... }
// ---------
events: {
"users.created"(payload, sender) {
if (sender == null) { ... }
}
}
After:
if (ctx.nodeID == ctx.broker.nodeID) { ... }
// ---------
events: {
"users.created"(payload, sender) {
if (sender == this.broker.nodeID) { ... }
}
}
internalActions
is renamed to internalServices
The internalActions
broker option is renamed to internalServices
.