To communicate other nodes (ServiceBrokers), you need to configure a transporter. The most transporters connect to a central message broker server which is liable for message transferring among nodes. These message brokers mainly support publish/subscribe messaging pattern.
Transporters
Transporter is an important module if you are running services on multiple nodes. Transporter communicates with other nodes. It transfers events, calls requests and processes responses …etc. If a service runs on multiple instances on different nodes, the requests will be load-balanced among live nodes.
The whole communication logic is outside of transporter class. It means switching between transporters without changing any lines of our code is easy.
There are several built-in transporters in Moleculer framework.
TCP transporter
This is a no-dependency, zero-configuration TCP transporter. It uses Gossip protocol to disseminate node statuses, service list and heartbeats. It contains an integrated UDP discovery feature to detect new and disconnected nodes on the network.
If the UDP is prohibited on your network, use urls
option. It is a list of remote endpoints (host/ip, port, nodeID). It can be a static list in your configuration or a file path which contains the list.
Please note, it is an experimental transporter. Do not use it in production yet!
Use TCP transporter with default options
const broker = new ServiceBroker({ |
All TCP transporter options with default values
const broker = new ServiceBroker({ |
TCP transporter with static endpoint list
const broker = new ServiceBroker({ |
You don’t need to set port
because it find & parse the self TCP port from URL list.
TCP transporter with shorthand static endpoint list
It needs to start with tcp://
.
const broker = new ServiceBroker({ |
TCP transporter with static endpoint list file
const broker = new ServiceBroker({ |
// nodes.json |
Serviceless nodePlease note, you don’t need to list all remote nodes. It’s enough at least one node which is online. For example, create a “serviceless” gossiper node, which does nothing, just shares other remote nodes addresses by gossip messages. So all nodes must know only the gossiper node address to be able to communicate with all other nodes.
NATS Transporter
Built-in transporter for NATS.
NATS Server is a simple, high performance open source messaging system for cloud-native applications, IoT messaging, and microservices architectures.
let { ServiceBroker } = require("moleculer"); |
DependenciesTo use this transporter install the
nats
module withnpm install nats --save
command.
Examples
// Connect to 'nats://localhost:4222' |
Redis Transporter
Built-in transporter for Redis.
let { ServiceBroker } = require("moleculer"); |
DependenciesTo use this transporter install the
ioredis
module withnpm install ioredis --save
command.
Examples
// Connect with default settings |
MQTT Transporter
Built-in transporter for MQTT protocol (e.g.: Mosquitto).
let { ServiceBroker } = require("moleculer"); |
DependenciesTo use this transporter install the
mqtt
module withnpm install mqtt --save
command.
Examples
// Connect with default settings |
AMQP 0.9 Transporter
Built-in transporter for AMQP protocol (e.g.: RabbitMQ).
let { ServiceBroker } = require("moleculer"); |
DependenciesTo use this transporter install the
amqplib
module withnpm install amqplib --save
command.
Transporter options
Options can be passed to amqp.connect()
method.
// Connect to 'amqp://guest:guest@localhost:5672' |
AMQP 1.0 Transporter
Built-in transporter for AMQP 1.0 protocol (e.g.: ActiveMq or RabbitMQ + rabbitmq-amqp1.0 plugin).
Please note, it is an experimental transporter. Do not use it in production yet!
let { ServiceBroker } = require("moleculer"); |
DependenciesTo use this transporter install the
rhea-promise
module withnpm install rhea-promise --save
command.
Transporter options
Options can be passed to rhea.connection.open()
method, the topics, the queues, and the messages themselves.
// Connect to 'amqp10://guest:guest@localhost:5672' |
Kafka Transporter
Built-in transporter for Kafka. It is a very simple implementation. It transfers Moleculer packets to consumers via pub/sub. There are not implemented offset, replay…etc features.
Please note, it is an experimental transporter. Do not use it in production yet!
DependenciesTo use this transporter install the
kafka-node
module withnpm install kafka-node --save
command.
Connect to Zookeeper
const broker = new ServiceBroker({ |
Connect to Zookeeper with custom options
const broker = new ServiceBroker({ |
NATS Streaming (STAN) Transporter
Built-in transporter for NATS Streaming. It is a very simple implementation. It transfers Moleculer packets to consumers via pub/sub. There are not implemented offset, replay…etc features.
Please note, it is an experimental transporter. Do not use it in production yet!
let { ServiceBroker } = require("moleculer"); |
DependenciesTo use this transporter install the
node-nats-streaming
module withnpm install node-nats-streaming --save
command.
Examples
// Connect with default settings |
Custom transporter
Custom transporter module can be created. We recommend to copy the source of NatsTransporter and implement the connect
, disconnect
, subscribe
and publish
methods.
Create custom transporter
const BaseTransporter = require("moleculer").Transporters.Base; |
Use custom transporter
const { ServiceBroker } = require("moleculer"); |
Disabled balancer
Some transporter servers have built-in balancer solution. E.g.: RabbitMQ, NATS, NATS-Streaming. If you want to use the transporter balancer instead of Moleculer balancer, set the disableBalancer
broker option to true
.
Example
const broker = new ServiceBroker({ |
Please noteIf you disable the built-in Moleculer balancer, all requests & events will be transferred via transporter (including local requests). E.g. you have a local math service and you call
math.add
locally, the request will be sent via transporter.
Serialization
Transporter needs a serializer module which serializes & deserializes the transferred packets. The default serializer is the JSONSerializer
but there are several built-in serializer.
Example
const { ServiceBroker } = require("moleculer"); |
JSON serializer
This is the built-in default serializer. It serializes the packets to JSON string and deserializes the received data to packet.
const broker = new ServiceBroker({ |
Avro serializer
Built-in Avro serializer.
const broker = new ServiceBroker({ |
DependenciesTo use this serializer install the
avsc
module withnpm install avsc --save
command.
MsgPack serializer
Built-in MsgPack serializer.
const broker = new ServiceBroker({ |
DependenciesTo use this serializer install the
msgpack5
module withnpm install msgpack5 --save
command.
Notepack serializer
Built-in Notepack serializer.
const broker = new ServiceBroker({ |
DependenciesTo use this serializer install the
notepack
module withnpm install notepack.io --save
command.
ProtoBuf serializer
Built-in Protocol Buffer serializer.
const broker = new ServiceBroker({ |
DependenciesTo use this serializer install the
protobufjs
module withnpm install protobufjs --save
command.
Thrift serializer
Built-in Thrift serializer.
const broker = new ServiceBroker({ |
DependenciesTo use this serializer install the
thrift
module withnpm install thrift --save
command.
Custom serializer
Custom serializer module can be created. We recommend to copy the source of JSONSerializer and implement the serialize
and deserialize
methods.
Create custom serializer
const BaseSerializer = require("moleculer").Serializers.Base; |
Use custom serializer
const { ServiceBroker } = require("moleculer"); |