为了与其他节点(ServiceBrokers) 进行沟通,您需要配置一个传输器。 大多数受支持的传输器都连接到一个中央消息转发器,它为远程节点之间交换消息提供了可靠的方式。 这些消息 brokers 主要支持 publish/subscribe 消息模式。
推送系统( Transporters 传输器, 以下不区分)
如果您在多个节点上运行服务,传输器将是一个重要的模块。 传输器用来与其他节点通信。 它传输事件、呼叫请求、处理响应…等。 如果一个服务的多个实例运行在不同的节点上,请求将在它们之间实现负载平衡。
传输器不负责通信逻辑。 这意味着您可以在多个传输器之间切换而不用改变任何代码。
Molecer框架内有几个内置的传输器。
TCP transporter
这是一个无依赖、零配置的 TCP 传输器。 它使用 Gossip 协议 来传播节点状态、服务列表和心跳。 它包含一个集成的 UDP发现功能,用于检测网络上的新增或断开的节点。 如果UDP在您的网络上被禁止,请使用 urls
选项。 这是远程端点列表(主机/ip、端口、节点ID)。 它可以是您配置中的静态列表或包含列表的文件路径。
使用包含默认选项的 TCP 传输器
// moleculer.config.js |
所有带有默认值的 TCP 传输器选项
// moleculer.config.js |
TCP 传输器和静态端点列表
// moleculer.config.js |
不需要设置 port
因为它会自动从 URL 列表查找并复制自身端口。
TCP transporter with shorthand static endpoint list 它需要以 tcp://
开头。
// moleculer.config.js |
TCP 传输器和静态端点列表文件
// moleculer.config.js |
// 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.
// moleculer.config.js |
DependenciesTo use this transporter install the
nats
module withnpm install nats --save
command.
示例
Connect to ‘nats://localhost:4222’
// moleculer.config.js |
Connect to a remote NATS server
// moleculer.config.js |
Connect to a remote NATS server with auth
// moleculer.config.js |
Connect with options
// moleculer.config.js |
Connect with TLS
// moleculer.config.js |
Redis Transporter
Built-in transporter for Redis.
// moleculer.config.js |
DependenciesTo use this transporter install the
ioredis
module withnpm install ioredis --save
command.
示例
Connect with default settings
// moleculer.config.js |
Connect with connection string
// moleculer.config.js |
Connect to a secure Redis server
// moleculer.config.js |
Connect with options
// moleculer.config.js |
Connect to Redis cluster
// moleculer.config.js |
MQTT Transporter
Built-in transporter for MQTT protocol (e.g.: Mosquitto).
// moleculer.config.js |
DependenciesTo use this transporter install the
mqtt
module withnpm install mqtt --save
command.
示例
Connect with default settings
// moleculer.config.js |
Connect with connection string
// moleculer.config.js |
Connect to secure MQTT server
// moleculer.config.js |
Connect with options
// moleculer.config.js |
AMQP (0.9) Transporter
Built-in transporter for AMQP 0.9 protocol (e.g.: RabbitMQ).
// moleculer.config.js |
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’
// moleculer.config.js |
Connect to a remote server
// moleculer.config.js |
Connect to a secure server
// moleculer.config.js |
Connect to a remote server with options & credentials
// moleculer.config.js |
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!
// moleculer.config.js |
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’
// moleculer.config.js |
Connect to a remote server
// moleculer.config.js |
Connect to a remote server with options & credentials
// moleculer.config.js |
Kafka Transporter
Built-in transporter for Kafka.
It is a simple implementation. It transfers Moleculer packets to consumers via pub/sub. There are not implemented offset, replay…etc features.
DependenciesTo use this transporter install the
kafka-node
module withnpm install kafka-node --save
command.
Connect to Zookeeper
// moleculer.config.js |
Connect to Zookeeper with custom options
// moleculer.config.js |
NATS Streaming (STAN) Transporter
Built-in transporter for NATS Streaming.
It is a simple implementation. It transfers Moleculer packets to consumers via pub/sub. There are not implemented offset, replay…etc features.
// moleculer.config.js |
DependenciesTo use this transporter install the
node-nats-streaming
module withnpm install node-nats-streaming --save
command.
示例
Connect with default settings
// moleculer.config.js |
Connect with connection string
// moleculer.config.js |
Connect with options
// moleculer.config.js |
Custom transporter
Custom transporter module can be created. We recommend to copy the source of NatsTransporter and implement the connect
, disconnect
, subscribe
and send
methods.
Create custom transporter
const BaseTransporter = require("moleculer").Transporters.Base; |
Use custom transporter
// moleculer.config.js |
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
.
示例
// moleculer.config.js |
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.
Note that certain data types (e.g., Date, Map, BigInt) cannot be serialized with native JSON serializer. If you are working with this kind of data consider using Avro, Notepack or any other binary serializer.
示例
// moleculer.config.js |
JSON serializer
This is the default serializer. It serializes the packets to JSON string and deserializes the received data to packet.
// moleculer.config.js |
Avro serializer
Built-in Avro serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
avsc
module withnpm install avsc --save
command.
MsgPack serializer
Built-in MsgPack serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
msgpack5
module withnpm install msgpack5 --save
command.
Notepack serializer
Built-in Notepack serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
notepack
module withnpm install notepack.io --save
command.
ProtoBuf serializer
Built-in Protocol Buffer serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
protobufjs
module withnpm install protobufjs --save
command.
Thrift serializer
Built-in Thrift serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
thrift
module withnpm install thrift --save
command.
CBOR serializer
CBOR (cbor-x) is the fastest than any other serializers.
示例
// moleculer.config.js |
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
// moleculer.config.js |