一次關於長鏈接的測試

測試需求
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
發佈了40 篇原創文章 · 獲贊 0 · 訪問量 9944
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章