From 387d6c3d4849fda6e13bf10942cdfe3682dbf48e Mon Sep 17 00:00:00 2001 From: Julien Le Coupanec Date: Sat, 10 Mar 2018 17:11:24 -0800 Subject: [PATCH] Moleculer.js updates --- backend/moleculer.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/backend/moleculer.js b/backend/moleculer.js index 1ddc1ad..4af4538 100644 --- a/backend/moleculer.js +++ b/backend/moleculer.js @@ -5,18 +5,22 @@ * Version: 0.12.x * ******************************************************************************************* */ + /* ******************************************************************************************* * Install Moleculer * ******************************************************************************************* */ + ```bash npm i moleculer ``` + /* ******************************************************************************************* * SERVICE BROKER OPTIONS * ******************************************************************************************* */ + // All ServiceBroker options with default values const broker = new ServiceBroker({ namespace: "", // Namespace for node segmentation @@ -73,10 +77,12 @@ const broker = new ServiceBroker({ ContextFactory: null // Custom Context factory class }); + /* ******************************************************************************************* * SERVICE BROKER METHODS * ******************************************************************************************* */ + // Broker properties broker.Promise // Pointer to Bluebird Promise lib broker.namespace // Namespace from options @@ -121,10 +127,12 @@ broker.sendPing(nodeID); // Ping a remote node broker.MOLECULER_VERSION // Version number of Moleculer lib broker.PROTOCOL_VERSION // Version number of Moleculer protocol + /* ******************************************************************************************* * BROKER SERVICE CALLS * ******************************************************************************************* */ + // Call the "users.get" service with params broker.call("users.get", { id: 150 }).then(user => console.log(user)); @@ -150,10 +158,12 @@ const res = await broker.mcall({ }); console.log(res.posts, res.users); + /* ******************************************************************************************* * BROKER EVENTS * ******************************************************************************************* */ + // Send a balanced event with payload broker.emit("user.created", { user: user }); @@ -166,11 +176,13 @@ broker.broadcast("user.created", { user: user }); // Send a broadcast event only for "mail" and "payment" services (all instances) broker.broadcast("user.created", { user: user }, ["mail", "payment"]); + /* ******************************************************************************************* * NATS TRANSPORTER * Requirement: `npm i nats` * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "NATS" @@ -208,11 +220,13 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * REDIS TRANSPORTER * Requirement: `npm i ioredis` * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "Redis" @@ -243,6 +257,7 @@ const broker = new ServiceBroker({ * Requirement: `npm i mqtt` * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "MQTT" @@ -266,11 +281,13 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * AMQP TRANSPORTER * Requirement: `npm i amqplib` * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "AMQP" @@ -293,11 +310,13 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * KAFKA TRANSPORTER * Requirement: `npm i kafka-node` * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "Kafka" @@ -339,11 +358,13 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * NATS STREAMING TRANSPORTER * Requirement: `npm i node-nats-streaming` * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "STAN" @@ -365,11 +386,13 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * TCP STREAMING TRANSPORTER * No requirements * ******************************************************************************************* */ + // Default options const broker = new ServiceBroker({ transporter: "TCP" @@ -444,6 +467,7 @@ const broker = new ServiceBroker({ * http://moleculer.services/docs/cachers.html * ******************************************************************************************* */ + // Memory cacher const broker = new ServiceBroker({ cacher: "Memory" @@ -489,10 +513,12 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * Manual caching * ******************************************************************************************* */ + // Save to cache broker.cacher.set("mykey.a", { a: 5 }); @@ -508,11 +534,13 @@ broker.cacher.clean("mykey.*"); // Clean all entries broker.cacher.clean(); + /* ******************************************************************************************* * SERIALIZER * http://moleculer.services/docs/serializers.html * ******************************************************************************************* */ + // JSON serializer (default) const broker = new ServiceBroker({ serializer: "JSON" @@ -533,10 +561,12 @@ const broker = new ServiceBroker({ serializer: "MsgPack" }); + /* ******************************************************************************************* * STRATEGY * ******************************************************************************************* */ + // Round-robin strategy (default) const broker = new ServiceBroker({ registry: { @@ -569,11 +599,13 @@ const broker = new ServiceBroker({ } }); + /* ******************************************************************************************* * LOGGER * http://moleculer.services/docs/logger.html * ******************************************************************************************* */ + // Logger methods broker.logger.fatal(); broker.logger.error(); @@ -602,10 +634,12 @@ const broker = new ServiceBroker({ logger: bindings => logger.child(bindings) }); + /* ******************************************************************************************* * SERVICE SCHEMA * ******************************************************************************************* */ + module.exports = { // Name name: "greeter", @@ -673,10 +707,12 @@ module.exports = { } }; + /* ******************************************************************************************* * SERVICE * ******************************************************************************************* */ + this.name // Name of service this.version // Version of service this.settings // Settings of service @@ -687,10 +723,12 @@ this.logger // Logger instance this.actions // Actions of service. this.waitForServices // Pointer to ‘broker.waitForServices’ method + /* ******************************************************************************************* * CONTEXT * ******************************************************************************************* */ + ctx.id // Context ID ctx.broker // Broker instance ctx.action // Action definition