moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

Documentation on how-to-use-in-production

Open thatisuday opened this issue 7 years ago • 3 comments

I appreciate your hardwork and this seems to be a very promising upcoming framework for creating microservices. I have tried Senecajs and I didn't quite like the architecture.

On other hand, moleculer is great but I still didn't get how to use it in production. Here are following use cases.

  • How am I supposed to start a service and keep it running in background. Like pm2 does. What are the dependencies needed to do so. Can you give minimalistic example?
  • How to call an action from another script. Do we need to create a broker? Is using event emit/broadcast meant for this thing?
  • Is there any molecular-cli commands (not molecular-repl) to see registered services?
  • What is $node service and why it's important?
  • What are Transporters and why they are important?
  • built-in service registry & auto discovery -| How does it work?
  • moleculer-cli, moleculer-runner, molecular-repl are all CLI interfaces. It's very hard to keep up. Can't we just combine them all.

I don't have in depth knowledge of all the necessary building blocks of this module and I am sure other people would also like to know about this. Most of the documentation on the website contains configuration and API information. But I am still confused about how to use it in real life.

thatisuday avatar Jan 10 '18 09:01 thatisuday

Very good questions in my opinion. I also just try to use the Moleculer and I have the same ones. I'll try to answer a few of them with what I got to figure out myself - somewhere I can be wrong, so if something corrects me.

What are Transporters and why they are important?

As far as I understand, Transporters are needed just for Nodes to see each other's services, for this one needs some kind of transport of messages. Moleculer provides three different options.

How am I supposed to start a service and keep it running in background. Like pm2 does. What are the dependencies needed to do so. Can you give minimalistic example?

That's just in order for the broker keep running in the background - you need something that will make it be a service. The easiest way is to create the API Gateway with moleculer-web. In this case, the Service Broker will just keep running to process the requests of the API Gateway, and here is an example.

How to call an action from another script. Do we need to create a broker? Is using event emit/broadcast meant for this thing?

Here it is just more complicated and the task and truth is not trivial. And I did not find an example, so I'll try to explain how it turned out for me. I launched two brokers - one of them provides services, the second only uses the services of the first. And in this case, we need Transporters - in order for both brokers to see each other's services. Moreover, by connecting to the Transporters - brokers are keep running in the background. For this, I launched a NATS server(nats://192.168.251.106:4222 in my example), which connects the requests of my two brokers.

First broker, witch provides service with actions add & sub

const { ServiceBroker } = require("moleculer");
let broker = new ServiceBroker({
    nodeID: "gate",
    cacher: "memory",
    logger: console,
    transporter: "nats://192.168.251.106:4222"
});


broker.createService({
    name: "math",
    actions: {
        add(ctx) {
            return Number(ctx.params.a) + Number(ctx.params.b);
        },

        sub(ctx) {
            return Number(ctx.params.a) - Number(ctx.params.b);
        }
    }
});

broker.start();

Second broker, witch use this actions

const { ServiceBroker } = require("moleculer");

let broker = new ServiceBroker({
    nodeID: "gate_client",
    cacher: "memory",
    logger: console,
    transporter: "nats://192.168.251.106:4222"
});

broker.start();

function timefunction() {

    let the_a = Math.random() * (100 - 0);
    let the_b = Math.random() * (100 - 0);

    broker.call("math.add", { a: the_a, b: the_b })
        .then(res => console.log( the_a + " + " + the_b + " =", res))
        .catch(err => console.error(`Error occured! ${err.message}`));

}

setInterval(timefunction, 1000);

built-in service registry & auto discovery -| How does it work?

Node registration in the Transporters enables ability to search for nodes and their services, as I understand it

suhamera avatar Feb 14 '18 22:02 suhamera

@suhamera's answer about transporters is correct. In addition, all calls and events (and other packet types) will go through the transporter you choose.

How to call an action from another script. Do we need to create a broker? Is using event emit/broadcast meant for this thing?

Are you asking how to call an action from a different node, or how to call an action from the CLI?

You can call an action from the CLI using https://github.com/ice-services/moleculer-repl If that doesn't fit your needs you could write your own script that sets up a broker.

Emit and broadcast are two ways of interacting between nodes. They are alternatives to "call". The main differences are that broadcast/emit won't get a response, and broadcasted/emitted events can be received by multiple nodes / services.

To call a different node's action, you would use broker.call('yourservice.youraction')

How am I supposed to start a service and keep it running in background. Like pm2 does. What are the dependencies needed to do so. Can you give minimalistic example?

You should be able to do this the same way you would do it for Express, or any other node server. Forever and PM2 should work fine, as far as I know.

I am not sure if suhamera is suggesting this, but you do not need to have moleculer-web setup in order to use Moleculer in production.

A side note I want to mention is that nodeIDs must be unique for each node. Using a nodeID like "gate", or "math" is fine for a single node, but if you want to run a single service on multiple nodes it is a problem. One solution is to put the PID or something in the nodeID as well. Ex: "gate-production-1532".

To learn more about $node, built-in service registry & auto discovery, I recommend reading the docs, reading the source, and experimenting. I looked into these things a while ago, but haven't re-investigated recently so my information could be out of date. @icebob wrote a nice explanation a while ago for packets, which ties into service discovery https://github.com/ice-services/moleculer/blob/master/docs/PROTOCOL.md

Nathan-Schwartz avatar Feb 15 '18 16:02 Nathan-Schwartz

How can we run an action using the runner? I can only load the service.

hongkongkiwi avatar Dec 06 '18 13:12 hongkongkiwi