Vertx eventbus模块解析

eventbus 事件總線

協議棧

TCP分包,粘包解決採用方案: 消息定长(定義消息体總长度),消息分为消息头和消息体

dataTypebytesdescription
int 4 包体总大小 code: << buffer.setInt(0, buffer.length() - 4) >>
byte 1 棧協議版本
byte 1 消息類型 codec 的路徑 io.vertx.core.eventbus.impl.codecs
自定義消息 val: -1
byte 1 發鬆模型 false Publish/subscribe , true Point/Point
int 4 send eventAddr 的長度
String ? send eventAddr 內容
int 4 replyAddress 的長度 , 無需 Response 長度爲 0
String ? replyAddress 內容, cluster模式爲 UUID , local模式採用 AtomicLong,保证全局唯一性
int 4 tcp服務 port
int 4 tcp服務 host 長度
String ? tcp服務 host 地址
int 4 headers exists 標志位, 不存在 appendInt 4 , 存在 appendInt 0
int 4 headers不爲null , headers 总大小
List<Map> ? headers 採用key/val , Map type : String/String
body ? 根據消息類型 append 內容, 不同類型具體實現 
io.vertx.core.eventbus.impl.codecs 包中 encodeToWire method

集羣模式

cluster model 目前 4種

1、Hazelcast 2、Infinispan 3、Apache Ignite 4、Apache Zookeeper

Apache Zookeeper 事件注冊流程

image

注册事件

image

一、point-to-point

send message

image

reply message

 

image

 

note: 1. point-point 核心点 replyAddress 全局唯一标识(UUID),根据标识确定链路point-point.
    2. HandlerRegistration Class的 context(上下文),同一线程处理(vertx存在多实例的情况下).
    3. HandlerRegistration  Claass的 maxBufferedMessages最大消息数(DEFAULT:1000),设置可能
       导致消息丢失,超过size直接poll丢弃,超过将丢弃消;防止消息丢失,send端需要超时机制.防止buffer堆积过大,最终导致OOM.
    4. ConnectionHolder class 中 writeMessage方法(参考2-1) //TODO optimise this (contention on monitor)
    5.如果是单向send message ,replyAddress 为 NULL.

2-1、default transports TCP send mechanism :

image

note: 1、 有丟失消息的風險(网络故障导致丢包).

二、publish-subscribe

image

posted @ 2019-03-14 11:15  cat_with_mouse  阅读(1561)  评论(0编辑  收藏  举报