一次关于长链接的测试

测试需求
AdLogServer主要负责接收、处理客户端(家长端、园丁端)广告展现、点击的日志。 AdCountSrv提供广告系统的统计功能
adlogserver和countserver之间采用长链接进行通讯,此次主要是针对这个长链接进行测试

开发文档
采用的协议:长连接通信采用序列化的对象进行传输,传输对象实现序列化Serializable接口,编码、解码采用Netty自带的编解码工具类;
可靠性设计
(1)报活
借助Netty IdleStateHandler实现Ping-Pong双向心跳机制,保证通信哪一方出现网络故障,都能及时发现。
如果发送三次ping包,均未收到pong回包,则认为报活失败,客户端将主动断开链接;
(2)重连机制,如果链路中断(包括网络异常、AdCountServer异常),等待INTERVAL(可配置,现在配置为2秒)时间后,重连;
(3)消息缓存重发:定时重传机制,在长连接正常情况下,如果AdCountServer在60秒(可配置)内没有回消息响应,消息将会重传;当发生链路中断之后,缓存在消息队列中的代发消息不能丢失,等待链路恢复之后,重新发送这些消息;
测试方案:
模拟场景,通过打印日志查看
测试用例
长链接测试
1、client和server正常通讯

[2018-03-21 16:10:13,899][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---ALL_IDLE---
[2018-03-21 16:10:13,900][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.client.Client:] send ping MessageDTO{type=1, uuid='1ac98502a711476d8d2ab442028ae13b', logBO=null} pingCount 1
[2018-03-21 16:10:13,902][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.client.Client:] get pong MessageDTO{type=2, uuid='null', logBO=null} pingCount 0

2、客户端ping三次失败 情况可能是(client和server之间网络异常,可以采用server这面网络断开)
测试步骤:
(1)将countserver的jar包放在另外一台机器上,放在一个目录下面,然后再这个目录下面运行即可
运行命令:java -jar -Dspring.profiles.active=test AdCountServer-1.0-SNAPSHOT.jar
即可运行起来
(2)在logserver 的配置文件中配置成新机器上面的IP 端口默认使用9001 因为长连接默认使用的是9001
改变配置
(3)断开另外一台机器上面的网络
adlogserver中就会出现 三次ping包的日志
如下:

[2018-03-21 19:49:56,024][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---ALL_IDLE---
[2018-03-21 19:49:56,025][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.client.Client:] send ping MessageDTO{type=1, uuid='48f478a4981742b3b2613e9e470199cb', logBO=null} pingCount 1
[2018-03-21 19:50:01,025][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---ALL_IDLE---
[2018-03-21 19:50:01,026][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.client.Client:] send ping MessageDTO{type=1, uuid='ebc186ccdce94fc6ae67559daae257df', logBO=null} pingCount 2
[2018-03-21 19:50:06,026][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---ALL_IDLE---
[2018-03-21 19:50:06,027][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.client.Client:] send ping MessageDTO{type=1, uuid='621653d864e3430d9cb88a66d14bd185', logBO=null} pingCount 3

[2018-03-21 19:50:11,029][epollEventLoopGroup-2-1][DEBUG][com.ztwl.netty.heartbeat.client.Client:] pingCount > 3 断开链接,重新链接,同时清空 ping count = 0
[2018-03-21 19:50:11,057][epollEventLoopGroup-2-1][WARN][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---172.16.208.207/172.16.208.207:9001 is inactive---
[2018-03-21 19:50:43,060][epollEventLoopGroup-2-2][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 19:51:15,063][epollEventLoopGroup-2-3][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 19:51:47,066][epollEventLoopGroup-2-4][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 19:52:19,068][epollEventLoopGroup-2-1][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 19:52:51,069][epollEventLoopGroup-2-2][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 19:53:06,102][epollEventLoopGroup-2-3][INFO][com.ztwl.netty.heartbeat.client.Client:] Connect to AdCountServer successfully!
[2018-03-21 19:53:06,102][epollEventLoopGroup-2-3][INFO][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---172.16.208.207/172.16.208.207:9001 is active---
[2018-03-21 19:53:11,102][epollEventLoopGroup-2-3][DEBUG][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---ALL_IDLE---
[2018-03-21 19:53:11,104][epollEventLoopGroup-2-3][DEBUG][com.ztwl.netty.heartbeat.client.Client:] send ping MessageDTO{type=1, uuid='fc37de0381d9453889296561ee4d817a', logBO=null} pingCount 1
[2018-03-21 19:53:11,107][epollEventLoopGroup-2-3][DEBUG][com.ztwl.netty.heartbeat.client.Client:] get pong MessageDTO{type=2, uuid='null', logBO=null} pingCount 0

3、还有一种情况countserver挂掉之后,logserver会收到一个标志,会发起重试

[2018-03-21 16:12:09,390][epollEventLoopGroup-2-1][WARN][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---172.16.200.17/172.16.200.17:9001 is inactive---
[2018-03-21 16:12:11,394][epollEventLoopGroup-2-2][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 16:12:13,396][epollEventLoopGroup-2-3][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 16:12:15,398][epollEventLoopGroup-2-4][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s

消息缓存重发测试
1、正常发送上报请求,查看

2、countserver挂掉,发送上报请求 一直存在队列中,无法发送到countserver

[2018-03-21 16:13:51,471][epollEventLoopGroup-2-4][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 16:13:51,850][epollEventLoopGroup-4-2][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] redis time :0
[2018-03-21 16:13:51,850][epollEventLoopGroup-4-2][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 1
[2018-03-21 16:13:51,851][epollEventLoopGroup-4-2][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :0
[2018-03-21 16:13:51,856][epollEventLoopGroup-4-2][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 2
[2018-03-21 16:13:51,856][epollEventLoopGroup-4-2][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :0
[2018-03-21 16:13:51,864][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] 插入UserViewTime,日志服务器打时间戳
[2018-03-21 16:13:51,868][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] update Redis
[2018-03-21 16:13:53,473][epollEventLoopGroup-2-1][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 16:14:09,486][epollEventLoopGroup-2-1][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 16:14:11,488][epollEventLoopGroup-2-2][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s
[2018-03-21 16:14:13,420][epollEventLoopGroup-4-4][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] redis time :0
[2018-03-21 16:14:13,420][epollEventLoopGroup-4-4][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 3
[2018-03-21 16:14:13,420][epollEventLoopGroup-4-4][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :0
[2018-03-21 16:14:13,428][epollEventLoopGroup-4-4][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 4
[2018-03-21 16:14:13,428][epollEventLoopGroup-4-4][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :1
[2018-03-21 16:14:13,431][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] 插入UserViewTime,日志服务器打时间戳
[2018-03-21 16:14:13,432][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] update Redis
[2018-03-21 16:14:13,490][epollEventLoopGroup-2-3][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s

3、将countserver开启来,然后查看

[2018-03-21 16:16:33,595][epollEventLoopGroup-2-2][INFO][com.ztwl.netty.heartbeat.common.BaseHeartbeatHandler:] ---172.16.200.17/172.16.200.17:9001 is active---
[2018-03-21 16:16:34,073][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 3
[2018-03-21 16:16:34,074][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  client sendData : MessageDTO{type=3, uuid='7225cffd45dd4140a16a5b1777ba1fe2', logBO={"uid":"2e779945d42d0a8f0697","ad_id":1,"space_id":1001,"ad_platform":1,"os":2,"activity_id":2,"log_type":"show","date":"2018-03-21 16:13:51"}}
[2018-03-21 16:16:34,075][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 2
[2018-03-21 16:16:34,075][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  client sendData : MessageDTO{type=3, uuid='24bbf5e6d0c84ee39fb358c3b1bca40c', logBO={"uid":"2e779945d42d0a8f0697","ad_id":1,"space_id":1001,"ad_platform":1,"os":2,"activity_id":2,"log_type":"click","date":"2018-03-21 16:13:51"}}
[2018-03-21 16:16:34,075][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 1
[2018-03-21 16:16:34,075][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  client sendData : MessageDTO{type=3, uuid='ce0e6ce322cd41f79b45825cb5cb7454', logBO={"uid":"2e779945d42d0a8f0697","ad_id":1,"space_id":1001,"ad_platform":1,"os":2,"activity_id":2,"log_type":"show","date":"2018-03-21 16:14:13"}}
[2018-03-21 16:16:34,075][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 0
[2018-03-21 16:16:34,075][KeepAlive client][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  client sendData : MessageDTO{type=3, uuid='e5610c2d6d624c07977c3c69490b2053', logBO={"uid":"2e779945d42d0a8f0697","ad_id":1,"space_id":1001,"ad_platform":1,"os":2,"activity_id":2,"log_type":"click","date":"2018-03-21 16:14:13"}}
[2018-03-21 16:16:34,110][epollEventLoopGroup-2-2][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  removeMessageDTOBuffer MessageDTO{type=4, uuid='7225cffd45dd4140a16a5b1777ba1fe2', logBO=null}
[2018-03-21 16:16:34,111][epollEventLoopGroup-2-2][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  removeMessageDTOBuffer MessageDTO{type=4, uuid='24bbf5e6d0c84ee39fb358c3b1bca40c', logBO=null}
[2018-03-21 16:16:34,112][epollEventLoopGroup-2-2][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  removeMessageDTOBuffer MessageDTO{type=4, uuid='ce0e6ce322cd41f79b45825cb5cb7454', logBO=null}
[2018-03-21 16:16:34,112][epollEventLoopGroup-2-2][DEBUG][com.ztwl.netty.heartbeat.client.Client:]  removeMessageDTOBuffer MessageDTO{type=4, uuid='e5610c2d6d624c07977c3c69490b2053', logBO=null}

4、设置缓存的最大数量 在logserver的配置文件中设置成3条,然后发送5条数据查看 logserver的日志是否会丢掉后面的那几个数据
我设置发送4个展示埋点,队列中只存3个埋点值,有一个会被丢掉

# 最大缓存队列
logQueueMax=3
[2018-03-21 20:11:08,681][epollEventLoopGroup-4-1][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] redis time :0
[2018-03-21 20:11:08,683][epollEventLoopGroup-4-1][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 1
[2018-03-21 20:11:08,683][epollEventLoopGroup-4-1][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :2
[2018-03-21 20:11:08,688][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] 插入UserViewTime,日志服务器打时间戳
[2018-03-21 20:11:08,694][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] update Redis
[2018-03-21 20:11:08,702][epollEventLoopGroup-4-1][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 2
[2018-03-21 20:11:08,702][epollEventLoopGroup-4-1][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :1
[2018-03-21 20:11:08,961][epollEventLoopGroup-4-3][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] redis time :0
[2018-03-21 20:11:08,961][epollEventLoopGroup-4-3][DEBUG][com.ztwl.netty.heartbeat.client.ClientHandler:]  LogQueue count 3
[2018-03-21 20:11:08,962][epollEventLoopGroup-4-3][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :0
[2018-03-21 20:11:08,969][epollEventLoopGroup-4-3][DEBUG][com.ztwl.netty.logserver.handler.OwnAdHandler:] write2Queue time :0
[2018-03-21 20:11:08,973][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] 插入UserViewTime,日志服务器打时间戳
[2018-03-21 20:11:08,973][redis Handler][DEBUG][com.ztwl.netty.redis.RedisHandler:] update Redis
[2018-03-21 20:11:09,485][epollEventLoopGroup-3-3][INFO][com.ztwl.netty.heartbeat.client.Client:] Failed to connect to AdCountServer, try connect after 2s

猜你喜欢

转载自blog.csdn.net/u013719297/article/details/79644023