事件驱动

在jetlinks中大量使用到事件驱动,使用事件总线(EventBus)基于topic来进行数据传递.

Topic

采用树结构来定义topic如:/device/id/message/type . topic支持路径通配符,如:/device/** 或者/device/*/message/*.

TIP

通配符**表示匹配多层路径,*表示匹配单层路径. 不支持前后匹配,如: /device/id-*/message. 发布和订阅均支持通配符,发布时使用通配符时则进行广播.

使用

订阅消息:

  1. @Subscribe("/user/*/saved")
  2. public Mono<Void> handleDeviceMessage(UserEntity entity){
  3. return publishDeviecMessageToKafka(message);
  4. }

发布消息:

  1. @Autowired
  2. private EventBus eventBus;
  3. public Mono<Void> saveUser(UserEntity entity){
  4. return service.saveUser(entity)
  5. .then(eventBus.publish("/user/"+entity.getId()+"/saved",entity))
  6. .then();
  7. }

topic列表

设备消息

所有设备消息的topic的前缀均为: /device/{productId}/{deviceId}. 如:设备device-1上线消息: /device/product-1/device-1/online. 可通过通配符订阅所有设备的指定消息,如:/device/*/*/online, 或者订阅所有消息:/device/**.

TIP

使用通配符订阅可能将收到大量的消息,请保证消息的处理速度,否则会影响系统消息吞吐量.

topic类型说明
/onlineDeviceOnlineMessage设备上线
/offlineDeviceOfflineMessage设备离线
/message/event/{eventId}DeviceEventMessage设备事件
/message/property/reportReportPropertyMessage设备上报属性
/message/property/read/replyReadPropertyMessageReply读取属性回复
/message/property/write/replyWritePropertyMessageReply修改属性回复
/message/function/replyFunctionInvokeMessageReply调用功能回复
/registerDeviceRegisterMessage设备注册,通常与子设备消息配合使用
/unregisterDeviceUnRegisterMessage设备注销,同上
/message/children/{childrenDeviceId}/{topic}ChildDeviceMessage子设备消息,{topic}为子设备消息对应的topic
/message/children/reply/{childrenDeviceId}/{topic}ChildDeviceMessage子设备回复消息,同上

WARNING

列表中的topic已省略前缀/device/{productId}/{deviceId},使用时请加上.

设备告警

在配置了设备告警规则,设备发生告警时,会发送消息到消息总线.

  1. `/rule-engine/device/alarm/{productId}/{deviceId}/{ruleId}`
  2. {
  3. "productId":"",
  4. "alarmId":"",
  5. "alarmName":"",
  6. "deviceId":"",
  7. "deviceName":"",
  8. "...":"其他从规则中获取到到信息"
  9. }

系统日志

topic格式: /logging/system/{logger名称,.替换为/}/{level}.

  1. `/logging/system/org/jetlinks/pro/TestService/{level}`
  2. {
  3. "name":"org.jetlinks.pro.TestService", //logger名称
  4. "threadName":"线程名称",
  5. "level":"日志级别",
  6. "className":"产生日志的类名",
  7. "methodName":"产生日志的方法名",
  8. "lineNumber"32,//行号
  9. "message":"日志内容",
  10. "exceptionStack":"异常栈信息",
  11. "createTime":"日志时间",
  12. "context":{} //上下文信息
  13. }