My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Moleculer, a modern microservices framework for NodeJS

Moleculer, a modern microservices framework for NodeJS

Icebob's photo
Icebob
·Jun 6, 2017

I would like to announce that as the outcome of a half year’s hard work I created a brand-new microservices framework for NodeJS.

What is Moleculer?

Moleculer is an open-source fast & flexible framework, licensed under MIT. It contains most of all important microservices features (service registry, auto-discovery, load balancing, circuit-breaker…etc).

Key features

  • Promise-based solution (with Bluebird)
  • request-reply concept
  • event bus system
  • supports middlewares
  • built-in caching solution (memory, Redis)
  • pluggable transporters (NATS, MQTT, Redis)
  • pluggable serializers (JSON, Avro, MsgPack, Protocol Buffer)
  • load balanced requests (round-robin, random)
  • auto discovery services
  • health monitoring, metrics & statistics

Install

Moleculer is available as an npm package. You can install it with npm or yarn

$ npm install moleculer

Usage

This simple example show you how easy it is to create a service in Moleculer and call it.

As you can see above, we created a math service which has an add action. This action is able to add two numbers, the values of “a” and “b” parameters. After it created, we can call it with thebroker.call method. The first parameter is the calling path (service name + action name), the second parameter is the params which is passed to the action handler wrapped to a Context object.

You can try it in your browser onRunkit!

Create a project

Use the Moleculer CLI tool to create a new Moleculer based microservices project.

1) Install moleculer-cli globally

npm install moleculer-cli -g

2) Create a new project (named moleculer-demo)

moleculer init project-simple moleculer-demo

Add API Gateway and press Y to npm install

This starter project template creates a sample services (greeter) with Jest tests and an API Gateway.

3) Open project folder and start it

cd moleculer-demo
npm run dev

4) Open the http://localhost:3000/greeter/hello link in your browser. It will call the hello action of greeter service with two parameters and return with the result (8).

5) Open the http://localhost:3000/greeter/welcome?name=world link to call the welcome action of greeter service.

Congratulations! You created your first Moleculer based microservices project. Welcome to the microservices world!

If you want to learn more about Moleculer framework, check out our website https://moleculer.services or github.

Follow us on Twitter at @MoleculerJS or join to Gitter chat.

If you liked this, click the upvote below to show your support and spread the word.