Vert.x 3: Node和浏览器端事件总线重连
Vert.x 3: SockJS 服务代理
Vert.x 3: 服务代理
Vert.x 3: Node和浏览器端事件总线重连
默认初始化的EventBus对象没有开启重连, 如果网络断开后, 再次调用服务代理会导致错误. 因此这里我们需要在 onopen 和 onreconnect 两个事件中处理一下服务注册的问题.
var EventBus = require("vertx3-eventbus-client"); var address = "http://localhost:8080/eventbus" var options = { vertxbus_reconnect_attempts_max: Infinity, // Max reconnect attempts vertxbus_reconnect_delay_min: 1000, // Initial delay (in ms) before first reconnect attempt vertxbus_reconnect_delay_max: 5000, // Max delay (in ms) between reconnect attempts vertxbus_reconnect_exponent: 2, // Exponential backoff factor vertxbus_randomization_factor: 0.5 // Randomization factor between 0 and 1 }; var eb = new EventBus(`${address}`, options); eb.enableReconnect(true) # 服务注册 var QrcodeServiceProxy = require("../lib/qrcode_service-proxy"); eb.onopen = function () { logger.info("Opening event bus...") # 注册到global对象, 让其在所有模块中可用. global.vertx_services = { qrcodeService: new QrcodeServiceProxy(eb, "com.totorotec.servicefactory.qrcode-service") } } eb.onerror = function() { logger.error("Eventbus: error ocurred.") } eb.onclose = function() { logger.error("Eventbus: closed.") } eb.onreconnect = function() { logger.info("Eventbus: reconnecting...") global.vertx_services = { qrcodeService: new QrcodeServiceProxy(eb, "com.totorotec.servicefactory.qrcode-service") } }
杀掉服务端进程, 观看Node端的输出:
[2017-11-16 16:00:01.821] [ERROR] default - Eventbus: closed. [2017-11-16 16:00:06.837] [ERROR] default - Eventbus: closed. ... ... ...
重启服务端 yarn run_qr8
后观察Node的输出:
... ... ... [2017-11-16 16:00:46.912] [ERROR] default - Eventbus: closed. [2017-11-16 16:00:52.062] [INFO] default - Opening event bus... [2017-11-16 16:00:52.062] [INFO] default - Eventbus: reconnecting...
我们看到连接恢复了.
代码库
二维码项目的所有源代码, 包括服务器和客户端在下面的仓库中:
相关推荐
boneix 2020-10-21
seanzed 2020-10-15
ifconfig 2020-10-14
学留痕 2020-09-20
往后余生 2020-09-17
kka 2020-09-14
redis 2020-09-07
lzccheng 2020-09-06
soyo 2020-08-31
stonerkuang 2020-08-18
LxyPython 2020-08-17
raksmart0 2020-08-17
Lzs 2020-08-14
MrHaoNan 2020-07-31
80530895 2020-07-05
lengyu0 2020-06-28
YarnSup 2020-06-28
huanglianhuabj00 2020-06-27