mirror of
https://github.com/namibia/awesome-cheatsheets.git
synced 2024-11-23 21:27:40 +00:00
Moleculer.js updates
This commit is contained in:
parent
6ac16cc7e2
commit
387d6c3d48
@ -5,18 +5,22 @@
|
|||||||
* Version: 0.12.x
|
* Version: 0.12.x
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* Install Moleculer
|
* Install Moleculer
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm i moleculer
|
npm i moleculer
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* SERVICE BROKER OPTIONS
|
* SERVICE BROKER OPTIONS
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// All ServiceBroker options with default values
|
// All ServiceBroker options with default values
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
namespace: "", // Namespace for node segmentation
|
namespace: "", // Namespace for node segmentation
|
||||||
@ -73,10 +77,12 @@ const broker = new ServiceBroker({
|
|||||||
ContextFactory: null // Custom Context factory class
|
ContextFactory: null // Custom Context factory class
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* SERVICE BROKER METHODS
|
* SERVICE BROKER METHODS
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Broker properties
|
// Broker properties
|
||||||
broker.Promise // Pointer to Bluebird Promise lib
|
broker.Promise // Pointer to Bluebird Promise lib
|
||||||
broker.namespace // Namespace from options
|
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.MOLECULER_VERSION // Version number of Moleculer lib
|
||||||
broker.PROTOCOL_VERSION // Version number of Moleculer protocol
|
broker.PROTOCOL_VERSION // Version number of Moleculer protocol
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* BROKER SERVICE CALLS
|
* BROKER SERVICE CALLS
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Call the "users.get" service with params
|
// Call the "users.get" service with params
|
||||||
broker.call("users.get", { id: 150 }).then(user => console.log(user));
|
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);
|
console.log(res.posts, res.users);
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* BROKER EVENTS
|
* BROKER EVENTS
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Send a balanced event with payload
|
// Send a balanced event with payload
|
||||||
broker.emit("user.created", { user: user });
|
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)
|
// Send a broadcast event only for "mail" and "payment" services (all instances)
|
||||||
broker.broadcast("user.created", { user: user }, ["mail", "payment"]);
|
broker.broadcast("user.created", { user: user }, ["mail", "payment"]);
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* NATS TRANSPORTER
|
* NATS TRANSPORTER
|
||||||
* Requirement: `npm i nats`
|
* Requirement: `npm i nats`
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "NATS"
|
transporter: "NATS"
|
||||||
@ -208,11 +220,13 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* REDIS TRANSPORTER
|
* REDIS TRANSPORTER
|
||||||
* Requirement: `npm i ioredis`
|
* Requirement: `npm i ioredis`
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "Redis"
|
transporter: "Redis"
|
||||||
@ -243,6 +257,7 @@ const broker = new ServiceBroker({
|
|||||||
* Requirement: `npm i mqtt`
|
* Requirement: `npm i mqtt`
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "MQTT"
|
transporter: "MQTT"
|
||||||
@ -266,11 +281,13 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* AMQP TRANSPORTER
|
* AMQP TRANSPORTER
|
||||||
* Requirement: `npm i amqplib`
|
* Requirement: `npm i amqplib`
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "AMQP"
|
transporter: "AMQP"
|
||||||
@ -293,11 +310,13 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* KAFKA TRANSPORTER
|
* KAFKA TRANSPORTER
|
||||||
* Requirement: `npm i kafka-node`
|
* Requirement: `npm i kafka-node`
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "Kafka"
|
transporter: "Kafka"
|
||||||
@ -339,11 +358,13 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* NATS STREAMING TRANSPORTER
|
* NATS STREAMING TRANSPORTER
|
||||||
* Requirement: `npm i node-nats-streaming`
|
* Requirement: `npm i node-nats-streaming`
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "STAN"
|
transporter: "STAN"
|
||||||
@ -365,11 +386,13 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* TCP STREAMING TRANSPORTER
|
* TCP STREAMING TRANSPORTER
|
||||||
* No requirements
|
* No requirements
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Default options
|
// Default options
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
transporter: "TCP"
|
transporter: "TCP"
|
||||||
@ -444,6 +467,7 @@ const broker = new ServiceBroker({
|
|||||||
* http://moleculer.services/docs/cachers.html
|
* http://moleculer.services/docs/cachers.html
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Memory cacher
|
// Memory cacher
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
cacher: "Memory"
|
cacher: "Memory"
|
||||||
@ -489,10 +513,12 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* Manual caching
|
* Manual caching
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Save to cache
|
// Save to cache
|
||||||
broker.cacher.set("mykey.a", { a: 5 });
|
broker.cacher.set("mykey.a", { a: 5 });
|
||||||
|
|
||||||
@ -508,11 +534,13 @@ broker.cacher.clean("mykey.*");
|
|||||||
// Clean all entries
|
// Clean all entries
|
||||||
broker.cacher.clean();
|
broker.cacher.clean();
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* SERIALIZER
|
* SERIALIZER
|
||||||
* http://moleculer.services/docs/serializers.html
|
* http://moleculer.services/docs/serializers.html
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// JSON serializer (default)
|
// JSON serializer (default)
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
serializer: "JSON"
|
serializer: "JSON"
|
||||||
@ -533,10 +561,12 @@ const broker = new ServiceBroker({
|
|||||||
serializer: "MsgPack"
|
serializer: "MsgPack"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* STRATEGY
|
* STRATEGY
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Round-robin strategy (default)
|
// Round-robin strategy (default)
|
||||||
const broker = new ServiceBroker({
|
const broker = new ServiceBroker({
|
||||||
registry: {
|
registry: {
|
||||||
@ -569,11 +599,13 @@ const broker = new ServiceBroker({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* LOGGER
|
* LOGGER
|
||||||
* http://moleculer.services/docs/logger.html
|
* http://moleculer.services/docs/logger.html
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
// Logger methods
|
// Logger methods
|
||||||
broker.logger.fatal();
|
broker.logger.fatal();
|
||||||
broker.logger.error();
|
broker.logger.error();
|
||||||
@ -602,10 +634,12 @@ const broker = new ServiceBroker({
|
|||||||
logger: bindings => logger.child(bindings)
|
logger: bindings => logger.child(bindings)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* SERVICE SCHEMA
|
* SERVICE SCHEMA
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
// Name
|
// Name
|
||||||
name: "greeter",
|
name: "greeter",
|
||||||
@ -673,10 +707,12 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* SERVICE
|
* SERVICE
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
this.name // Name of service
|
this.name // Name of service
|
||||||
this.version // Version of service
|
this.version // Version of service
|
||||||
this.settings // Settings of service
|
this.settings // Settings of service
|
||||||
@ -687,10 +723,12 @@ this.logger // Logger instance
|
|||||||
this.actions // Actions of service.
|
this.actions // Actions of service.
|
||||||
this.waitForServices // Pointer to ‘broker.waitForServices’ method
|
this.waitForServices // Pointer to ‘broker.waitForServices’ method
|
||||||
|
|
||||||
|
|
||||||
/* *******************************************************************************************
|
/* *******************************************************************************************
|
||||||
* CONTEXT
|
* CONTEXT
|
||||||
* ******************************************************************************************* */
|
* ******************************************************************************************* */
|
||||||
|
|
||||||
|
|
||||||
ctx.id // Context ID
|
ctx.id // Context ID
|
||||||
ctx.broker // Broker instance
|
ctx.broker // Broker instance
|
||||||
ctx.action // Action definition
|
ctx.action // Action definition
|
||||||
|
Loading…
Reference in New Issue
Block a user