基于Drools的CEP规则引擎实现

来源:互联网 发布:tv版直播软件 编辑:程序博客网 时间:2024/05/21 09:32

1,整体数据流程图
Agent负责数据的采集,通过远程过程调用,经Loastach到Kafka,Collector负责原始数据的整理,生成特定结构的数据体在持久化的同时由需要数据的业务消费。在被规则引擎消费时数据被处理为Rule Event,规则引擎CEP(Complex Event Processing,是复杂事件处理系统)根据特定规则Rule和事件生成规则事件,Common Event。

这里写图片描述

2,该模块在APM整体结构中的位置

这里写图片描述

3,CEP与系统交互设计

这里写图片描述

功能支持:

规则命名;规则有效性检查;事件统计;事件告警级别;告警级别变更报告;

接口:

规则的增删查改;规则包的生成;规则库的创建;规则包向规则库的推送;kafka事件获取;规则库中规则包的动态加载;告警事件输出到kafka;

4,详细设计
这里写图片描述

规则文件实例:

package com.aires.oespaas.cep.rules;import com.aires.oespaas.cep.common.model.ApmSourceEvent;import com.aires.oespaas.cep.common.model.ApmResultEvent;import com.aires.oespaas.cep.common.model.ApmResultSet;declare ApmSourceEvent     @role(event)     @Expires(10m)     @Timestamp(timestamp)endrule "ResponseTimeRule"   no-loop   enabled true     when       $t1: ApmSourceEvent($objectName: objectname,["applicationlication=HelloWord"] contains $objectName,$objectType: objecttype == "applicationlication",  $timestamp:timestamp,$metricName:metricname == "responsetime")       $flag : Integer() from accumulate(            $t2: ApmSourceEvent(                objectname == $objectName,                objecttype == $objectType,                metricname == $metricName,                $metricValue : metricvalue,                this before[0m, 29m59s] $t1            ),            init(int result = 10070;),            action(  if ($metricValue <=2000.0 && result <=10070) {result =10070;}  else if ($metricValue >5000.0){result =10072;}  else if ($metricValue >2000.0 && $metricValue <= 5000.0 && result !=10072){result =10071 ;} ),            result( new Integer( result ) )        )    then       String info = "rulename:rules_Res,applicationlication" + $metricName;       String detail = "";       if($flag == 10070){         detail = info + ";ruledetail:calls< 2000.0 in last 30min;";       }       if($flag == 10071){          detail = info + ";ruledetail: 2000.0<responsetime< 5000.0 in last 30min;";       }       if($flag == 10072){          detail = info + ";ruledetail: responsetime>5000.0  in last 30min;";     }         channels["apmDefaultChannel"].send(new ApmResultSet($metricName,new ApmResultEvent($flag,$objectName,detail, $timestamp)));endrule "ErrorsRule"   no-loop   enabled true     when       $t1: ApmSourceEvent($objectName: objectname,["applicationlication=HelloWord"] contains $objectName,$objectType: objecttype == "applicationlication",  $timestamp:timestamp,$metricName:metricname == "errors")       $flag : Integer() from accumulate(            $t2: ApmSourceEvent(                objectname == $objectName,                objecttype == $objectType,                metricname == $metricName,                $metricValue : metricvalue,                this before[0m, 9m59s] $t1            ),            init(int result = 10030;),            action(  if ($metricValue <=1.0 && result <=10030) {result =10030;}  else if ($metricValue >3.0){result =10032;}  else if ($metricValue >1.0 && $metricValue <= 3.0 && result !=10032){result =10031 ;} ),            result( new Integer( result ) )        )    then       String info = "rulename:rules_errors,applicationlication" + $metricName;       String detail = "";       if($flag == 10030){         detail = info + ";ruledetail:calls< 1.0 in last 10min;";       }       if($flag == 10031){          detail = info + ";ruledetail: 1.0<errors< 3.0 in last 10min;";       }       if($flag == 10032){          detail = info + ";ruledetail: errors>3.0  in last 10min;";     }         channels["apmDefaultChannel"].send(new ApmResultSet($metricName,new ApmResultEvent($flag,$objectName,detail, $timestamp)));endrule "CallsRule"   no-loop   enabled true     when       $t1: ApmSourceEvent($objectName: objectname,["applicationlication=HelloWord"] contains $objectName,$objectType: objecttype == "applicationlication",  $timestamp:timestamp,$metricName:metricname == "calls")       $flag : Integer() from accumulate(            $t2: ApmSourceEvent(                objectname == $objectName,                objecttype == $objectType,                metricname == $metricName,                $metricValue : metricvalue,                this before[0m, 9m59s] $t1            ),            init(int result = 10010;),            action(  if ($metricValue <=1.0 && result <=10010) {result =10010;}  else if ($metricValue >3.0){result =10012;}  else if ($metricValue >1.0 && $metricValue <= 3.0 && result !=10012){result =10011 ;} ),            result( new Integer( result ) )        )    then       String info = "rulename:rules_calls,applicationlication" + $metricName;       String detail = "";       if($flag == 10010){         detail = info + ";ruledetail:calls< 1.0 in last 10min;";       }       if($flag == 10011){          detail = info + ";ruledetail: 1.0<calls< 3.0 in last 10min;";       }       if($flag == 10012){          detail = info + ";ruledetail: calls>3.0  in last 10min;";     }         channels["apmDefaultChannel"].send(new ApmResultSet($metricName,new ApmResultEvent($flag,$objectName,detail, $timestamp)));end
0 0
原创粉丝点击