Commit 753fc22d6b3854054847492719c346361bf1d06d
1 parent
f758c964
Exists in
master
1.optimized monitor
Showing
6 changed files
with
63 additions
and
33 deletions
Show diff stats
src/main/java/com/taover/bazhuayun/analysis/web/module/heartbeat/bean/ClientInstance.java
... | ... | @@ -5,9 +5,10 @@ import com.taover.util.UtilLog; |
5 | 5 | |
6 | 6 | public class ClientInstance implements Instance{ |
7 | 7 | private AnalysisHeartbeatInstanceEntity instance; |
8 | - private ClientRequest lastestClientRequest; | |
9 | - private long lastestServerUnixtime = System.currentTimeMillis()/1000; | |
8 | + private ClientRequest latestClientRequest; | |
10 | 9 | private long currFlushServerUnixtimeBaseline = 0L; |
10 | + private long latestClientServerUnixtime = System.currentTimeMillis()/1000; | |
11 | + private long latestDetectServerUnixtime = this.latestClientServerUnixtime; | |
11 | 12 | private int lostClientRequestCount = 0; |
12 | 13 | private boolean detectNewLost = false; |
13 | 14 | |
... | ... | @@ -18,45 +19,51 @@ public class ClientInstance implements Instance{ |
18 | 19 | @Override |
19 | 20 | public void flush() { |
20 | 21 | //还未收到请求,则无需刷新失败次数 |
21 | - if(lastestClientRequest == null) { | |
22 | + if(latestClientRequest == null) { | |
22 | 23 | return; |
23 | 24 | } |
24 | 25 | |
25 | - this.detectNewLost = false; | |
26 | - this.currFlushServerUnixtimeBaseline = System.currentTimeMillis()/1000; | |
27 | - //通过服务器时间,判断是否超时未收到客户端请求 | |
28 | - if(isNewLoopForFixRateSec()) { | |
29 | - ++this.lostClientRequestCount; | |
30 | - this.detectNewLost = true; | |
31 | - | |
32 | - UtilLog.infoForMessage("检测到超时未收到请求,code["+this.getIdentity()+"],丢包统计["+this.lostClientRequestCount+"]", ServerInstance.class); | |
26 | + synchronized (this) { | |
27 | + this.detectNewLost = false; | |
28 | + this.currFlushServerUnixtimeBaseline = System.currentTimeMillis()/1000; | |
29 | + //通过服务器时间,判断是否超时未收到客户端请求 | |
30 | + if(isNewLoopForFixRateSec()) { | |
31 | + this.latestDetectServerUnixtime = this.currFlushServerUnixtimeBaseline; | |
32 | + if(isOverdueFixAndMaxWaitTime()) { | |
33 | + ++this.lostClientRequestCount; | |
34 | + this.detectNewLost = true; | |
35 | + UtilLog.infoForMessage("检测到超时未收到请求,code["+this.getIdentity()+"],丢包统计["+this.lostClientRequestCount+"]", ServerInstance.class); | |
36 | + } | |
37 | + } | |
33 | 38 | } |
34 | 39 | } |
35 | 40 | |
41 | + private boolean isOverdueFixAndMaxWaitTime() { | |
42 | + //System.out.println("isOverdueFixAndMaxWaitTime:"+(this.currFlushServerUnixtimeBaseline-this.latestClientServerUnixtime)+" > "+(this.instance.getFixRateSec()+this.instance.getMaxWaitSec())); | |
43 | + return (this.currFlushServerUnixtimeBaseline-this.latestClientServerUnixtime) > (this.instance.getFixRateSec()+this.instance.getMaxWaitSec()); | |
44 | + } | |
45 | + | |
36 | 46 | private boolean isNewLoopForFixRateSec() { |
37 | - return ((this.currFlushServerUnixtimeBaseline-this.lastestServerUnixtime) / this.instance.getFixRateSec()) > this.lostClientRequestCount; | |
47 | + //System.out.println("isNewLoopForFixRateSec:"+(this.currFlushServerUnixtimeBaseline-this.latestDetectServerUnixtime)+" > "+this.instance.getFixRateSec()); | |
48 | + return ((this.currFlushServerUnixtimeBaseline-this.latestDetectServerUnixtime) / this.instance.getFixRateSec()) > 0; | |
38 | 49 | } |
39 | 50 | |
40 | 51 | @Override |
41 | - public boolean needReform() { | |
52 | + public boolean needReform() { | |
42 | 53 | return this.detectNewLost && this.lostClientRequestCount>=1 && this.lostClientRequestCount<=this.instance.getReformMaxTime(); |
43 | 54 | } |
44 | 55 | |
45 | 56 | @Override |
46 | - public boolean needDeploy() { | |
57 | + public boolean needDeploy() { | |
47 | 58 | return this.detectNewLost && this.lostClientRequestCount == this.instance.getDeployAtLostNum(); |
48 | 59 | } |
49 | 60 | |
50 | - public long getLastestServerUnixtime() { | |
51 | - return lastestServerUnixtime; | |
52 | - } | |
53 | - | |
54 | 61 | public int getLostClientRequestCount() { |
55 | 62 | return lostClientRequestCount; |
56 | 63 | } |
57 | 64 | |
58 | 65 | public ClientRequest getLastestClientRequest() { |
59 | - return lastestClientRequest; | |
66 | + return latestClientRequest; | |
60 | 67 | } |
61 | 68 | |
62 | 69 | public AnalysisHeartbeatInstanceEntity getInstance() { |
... | ... | @@ -64,15 +71,20 @@ public class ClientInstance implements Instance{ |
64 | 71 | } |
65 | 72 | |
66 | 73 | public void dealClientRequest(ClientRequest clientRequest) { |
74 | + //System.out.println("dealClientRequest:"+clientRequest.toString()); | |
75 | + | |
67 | 76 | if(this.instance.getFixRateSec() == null) { |
68 | 77 | this.instance.setFixRateSec(Instance.DEFAULT_FIX_RATE_SEC); |
69 | 78 | } |
70 | 79 | if(this.instance.getMaxWaitSec() == null) { |
71 | 80 | this.instance.setMaxWaitSec(Instance.DEFAULT_MAX_WAIT_SEC); |
72 | 81 | } |
73 | - this.lastestClientRequest = clientRequest; | |
74 | - this.lastestServerUnixtime = System.currentTimeMillis()/1000; | |
75 | - this.lostClientRequestCount = 0; | |
82 | + | |
83 | + synchronized (this) { | |
84 | + this.latestClientRequest = clientRequest; | |
85 | + this.latestClientServerUnixtime = System.currentTimeMillis()/1000; | |
86 | + this.lostClientRequestCount = 0; | |
87 | + } | |
76 | 88 | } |
77 | 89 | |
78 | 90 | @Override |
... | ... | @@ -80,11 +92,15 @@ public class ClientInstance implements Instance{ |
80 | 92 | return this.instance.getCode(); |
81 | 93 | } |
82 | 94 | |
95 | + public long getLatestClientServerUnixtime() { | |
96 | + return this.latestClientServerUnixtime; | |
97 | + } | |
98 | + | |
83 | 99 | public long getUnixtime() { |
84 | - if(this.lastestClientRequest == null) { | |
85 | - return this.lastestServerUnixtime; | |
100 | + if(this.latestClientRequest == null) { | |
101 | + return this.latestClientServerUnixtime; | |
86 | 102 | }else { |
87 | - return this.lastestClientRequest.getUnixtime(); | |
103 | + return this.latestClientRequest.getUnixtime(); | |
88 | 104 | } |
89 | 105 | } |
90 | 106 | } | ... | ... |
src/main/java/com/taover/bazhuayun/analysis/web/module/heartbeat/bean/ClientRequest.java
... | ... | @@ -60,4 +60,9 @@ public class ClientRequest { |
60 | 60 | public String getIdentity() { |
61 | 61 | return this.code; |
62 | 62 | } |
63 | + | |
64 | + @Override | |
65 | + public String toString() { | |
66 | + return "ClientRequest [code=" + code + ", ip=" + ip + ", unixtime=" + unixtime + ", seckey=" + seckey + "]"; | |
67 | + } | |
63 | 68 | } | ... | ... |
src/main/java/com/taover/bazhuayun/analysis/web/module/heartbeat/bean/ServerInstance.java
... | ... | @@ -39,8 +39,9 @@ public class ServerInstance implements Instance { |
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | - this.latestRequestUnixtime = this.serverUnixtimeBaseline; | |
42 | + //发送数据包,并更新结构数据 | |
43 | 43 | try { |
44 | + this.latestRequestUnixtime = this.serverUnixtimeBaseline; | |
44 | 45 | this.isSendNewHeartbeat = true; |
45 | 46 | this.latestUrlWithParam = this.instance.getUrl() + this.getUrlParams(); |
46 | 47 | UtilLog.infoForMessage("向code["+this.getIdentity()+"],url["+this.latestUrlWithParam+"]发送心跳请求", ServerInstance.class); |
... | ... | @@ -52,7 +53,7 @@ public class ServerInstance implements Instance { |
52 | 53 | } catch(SocketTimeoutException timeError) { |
53 | 54 | this.latestServerResponse = ServerResponse.createOverdue(); |
54 | 55 | } catch (Exception e) { |
55 | - this.latestServerResponse = ServerResponse.createException(e); | |
56 | + this.latestServerResponse = ServerResponse.createException(e); | |
56 | 57 | } |
57 | 58 | |
58 | 59 | //判断响应是否正常 |
... | ... | @@ -69,7 +70,8 @@ public class ServerInstance implements Instance { |
69 | 70 | } |
70 | 71 | |
71 | 72 | private boolean isNewLoopForFixRateSec() { |
72 | - return ((this.serverUnixtimeBaseline-this.latestRequestUnixtime)/this.instance.getFixRateSec()) > this.errorServerResponseCount; | |
73 | + System.out.println("isNewLoopForFixRateSec:"+(this.serverUnixtimeBaseline-this.latestRequestUnixtime)+" > "+this.instance.getFixRateSec()); | |
74 | + return (this.serverUnixtimeBaseline-this.latestRequestUnixtime) > this.instance.getFixRateSec(); | |
73 | 75 | } |
74 | 76 | |
75 | 77 | public AnalysisHeartbeatInstanceEntity getInstance() { | ... | ... |
src/main/java/com/taover/bazhuayun/analysis/web/module/heartbeat/service/HeartbeatReformServiceImpl.java
... | ... | @@ -60,11 +60,11 @@ public class HeartbeatReformServiceImpl implements HeartbeatReformService { |
60 | 60 | htmlContent += " 未收到服务器响应\n"; |
61 | 61 | }else { |
62 | 62 | if(instance.getLatestServerResponse().isOverdue()) { |
63 | - htmlContent += " 向接口["+entity.getUrl()+"]发送请求,响应超时["+entity.getMaxWaitSec()+"s],请及时确认服务是否正常运行\n"; | |
63 | + htmlContent += " CODE["+instance.getIdentity()+"]监控异常,向接口["+entity.getUrl()+"]发送请求,响应超时["+entity.getMaxWaitSec()+"s],请及时确认服务是否正常运行\n"; | |
64 | 64 | }else if(!instance.getLatestServerResponse().isCodeOk()) { |
65 | - htmlContent += " 向接口["+entity.getUrl()+"]发送请求,响应数据不正常(详细请查看邮件),请及时确认服务是否正常运行\n"; | |
65 | + htmlContent += " CODE["+instance.getIdentity()+"]监控异常,向接口["+entity.getUrl()+"]发送请求,响应数据不正常(详细请查看邮件),请及时确认服务是否正常运行\n"; | |
66 | 66 | }else { |
67 | - htmlContent += " 向接口["+entity.getUrl()+"]发送请求,未知异常,请及时确认服务是否正常运行\n"; | |
67 | + htmlContent += " CODE["+instance.getIdentity()+"]监控异常, 向接口["+entity.getUrl()+"]发送请求,未知异常,请及时确认服务是否正常运行\n"; | |
68 | 68 | } |
69 | 69 | } |
70 | 70 | htmlContent += " 最后一次请求时间: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(instance.getLatestRequestUnixtime()*1000)); |
... | ... | @@ -148,7 +148,7 @@ public class HeartbeatReformServiceImpl implements HeartbeatReformService { |
148 | 148 | htmlContent += "<li>请求频率: "+entity.getFixRateSec()+"秒/次</li>"; |
149 | 149 | htmlContent += "<li>请求最大等待时间: "+entity.getMaxWaitSec()+"秒</li>"; |
150 | 150 | htmlContent += "<li>客户端请求时间戳: "+sdf.format(new Date(instance.getUnixtime()*1000))+"</li>"; |
151 | - htmlContent += "<li>服务器接收时间: "+sdf.format(new Date(instance.getLastestServerUnixtime()*1000))+"</li>"; | |
151 | + htmlContent += "<li>服务器接收时间: "+sdf.format(new Date(instance.getLatestClientServerUnixtime()*1000))+"</li>"; | |
152 | 152 | htmlContent += "<li>最后一次收到的请求包: <span style=\"color:gray;\">"+JSON.toJSONString(instance.getLastestClientRequest())+"</span></li>"; |
153 | 153 | htmlContent += "<li>累计次数("+entity.getReformMaxTime()+"次后不再报警):"+instance.getLostClientRequestCount()+"</li>"; |
154 | 154 | htmlContent += "</ul>"; | ... | ... |
test/main/java/com/taover/bazhuayun/analysis/test/SemilarClient.java
... | ... | @@ -14,7 +14,7 @@ public class SemilarClient { |
14 | 14 | String token = ""; |
15 | 15 | try { |
16 | 16 | System.out.println("=======send heartbeat["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"]======="); |
17 | - System.out.println("response:"+UtilHttpByOkHttp.sendGet("http://localhost/api/heartbeat?code="+code+"&unixtime="+time+"&seckey="+UtilEncrypt.MD5Lower32(code+time+token), null)); | |
17 | + System.out.println("response:"+UtilHttpByOkHttp.sendGet("http://localhost/api/heartbeat/demon?code="+code+"&unixtime="+time+"&seckey="+UtilEncrypt.MD5Lower32(code+time+token), null)); | |
18 | 18 | |
19 | 19 | Thread.sleep(1000*60); |
20 | 20 | } catch (Exception e) { | ... | ... |
test/main/java/com/taover/bazhuayun/analysis/test/WbTest.java
0 → 100644