本片將主要說明QuickAlarm該如何使用,以及使用時需要注意事項
1. 基本使用姿勢
首先我們不做任何的自定義操作,全部依靠系統預設的實現,我們的使用步驟如下
1. 新增註冊檔案
首先在專案的資源目錄下,新增註冊檔案 alarm.properties
,檔案內容如下
## 應用名,必填
appName=test
## 報警規則檔案所在的路徑,如果採用系統預設載入方式,必填
## / 開頭,表示存的是絕對路徑
## 非/開頭,表示存的是系統相對路徑,一般是放在資源目錄下
alarmConfPath=/tmp/alarmConfig
## 最大的報警型別,非必填
maxAlarmType=1000
## 預設報警使用者,必填
defaultAlarmUsers=yihui
複製程式碼
具體存放的位置,可以參考下圖,放在resources目錄下(原始碼中,是放在測試資源目錄下的)
2. 新增報警規則
根據註冊檔案中指定的路徑,設定報警規則檔案,如我們的報警規則檔案
內容為json串格式,支援格式化的json串解析,為了節省篇幅,下面壓縮成一行,點選獲取json格式化小工具
/tmp/alarmConig:
{"default":{"level":"LOG","autoIncEmergency":true,"max":30,"min":3,"threshold":[{"level":"SMS","threshold":20,"users":["345345345345","123123123123"]},{"level":"WEIXIN","threshold":10,"users":["yihui","erhui"]},{"level":"LOG","threshold":5,"users":["yihui","erhui"]}],"users":["yihui"]},"NPE":{"level":"WEIXIN","autoIncEmergency":false,"max":30,"min":0,"threshold":[{"level":"SMS","threshold":20,"users":["345345345345","123123123123"]},{"level":"WEIXIN","threshold":10,"users":["3h ui","4hui"]}],"users":["yihui"]},"XXX,YYY":{"level":"EMAIL","autoIncEmergency":true,"max":30,"min":3,"threshold":[{"level":"SMS","threshold":20,"users":["345345345345","123123123123"]},{"level":"WEIXIN","threshold":10,"users":["yihui","erhui"]},{"level":"EMAIL","threshold":5,"users":["yihui@xxx.com","erhui@xxx.com"]}],"users":["yihui@xxx.com"]}}
複製程式碼
3. 測試類
一個簡單的使用測試
@Test
public void sendMsg() throws InterruptedException {
String key = "NPE";
String title = "NPE異常";
String msg = "出現NPE異常了!!!";
AlarmWrapper.getInstance().sendMsg(key, title, msg); // 微信報警
// 不存在異常配置型別, 採用預設報警, 次數較小, 則直接部署出
AlarmWrapper.getInstance().sendMsg("zzz", "不存在xxx異常配置", "報警嗒嗒嗒嗒");
Thread.sleep(1000);
}
複製程式碼
II. 報警執行機器擴充套件
前面的報警規則配置中,有WEIXIN, SMS, EMAIL的報警,但是系統只提供了兩個NONE和LOG,所以我們可以看下如何自定義實現上面的三個
1. 實現IExecute介面
郵件報警
public class EmailExecute extends LogExecute {
@Override
public void sendMsg(List<String> users, String title, String msg) {
super.sendMsg(users, title, msg);
}
}
複製程式碼
簡訊報警
/**
* Created by yihui on 2018/2/7.
*/
public class SmsExecute extends LogExecute {
@Override
public void sendMsg(List<String> users, String title, String msg) {
super.sendMsg(users, title, msg);
}
}
複製程式碼
微信報警
/**
* Created by yihui on 2018/2/7.
*/
public class WeiXinExecute extends LogExecute {
@Override
public void sendMsg(List<String> users, String title, String msg) {
super.sendMsg(users, title, msg);
}
}
複製程式碼
說明,因為沒有具體的實現,所以我們直接用日誌輸出來模擬,所以就都繼承了LogExecute, 實際使用中,可以在上面補上相應的實現程式碼
2. 新增SPI定義
在 resources 目錄下,新增
- 目錄:META-INF/services/
- 檔案:com.hust.hui.alarm.core.execut.api.IExecute
檔案內容為上面幾個實現類的全路徑
com.hust.hui.alarm.core.test.execute.EmailExecute
com.hust.hui.alarm.core.test.execute.SmsExecute
com.hust.hui.alarm.core.test.execute.WeiXinExecute
複製程式碼
目錄結構如:
3. 測試
public static void main(String[] args) throws InterruptedException {
// 測試異常升級的case
// 計數 [1 - 2] 預設報警(即無日誌) (其中 < 3 的是因為未達到下限, 採用的預設報警)
// 計數 [3 - 4] 預設郵件報警(其中 < 5 採用的預設報警, 與下面的區別是報警使用者)
// 計數 [5 - 9] 郵件報警 (大於5小於10根據上升規則,還是選擇郵件報警)
// 計數 [10 - 19] 微信報警
// 計數 [20 - 30] 簡訊報警
// 計數 [31 -] 預設報警 (超過上限, 不報警)
for (int i = 0; i < 40; i++) {
new Thread(new Runnable() {
@Override
public void run() {
AlarmWrapper.getInstance().sendMsg("YYY", "異常報警升級測試");
}
}).start();
}
Thread.sleep(1000 * 600);
}
複製程式碼
實測輸出結果如下:
18:36:28.997 [Thread-12] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 26 >>> 異常報警升級測試
18:36:28.998 [Thread-24] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 16 >>> 異常報警升級測試
18:36:28.998 [Thread-33] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com, erhui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 6 >>> 異常報警升級測試
18:36:28.998 [Thread-22] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 18 >>> 異常報警升級測試
18:36:28.998 [Thread-26] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 14 >>> 異常報警升級測試
18:36:28.998 [Thread-23] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 17 >>> 異常報警升級測試
18:36:28.998 [Thread-35] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 4 >>> 異常報警升級測試
18:36:28.997 [sms-sender1-thread-4] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 10 >>> 異常報警升級測試
18:36:28.997 [sms-sender1-thread-3] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com, erhui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 5 >>> 異常報警升級測試
18:36:28.997 [Thread-18] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 27 >>> 異常報警升級測試
18:36:28.997 [Thread-11] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 28 >>> 異常報警升級測試
18:36:28.998 [Thread-21] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 19 >>> 異常報警升級測試
18:36:28.997 [sms-sender1-thread-2] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com, erhui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 9 >>> 異常報警升級測試
18:36:28.998 [Thread-14] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 24 >>> 異常報警升級測試
18:36:28.997 [Thread-10] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 29 >>> 異常報警升級測試
18:36:28.998 [Thread-15] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 22 >>> 異常報警升級測試
18:36:28.998 [Thread-16] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 23 >>> 異常報警升級測試
18:36:28.998 [sms-sender1-thread-5] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 15 >>> 異常報警升級測試
18:36:28.998 [Thread-9] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 30 >>> 異常報警升級測試
18:36:28.998 [sms-sender1-thread-1] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 11 >>> 異常報警升級測試
18:36:28.998 [Thread-13] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 25 >>> 異常報警升級測試
18:36:28.998 [Thread-19] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 21 >>> 異常報警升級測試
18:36:28.998 [Thread-34] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 3 >>> 異常報警升級測試
18:36:29.010 [sms-sender1-thread-4] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com, erhui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 7 >>> 異常報警升級測試
18:36:29.010 [sms-sender1-thread-3] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 12 >>> 異常報警升級測試
18:36:29.011 [sms-sender1-thread-2] INFO alarm - Do send msg by WEIXIN to user:[yihui, erhui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 13 >>> 異常報警升級測試
18:36:29.014 [sms-sender1-thread-5] INFO alarm - Do send msg by EMAIL to user:[yihui@xxx.com, erhui@xxx.com], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 8 >>> 異常報警升級測試
18:36:29.014 [sms-sender1-thread-1] INFO alarm - Do send msg by SMS to user:[345345345345, 123123123123], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 20 >>> 異常報警升級測試
複製程式碼
III. 報警規則載入自定義
1. 實現IConfLoader介面
自定義載入器,給了一個最基本的
public class SelfAlarmConfLoader implements IConfLoader {
@Override
public RegisterInfo getRegisterInfo() {
RegisterInfo registerInfo = new RegisterInfo();
registerInfo.setMaxAlarmType(100);
registerInfo.setDefaultAlarmUsers("yihui");
registerInfo.setAppName("test");
return registerInfo;
}
@Override
public boolean alarmEnable() {
return true;
}
@Override
public int order() {
return 0;
}
@Override
public AlarmConfig getAlarmConfig(String alarmKey) {
//db 查詢,獲取對應的配置資訊
// 下面是模擬,返回一個固定的配置
AlarmConfig alarmConfig = new AlarmConfig();
alarmConfig.setAlarmLevel("WEIXIN");
alarmConfig.setAutoIncEmergency(false);
alarmConfig.setMinLimit(10);
alarmConfig.setMaxLimit(14);
alarmConfig.setUsers(Arrays.asList("yihui"));
alarmConfig.setAlarmThreshold(Collections.emptyList());
return alarmConfig;
}
}
複製程式碼
2. 新增SPI配置
在resources目錄下新增
- 目錄: META-INF/services
- 檔案: com.hust.hui.alarm.core.loader.api.IConfLoader
檔案內容
com.hust.hui.alarm.core.test.loader.SelfAlarmConfLoader
複製程式碼
3. 測試
同樣是上面的程式碼,輸出結果
18:43:04.275 [sms-sender1-thread-2] INFO alarm - Do send msg by WEIXIN to user:[yihui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 10 >>> 異常報警升級測試
18:43:04.275 [sms-sender1-thread-4] INFO alarm - Do send msg by WEIXIN to user:[yihui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 12 >>> 異常報警升級測試
18:43:04.276 [sms-sender1-thread-1] INFO alarm - Do send msg by WEIXIN to user:[yihui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 11 >>> 異常報警升級測試
18:43:04.275 [sms-sender1-thread-5] INFO alarm - Do send msg by WEIXIN to user:[yihui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 14 >>> 異常報警升級測試
18:43:04.275 [sms-sender1-thread-3] INFO alarm - Do send msg by WEIXIN to user:[yihui], title: [test], msg: ip:172.17.13.18 >>> key:YYY >>> 異常數: 13 >>> 異常報警升級測試
複製程式碼
4. 說明
系統預設的order是10,所以如果在測試上面的第二步時,不妨把com.hust.hui.alarm.core.test.loader.SelfAlarmConfLoader#order
返回值,改成大於10,這樣就會走到預設的配置載入類
採用 SelfAlarmConfLoader
時,前面說的兩個基礎配置檔案,是可以沒有的,完全不會有任何影響,因為對應的註冊類和報警規則,都是右這個類內部提供了
IV. 小結
所有測試相關資料,均可以在測試工程中獲取,請主要關注:
- 測試case
- 註冊檔案:alarmConfig
- 報警規則配置檔案:alarm.properties
V. 其他
相關博文
- 報警系統QuickAlarm總綱
- 報警系統QuickAlarm之報警執行器的設計與實現
- 報警系統QuickAlarm之報警規則的設定與載入
- 報警系統QuickAlarm之報警規則解析
- 報警系統QuickAlarm之頻率統計及介面封裝
- 報警系統QuickAlarm使用手冊
專案: QuickAlarm
- 專案地址: Quick-Alarm
- 部落格地址: 小灰灰Blog
個人部落格: Z+|blog
基於hexo + github pages搭建的個人部落格,記錄所有學習和工作中的博文,歡迎大家前去逛逛
宣告
盡信書則不如,已上內容,純屬一家之言,因本人能力一般,見識有限,如發現bug或者有更好的建議,隨時歡迎批評指正,我的微博地址: 小灰灰Blog