6. 报警系统QuickAlarm使用手册

文章目录
  1. 1. 基本使用姿势
    1. 1. 添加注册文件
    2. 2. 添加报警规则
    3. 3. 测试类
  2. II. 报警执行机器扩展
    1. 1. 实现IExecute接口
    2. 2. 添加SPI定义
    3. 3. 测试
  3. III. 报警规则加载自定义
    1. 1. 实现IConfLoader接口
    2. 2. 添加SPI配置
    3. 3. 测试
    4. 4. 说明
  4. IV. 小结
  5. V. 其他
    1. 相关博文
    2. 项目: QuickAlarm
    3. 个人博客: Z+|blog
    4. 声明
    5. 扫描关注

本文将主要说明QuickAlarm该如何使用,以及使用时需要注意事项

1. 基本使用姿势

首先我们不做任何的自定义操作,全部依靠系统默认的实现,我们的使用步骤如下

1. 添加注册文件

首先在项目的资源目录下,添加注册文件 alarm.properties,文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
## 应用名,必填
appName=test

## 报警规则文件所在的路径,如果采用系统默认加载方式,必填
## / 开头,表示存的是绝对路径
## 非/开头,表示存的是系统相对路径,一般是放在资源目录下
alarmConfPath=/tmp/alarmConfig

## 最大的报警类型,非必填
maxAlarmType=1000

## 默认报警用户,必填
defaultAlarmUsers=yihui

具体存放的位置,可以参考下图,放在resources目录下(源码中,是放在测试资源目录下的)

IMAGE

2. 添加报警规则

根据注册文件中指定的路径,设置报警规则文件,如我们的报警规则文件

内容为json串格式,支持格式化的json串解析,为了节省篇幅,下面压缩成一行,点击获取json格式化小工具

/tmp/alarmConig:

1
{"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. 测试类

一个简单的使用测试

1
2
3
4
5
6
7
8
9
10
11
12
@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接口

邮件报警

1
2
3
4
5
6
public class EmailExecute extends LogExecute {
@Override
public void sendMsg(List<String> users, String title, String msg) {
super.sendMsg(users, title, msg);
}
}

短信报警

1
2
3
4
5
6
7
8
9
10
/**
* 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);
}
}

微信报警

1
2
3
4
5
6
7
8
9
/**
* 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

文件内容为上面几个实现类的全路径

1
2
3
com.hust.hui.alarm.core.test.execute.EmailExecute
com.hust.hui.alarm.core.test.execute.SmsExecute
com.hust.hui.alarm.core.test.execute.WeiXinExecute

目录结构如:

IMAGE

3. 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
}

实测输出结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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接口

自定义加载器,给了一个最基本的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

文件内容

1
com.hust.hui.alarm.core.test.loader.SelfAlarmConfLoader

3. 测试

同样是上面的代码,输出结果

1
2
3
4
5
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. 小结

所有测试相关数据,均可以在测试工程中获取,请主要关注:

V. 其他

相关博文

  1. 报警系统QuickAlarm总纲
  2. 报警系统QuickAlarm之报警执行器的设计与实现
  3. 报警系统QuickAlarm之报警规则的设定与加载
  4. 报警系统QuickAlarm之报警规则解析
  5. 报警系统QuickAlarm之频率统计及接口封装
  6. 报警系统QuickAlarm使用手册
  7. 报警系统QuickAlarm之默认报警规则扩展

项目: QuickAlarm

个人博客: Z+|blog

基于hexo + github pages搭建的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

声明

尽信书则不如,已上内容,纯属一家之言,因本人能力一般,见识有限,如发现bug或者有更好的建议,随时欢迎批评指正,我的微博地址: 小灰灰Blog

扫描关注

QrCode

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×