New release is coming soon! If you want to try out the latest features, simply run npm i -s moleculer@next. The docs for the latest version are available here.

配置

服务管理器选项

这些选项可以在ServiceBroker构造函数或moleculer.config.js文件中使用。

所有可用的服务管理器选项列表:

  • namespace: String - 节点的命名空间,用于在同一网络上细分您的节点(例如:“development”,“staging”,“production”) Default: ""
  • nodeID: String - 节点标识符。 在命名空间中必须是唯一的。 否则,服务管理器会引发致命错误并停止该进程。 Default: hostname + PID
  • logger: Boolean | String | Object | Array - 日志类。 默认情况下,将消息打印到 console更多. _Default: "Console"
  • logLevel: String | Object - 日志级别 (trace, debug, info, warn, error, fatal). 更多. Default: info
  • transporter: String | Object | Transporter - 推送系统配置项 更多. Default: null
  • requestTimeout: Number - 在拒绝一个带有RequestTimeout错误的请求之前等待的毫秒数。 Disabled: 0 Default: 0
  • retryPolicy: Object - 重试策略配置项 Read more.
  • contextParamsCloning: Boolean - 若启用则会复制上下文的 params会影响性能。 请谨慎使用! 默认: false
  • dependencyInterval: 间隔时间(单位 毫秒) 用于配置服务等待其依赖服务。 Default: 1000
  • maxCallLevel: Number - 限制调用层级。 如果超出调用层级限制,broker 会抛出一个 MaxCallLevelError 错误。 (无限制) 默认值: 0
  • heartbeatInterval: Number - 向其他节点发送心跳包的间隔秒数。 Default: 5
  • heartbeatTimeout: Number - 在设置远程节点在注册表中不可用状态之前等待的秒数。 Default: 15
  • tracking: Object - 跟踪请求并等待请求运行后关闭. (优雅停机) 更多.
  • disableBalancer: Boolean - 禁用内置的 request & emit 负载均衡。 必须保证推送器具有 request & emit 负载均衡功能 Read more. Default: false
  • registry: Object - 用于设置 Service Registry.
  • circuitBreaker: Object - 熔断器设置。
  • bulkhead: Object - 用于设置 bulkhead.
  • transit.maxQueueSize: Number - 请求过多时用于保护内存使用。 外部请求多于 stated 时, 新请求将被拒绝并抛出 QueueIsFullError错误。 Default: 50000
  • transit.maxChunkSize Number - Maximum chunk size while streaming. Default: 256KB
  • transit.disableReconnect: Boolean - Disables the reconnection logic while starting a broker. Default: false
  • transit.disableVersionCheck: Boolean - Disable protocol version checking logic in Transit. Default: false
  • transit.packetLogFilter: Array -过滤调试信息。 在调试时过滤 HEARTBEAT 数据包可能是有用的。 Default: []
  • uidGenerator: Function - 自定义 Context ID 生成器函数。
  • errorHandler: Function - 全局错误处理
  • cacher: String | Object | Cacher - 缓存器设置。 Read more. Default: null
  • serializer: String | Serializer - serializer 实例。 Read more. 默认: JSONSerializer
  • validator: Boolean | Validator - 启用默认或自定义的 parameters validation. Default: true
  • errorRegenerator: Regenerator - Instance of error regenerator. Read more. Default: null
  • metrics: Boolean | Object - 启用& 配置metrics 特性。 Default: false
  • tracing: Boolean | Object - Enable & configure tracing feature. Default: false
  • internalServices: Boolean | Object - Register internal services at start. Default: true
  • internalServices.$node - Object - Extend internal services with custom actions. Default: null
  • internalMiddlewares: Boolean - Register internal middlewares. Default: true
  • hotReload: Boolean - Watch the loaded services and hot reload if they changed. Read more. Default: false
  • middlewares: Array<Object> - Register custom middlewares. Default: null
  • replDelimiter: String - Custom REPL commands delimiter. Default: mol $
  • replCommands: Array<Object> - Register custom REPL commands. Default: null
  • metadata: Object - Store custom values. Default: null
  • skipProcessEventRegistration: Boolean - Skip the default graceful shutdown event handlers. In this case, you have to register them manually. Default: false
  • maxSafeObjectSize: Number - Maximum size of objects that can be serialized. On serialization process, check each object property size (based on length or size property value) and trim it, if object size bigger than maxSafeObjectSize value. Default: null
  • created: Function - Fired when the broker created. Default: null
  • started: Function - Fired when the broker started (all local services loaded & transporter is connected). Default: null
  • stopped: Function - Fired when the broker stopped (all local services stopped & transporter is disconnected). Default: null
  • ServiceFactory: ServiceClass - Custom Service class. If not null, broker will use it when creating services by service schema. Default: null
  • ContextFactory: ContextClass - Custom Context class. If not null, broker will use it when creating contexts for requests & events. Default: null

完整选项

{
namespace: "dev",
nodeID: "node-25",

logger: true,
logLevel: "info",
logFormatter: "default",
logObjectPrinter: null,

transporter: "nats://localhost:4222",

requestTimeout: 5000,
retryPolicy: {
enabled: true,
retries: 5,
delay: 100,
maxDelay: 1000,
factor: 2,
check: err => err && !!err.retryable
},

contextParamsCloning: false,
maxCallLevel: 100,
heartbeatInterval: 5,
heartbeatTimeout: 15,

tracking: {
enabled: true,
shutdownTimeout: 5000,
},

disableBalancer: false,

registry: {
strategy: "RoundRobin",
preferLocal: true
},

circuitBreaker: {
enabled: true,
threshold: 0.5,
windowTime: 60,
minRequestCount: 20,
halfOpenTime: 10 * 1000,
check: err => err && err.code >= 500
},

bulkhead: {
enabled: true,
concurrency: 10,
maxQueueSize: 100,
},

transit: {
maxQueueSize: 50 * 1000,
disableReconnect: false,
disableVersionCheck: false,
packetLogFilter: ["HEARTBEAT"]
},

uidGenerator: null,

errorHandler: null,

cacher: "MemoryLRU",
serializer: "JSON",

validator: true,
errorRegenerator: null,

metrics: {
enabled: true,
reporter: [
"Console"
]
},

tracing: {
enabled: true,
exporter: [
"Console"
]
},

internalServices: true,
internalMiddlewares: true,

hotReload: true,

middlewares: ["MyMiddleware"],

replDelimiter: "mol $",
replCommands: [],

metadata: {
region: "eu-west1"
},

skipProcessEventRegistration: false,
maxSafeObjectSize: null,

ServiceFactory: null,
ContextFactory: null,

created(broker) {},

started(broker) {},

stopped(broker) {}
}