风控系统一:dtools规则引擎学习使用
2020-04-14 本文已影响0人
源码互助空间站
一、dtools在风控系统中的作用
我们的风控系统主要作用是在功能系统前进行拦截,比如同一IP地址1分钟内只准登录3次、同一账号只准绑定的设备号为3个等。在这前提下需要有多个规则进行校验,本系统就是使用dtool工具来执行规则引擎的。
二、dtool简单demo使用
2.1 创建maven项目,包结构如下:
image.png2.2 pom.xml引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<drools.version>6.1.0.Final</drools.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
</dependencies>
2.2 编写规则
规则文件放在resources下rules文件夹下
package rules
//package 是规则文件在resource的位置
import vtool.po.Stu;
//导入java文件
rule "rule1"
salience 1
when
$stu:Stu(age>30)//这里就是LHS
then
System.out.println("age>30岁的人:"+$stu.getName());//RHS
end
rule "rule2"
salience 2
when
$stu:Stu(gender=="male",age>25)
then
System.out.println("性别为男,age>25岁的人:"+$stu.getName());
end
rule "rule3"
salience 3
when
$stu:Stu(getName()=="张三")
then
System.out.println("名字为:"+$stu.getName());
2.3 实体类bean
package vtool.po;
public class Stu {
private String name;
private Integer age;
private String gender;
public Stu() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Stu{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public Stu(String name, Integer age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
2.4 测试类
package vtool.app;
import org.drools.core.io.impl.ClassPathResource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.definition.KnowledgePackage;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import vtool.po.Stu;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class StuTest {
private static KieContainer container = null;
private static KieSession statefulKieSession = null;
public static void main(String[] args) {
// 规则编译器
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
// 添加规则,并编译
kb.add(new ClassPathResource("rules/stu.drl"), ResourceType.DRL);
// 得到规则包
Collection<KnowledgePackage> collection = kb.getKnowledgePackages();
// 创建base
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
// 加入包
knowledgeBase.addKnowledgePackages(collection);
// 得到session
StatefulKnowledgeSession statefulSession = knowledgeBase.newStatefulKnowledgeSession();
List<Stu> list = getStus();
for (Stu stu : list) {
// 插入fact对象
statefulSession.insert(stu);
}
statefulSession.fireAllRules();
statefulSession.dispose();
System.out.println("end....");
}
public static List<Stu> getStus() {
List<Stu> stus = new ArrayList<>();
stus.add(new Stu("张三", 16, "male"));
stus.add(new Stu("huhu", 18, "male"));
stus.add(new Stu("王五", 32, "male"));
stus.add(new Stu("张红", 23, "female"));
stus.add(new Stu("李四", 35, "male"));
stus.add(new Stu("张小雅", 31, "female"));
return stus;
}
}
2.5 结果
名字为:张三
性别为男,age>25岁的人:李四
性别为男,age>25岁的人:王五
age>30岁的人:张小雅
age>30岁的人:李四
age>30岁的人:王五
end....s