1 Star 0 Fork 161

瓜兮兮 / quick-alarm

forked from 一灰灰Blog / quick-alarm 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MulanPSL-1.0

一个可扩展的报警系统Quick-Alarm

I. 背景

日常的系统中,报警是不可缺少的一环,目前报警方式很多,最常见的有直接打日志,微信报警,短信报警,邮件报警等;而涉及到报警,一般不可避免的需要提前设置一些基本信息,如报警方式,报警频率,报警用户,开关等;

另外一个常见的问题是一般采用的是单一的报警方式,比如不管什么类型的报警全部都用短信方式触达,然后就会发现手机时常处于被淹没的状态了,久而久之对报警短信就不会敏感了

II. 目标

因此我们准备设计一个通用的报警框架

  • 可以自由选择报警方式,
  • 支持用户自定义报警方式拓展
  • 支持动态的报警配置,
  • 支持用户自定义报警规则拓展
  • 支持报警方式自动切换规则设定
  • 支持报警方式自定义自动切换规则拓展

通过做一个东西,当然是希望可以带来一些用处,或者能学习到什么东西,才不枉花费精力来折腾一下,那么我们这个报警系统,究竟有什么用,或者可以从中学习到什么东西呢?

用途:

  • 支持灵活可配的报警规则,以及具体报警业务的自定义拓展
  • 目标就是统一报警的使用姿势,也就是不管什么报警,都一个姿势,但是内部可以玩出各种花样,对使用者而言就方便简洁了

学习:

抛开特有的知识点,可以抽象一些公共可用的地方,大概就下面这两点了

  • 我们可以如何支持功能的动态可拓展
  • 线程池的使用

III. 设计

整体来说,报警主要可以划分为三个步骤,如下:

IMAGE

  • 提交报警:对外部使用者提供的接口
  • 选择报警:根据报警相关信息,选择具体的报警执行单元
  • 执行报警:实现具体的报警逻辑

从任务划分上来看,比较清晰简单,但是每一块的内容又必须可以拓展,

  • 选择报警:

    • 报警规则的制定
    • 报警规则加载器 ConfLoader
    • 报警规则变更的触发器 ConfChangeTrigger
    • 报警规则解析器
      • ConfParse : 解析文本格式报警规则为业务对象
      • AlarmSelector :根据报警规则和报警类型,选择具体报警执行器 AlarmExecute
  • 执行报警:

    • 线程池执行(以防止影响主业务流程)
    • AlarmExecute的动态拓展(支持用户自定义的报警器实现)
    • 实际的报警逻辑

根据上面的拆解,在应用启动的时候,就有一些事情必须去做了

  1. ConfLoader的选择
  2. 报警规则加载
  3. AlarmExecute的加载(包括默认的+自定义实现的)

下图显示在应用启动时,报警规则解析的相关步骤

应用启动.png

至于报警执行器的加载就比较简单了,如下图

IMAGE

因此,整个的工作流程如下图

alarm-arch.jpg

IV. 任务拆解

通过前面的任务设计之后,对需要做的东西有了一个大概的脉络了,因此在正式操刀实现之前,下对整个架构进行任务拆解,看下可以具体的执行步骤可以怎么来

  • 最直接的就是设计报警执行器AlarmExecute
    • 定义基本接口
    • 制定自定义扩展规则
  • 接下来就是设计报警规则
    • 如何加载报警规则?
    • 报警规则具体的定义细则
    • 报警规则的解析:即根据报警类型来获取报警执行器
    • 报警规则动态更新支持
  • 报警线程池
    • 维护报警队列
    • 报警的计数与频率控制
  • 封装对外使用接口

所以,通过上面的分析可以看出,这个系统的结构还是蛮简单的,整个只需要四个部分就可以搞定,其中最主要的就是前面两个了,后面将分别说明

V. 整体说明

0. 简单使用case

a. 引入依赖

基于maven项目,如下配置

先添加仓库

<repositories>
    <repository>
        <id>yihui-maven-repo</id>
        <url>https://raw.githubusercontent.com/liuyueyi/maven-repository/master/repository</url>
    </repository>
</repositories>

引入依赖

<dependency>
    <groupId>com.hust.hui.alarm</groupId>
    <artifactId>core</artifactId>
    <version>0.1</version>
</dependency>

也可以直接在将代码拷贝下来使用

b. 添加基本配置文件

如果使用系统默认的配置注册方式,则在项目的资源目录下新增文件

alarm.properties

## 应用名,必填
appName=test

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

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

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

请注意其中的 alramConfPath 参数,这个指定报警规则文件的路径, 然后根据这个路径,添加报警规则文件,一个case如下:

(说明,外层的key为报警类型,default为默认的兜底规则,支持多个报警规则共享一个配置项,只需要用英文逗号分割即可,如下面的NPE, SELFDEFINE两种报警类型)

{
    "default": {
        "level": "NONE",
        "autoIncEmergency": true,
        "max": 30,
        "min": 3,
        "threshold": [
            {
                "level": "LOG",
                "threshold": 5,
                "users": [
                    "yihui",
                    "erhui"
                ]
            }
        ],
        "users": [
            "yihui"
        ]
    },
    "NPE,SELFDEFINE": {
        "level": "LOG",
        "autoIncEmergency": false,
        "max": 30,
        "min": 0,
        "threshold": [
            {
                "level": "SMS",
                "threshold": 20,
                "users": [
                    "345345345345",
                    "123123123123"
                ]
            },
            {
                "level": "WEIXIN",
                "threshold": 10,
                "users": [
                    "小灰灰Blog",
                    "greyBlog"
                ]
            }
        ],
        "users": [
            "yihui"
        ]
    }
}

c. 报警调用

一个测试case如下

@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);
}

d. 更多

对于如何扩展自定义报警执行器,如何使用自定义的配置文件加载类替换系统的,可以参考更详细文档: 报警系统QuickAlarm使用手册

如有任何问题,随时欢迎联系,通过issuse,email,微博都可以

这是个人信息主页: 小灰灰Blog

1. 相关文档

所有文档可以在个人博客查看汇总:

  1. QuickAlarm博客汇总

v0.1版相关文档

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

v0.2版改进文档

  1. 报警系统QuickAlarm之默认报警规则扩展

2. 历程

  • 2018.02.07
    • 整体框架搭建完成
    • 基本功能实现
  • 2018.02.12 : v0.1 正式包
    • 项目开发技术文档完成
    • 开发使用手册完成
  • 2018.03.05
  • 2018.03.28
    • 新增模板方式输出报警内容接口
    • 新增指定报警用户的接口
    • 支持多ConfLoader加载器同时生效,根据IConfLoader优先级排序,通过AlarmKey优先选择精确匹配的报警规则,都没有时,选择最高优先级的IConfLoader的兜底报警规则
  • 2018.04.08
  • 2018.04.10
    • 完成邮件报警方式封装
    • 使用姿势参考: 使用说明

3. todoList

  • 剥离报警规则和解析器,支持用户自定义
  • 包装邮件报警方式
  • 包装微信报警方式
  • 支持多重报警类型同时选择

VI. 其他

个人博客: Z+|blog

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

声明

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

扫描关注

公众号&博客

QrCode

打赏码

pay

木兰宽松许可证, 第1版 木兰宽松许可证, 第1版 2019年8月 http://license.coscl.org.cn/MulanPSL 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第1版(“本许可证”)的如下条款的约束: 0. 定义 “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 “法人实体”是指提交贡献的机构及其“关联实体”。 “关联实体”是指,对“本许可证”下的一方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 1. 授予版权许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 2. 授予专利许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括仅因您或他人修改“贡献”或其他结合而将必然会侵犯到的专利权利要求。如您或您的“关联实体”直接或间接地(包括通过代理、专利被许可人或受让人),就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 3. 无商标许可 “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 4. 分发限制 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 5. 免责声明与责任限制 “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 条款结束。 如何将木兰宽松许可证,第1版,应用到您的软件 如果您希望将木兰宽松许可证,第1版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; 3, 请将如下声明文本放入每个源文件的头部注释中。 Copyright (c) [2019] [name of copyright holder] [Software Name] is licensed under the Mulan PSL v1. You can use this software according to the terms and conditions of the Mulan PSL v1. You may obtain a copy of Mulan PSL v1 at: http://license.coscl.org.cn/MulanPSL THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v1 for more details. Mulan Permissive Software License,Version 1 Mulan Permissive Software License,Version 1 (Mulan PSL v1) August 2019 http://license.coscl.org.cn/MulanPSL Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v1 (this License) with following terms and conditions: 0. Definition Software means the program and related documents which are comprised of those Contribution and licensed under this License. Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. Legal Entity means the entity making a Contribution and all its Affiliates. Affiliates means entities that control, or are controlled by, or are under common control with a party to this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. Contribution means the copyrightable work licensed by a particular Contributor under this License. 1. Grant of Copyright License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. 2. Grant of Patent License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed, excluding of any patent claims solely be infringed by your or others’ modification or other combinations. If you or your Affiliates directly or indirectly (including through an agent, patent licensee or assignee), institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. 3. No Trademark License No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in section 4. 4. Distribution Restriction You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. 5. Disclaimer of Warranty and Limitation of Liability The Software and Contribution in it are provided without warranties of any kind, either express or implied. In no event shall any Contributor or copyright holder be liable to you for any damages, including, but not limited to any direct, or indirect, special or consequential damages arising from your use or inability to use the Software or the Contribution in it, no matter how it’s caused or based on which legal theory, even if advised of the possibility of such damages. End of the Terms and Conditions How to apply the Mulan Permissive Software License,Version 1 (Mulan PSL v1) to your software To apply the Mulan PSL v1 to your work, for easy identification by recipients, you are suggested to complete following three steps: i. Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; ii. Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; iii. Attach the statement to the appropriate annotated syntax at the beginning of each source file. Copyright (c) [2019] [name of copyright holder] [Software Name] is licensed under the Mulan PSL v1. You can use this software according to the terms and conditions of the Mulan PSL v1. You may obtain a copy of Mulan PSL v1 at: http://license.coscl.org.cn/MulanPSL THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v1 for more details.

简介

通用报警框架,支持报警方式自定义,报警配置自定义 展开 收起
Java
MulanPSL-1.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/jlon/quick-alarm.git
git@gitee.com:jlon/quick-alarm.git
jlon
quick-alarm
quick-alarm
master

搜索帮助