The development of a microservices-based application involves multiple moving parts. It’s a process of designing/creating complex interactions between several services. As the application grows in complexity it becomes hard to create a mental picture of service relations and their interactions, which inevitably slows the development and the debugging. A tool that can provide a graphical overview of the system and track the interactions between the services can greatly alleviate the cognitive load of the developer and, therefore, result in a significant speed-up during the application development.
Moleculer’s Lab is a dashboard designed to solve the abovementioned issues during the development of a Moleculer-based application. The dashboard includes a global architecture overview, information about system load, service calls, events, and much more.
In this article, you will see how to create a microservice application developed in the Node.js environment, based on the Moleculer framework, and also how to integrate Moleculer’s Lab into the project and have a complete dashboard for our microservices.
What is Moleculer?
Moleculer is an open-source fast & flexible microservices framework, licensed under MIT. It contains most of all important microservices features (service registry, auto-discovery, load balancing, circuit-breaker, bulkhead, fallback response…etc).¹
What is Moleculer’s Laboratory?
Moleculer’s Laboratory is a developer tool for Moleculer-based microservices projects. It is still only available for Beta Testers. To join beta testing, fill out this form.²
Hands-on
Let’s use an example to demonstrate the power of Moleculer’s Lab.
System overview
This example is a fictitious travel control system responsible for validating a trip by checking the customer’s body temperature and if the customer’s name is registered in a suspect list. The following services will be implemented:
- api: The API gateway, responsible for receiving all requests and redirecting to the corresponding internal service.
- passengers: Service responsible for receiving passenger's name and checking their temperature. If the temperature is greater than or equal to 38 degrees, an event
fever.detected
is emitted warning that fever was detected and doctors will be awaited.
Then, the passenger’s name will be consulted in another service that has a list of suspects. If the passenger is a suspect, he will be arrested, otherwise, his trip will be allowed. - doctors: Service responsible for listening to events
fever.detected
and providing appropriate care. - suspects: A service responsible for providing a list of known suspects.
- lab: A service that will be the agent of the Moleculer’s Lab.
Implementation
1. First, install Moleculer CLI.
npm i moleculer-cli -g
2. Create a new project based on moleculer-demo template.
moleculer init project moleculer-demo
The CLI is going to ask you some questions regarding the template configuration. To make things simple, just follow these options::
3. Go to the new folder created and open it with your favorite editor. Now, let’s review the template and add the services that are necessary to implement our travel control system.
- Create suspects.service.js service in the services folder and copy the following code:
- Create doctors.service.js service in the services folder and copy the following code:
- Create passengers.service.js service in the services folder and copy the following code:
4. Install Lab Agent service with:
npm i @moleculer/lab --save
5. Create lab.service.js service in the services folder and copy the following code:
6. Create a .env file with your credentials:
LAB_TOKEN=labdemo
LAB_APIKEY=
1. Protect the access from others (if you expose the Agent service to the internet). It can be a static string, or if it’s not set, a random token will be generated and printed to the console. Env var: LAB_TOKEN
2 . The API key is sent to users by e-mail. Env var: LAB_APIKEY. ²
7. To load the environment variables from the .env file, install dotenv package and change the package.json to include the option --env in dev script.
npm install dotenv
8. You can delete the greeter and products services from the services folder. They are included in the template by default and are not necessary for this example. After that, your file structure should look like this:
9. Adjust the moleculer.config.js
file
The lab package contains a logger, metrics reporter, and tracing exporter modules that collect and aggregate the internal state changes and events of the system.
- Logger. Moleculer supports multiple loggers. In addition to the console logger, let’s add the Laboratory logger. Configure the logger to look like this:
- Metrics. Export metrics to Moleculer’s Lab.
- Tracing. Export tracings to Moleculer’s Lab.
10. Start the application.
npm run dev
To check if API is ok, enter http://localhost:5000/api/suspects/ in the browser. Seven suspects will be listed. Great!
Moleculer’s Lab in action
After initializing the application, the Console logger will display a message saying that the Lab is online. To open the laboratory, follow the link printed in the log.
For security purposes, the GUI will ask you to confirm the token. In our case is labdemo, but can be any string created by you to protect your lab. Finally, press the CONNECT
button.
1. Overview
After the connection is established, you’ll see the initial tab providing a global view of the system. The dashboard includes the total node count, services, actions, events, and some general system stats.
- Top metrics
- Node Status
- Service Status
- Action Status
- Event Status
2. Nodes
The Nodes tab provides an overview of the deployed nodes, it can be a simple list or a graph.
3. Services
The Services tab lists the services currently available. Clicking on a service will provide service-related information and statistics. The Lab marks as “non-redundant” services running on a single instance, i.e., services without a replica.
4. Actions
Actions tab allows interacting with the actions exposed by the services.
To call an action (e.g.,passengers.check
) simply click on it. Fill in the parameters and press the CALL
button.
5. Events
The Events tab provides the list of event listeners.
To emit an event, click on an event listener (e.g. fever.detected
), fill payload, and press EMIT
button.
6. Tracing
The Tracing tab shows the action calls and their spans.
The “span” is the primary building block of a distributed trace, representing an individual unit of work done in a distributed system.³
7. Logs
The Log tab shows the system logs.
Conclusion
As demonstrated in this article, Moleculer’s Lab is a tool that can improve developer’s productivity, allowing them to quickly build and deploy moleculer-based microservices applications.
You can see the contents of this project in this repository: https://github.com/brasiqui/labdemo
If you need another demo to better understand the basis of the Moleculer, follow this article.⁴
Here you can find the complete documentation about Moleculer. If you have any questions please come chat with us at Discord. ⁵