SpringBoot集成easy-rules规则引擎流程详解
作者:QiHY 发布时间:2022-04-20 11:39:35
合理的使用规则引擎可以极大的减少代码复杂度,提升代码可维护性。业界知名的开源规则引擎有Drools,功能丰富,但也比较庞大。在一些简单的场景中,我们只需要简易的规则引擎就能满足要求。
本文介绍一个小巧的规则引擎 easy-rules,作为一个lib库提供,支持spring的SPEL表达式,可以很好的集成在spring项目中。
具体的代码参照 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-easy-rule
一、概述
通过将业务规则配置的配置文件中,可以精简代码,同时已于维护,当规则修改时,只需要修改配置文件即可。easy-rules是一个小巧的规则引擎,支持spring的SPEL表达式,同时还支持 Apache JEXL 表达式和 MVL 表达式。
二、项目中加入依赖
在项目的gradle中增加依赖关系。
build.gradle:
plugins {
id 'org.springframework.boot' version '3.0.5'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}group = 'cn.springcamp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'configurations {
compileOnly {
extendsFrom annotationProcessor
}
testCompileOnly {
extendsFrom testAnnotationProcessor
}
}repositories {
mavenCentral()
}dependencies {
implementation "org.springframework.boot:spring-boot-starter-json"
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.jeasy:easy-rules-core:4.1.0'
implementation 'org.jeasy:easy-rules-spel:4.1.0'
implementation 'org.jeasy:easy-rules-support:4.1.0'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation 'org.junit.vintage:junit-vintage-engine'
testImplementation 'org.junit.vintage:junit-vintage-engine'
}dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2022.0.1"
}
}test {
useJUnitPlatform()
}
三、配置文件
示例程序将业务规则放到配置文件中,业务规则配置文件(demo-rule.yml)代码:
name: "age rule"
description: ""
priority: 1
condition: "#person.getAdult() == false"
actions:
- "T(java.lang.System).out.println(\"Shop: Sorry, you are not allowed to buy alcohol\")"
- "#person.setAdult(true)"
- "#person.setAge(18)"
---
name: "alcohol rule"
description: ""
priority: 1
condition: "#person.getAdult() == true"
actions:
- "T(java.lang.System).out.println(\"Shop: you are now allowed to buy alcohol\")"
配置文件中的规则通过 condition 进行配置,当满足规则时,会调用 actions 中配置的动作。
示例项目使用了spring的SPEL表达式进行规则配置,配置文件中配置了2个规则,第一个规则通过 person
这个spring bean中的getAdult()判断是否满足规则,满足规则时调用三个方法。
在spring-boot本身的配置文件中 application.yml 配置规则文件:
rule:
skip-on-first-failed-rule: true
skip-on-first-applied-rule: false
skip-on-first-non-triggered-rule: true
rules:
- rule-id: "demo"
rule-file-location: "classpath:demo-rule.yml"
四、代码中对规则引擎进行配置
通过 RuleEngineConfig
这个spring的配置类对规则引擎进行配置:
@Slf4j
@EnableConfigurationProperties(RuleEngineConfigProperties.class)
@Configuration
public class RuleEngineConfig implements BeanFactoryAware {
@Autowired(required = false)
private List<RuleListener> ruleListeners;
@Autowired(required = false)
private List<RulesEngineListener> rulesEngineListeners;
private BeanFactory beanFactory;
@Bean
public RulesEngineParameters rulesEngineParameters(RuleEngineConfigProperties properties) {
RulesEngineParameters parameters = new RulesEngineParameters();
parameters.setSkipOnFirstAppliedRule(properties.isSkipOnFirstAppliedRule());
parameters.setSkipOnFirstFailedRule(properties.isSkipOnFirstFailedRule());
parameters.setSkipOnFirstNonTriggeredRule(properties.isSkipOnFirstNonTriggeredRule());
return parameters;
}
@Bean
public RulesEngine rulesEngine(RulesEngineParameters rulesEngineParameters) {
DefaultRulesEngine rulesEngine = new DefaultRulesEngine(rulesEngineParameters);
if (!CollectionUtils.isEmpty(ruleListeners)) {
rulesEngine.registerRuleListeners(ruleListeners);
}
if (!CollectionUtils.isEmpty(rulesEngineListeners)) {
rulesEngine.registerRulesEngineListeners(rulesEngineListeners);
}
return rulesEngine;
}
@Bean
public BeanResolver beanResolver() {
return new BeanFactoryResolver(beanFactory);
}
@Bean
public RuleEngineTemplate ruleEngineTemplate(RuleEngineConfigProperties properties, RulesEngine rulesEngine) {
RuleEngineTemplate ruleEngineTemplate = new RuleEngineTemplate();
ruleEngineTemplate.setBeanResolver(beanResolver());
ruleEngineTemplate.setProperties(properties);
ruleEngineTemplate.setRulesEngine(rulesEngine);
return ruleEngineTemplate;
}
@Bean
public RuleListener defaultRuleListener() {
return new RuleListener() {
@Override
public boolean beforeEvaluate(Rule rule, Facts facts) {
return true;
}
@Override
public void afterEvaluate(Rule rule, Facts facts, boolean b) {
log.info("-----------------afterEvaluate-----------------");
log.info(rule.getName() + rule.getDescription() + facts.toString());
}
@Override
public void beforeExecute(Rule rule, Facts facts) {
log.info("-----------------beforeExecute-----------------");
log.info(rule.getName() + rule.getDescription() + facts.toString());
}
@Override
public void onSuccess(Rule rule, Facts facts) {
log.info("-----------------onSuccess-----------------");
log.info(rule.getName() + rule.getDescription() + facts.toString());
}
@Override
public void onFailure(Rule rule, Facts facts, Exception e) {
log.info("-----------------onFailure-----------------");
log.info(rule.getName() + "----------" + rule.getDescription() + facts.toString() + e.toString());
}
};
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}
配置文件中配置了 ruleEngineTemplate
这个spring bean,通过ruleEngineTemplate触发规则引擎的执行。
五、执行规则引擎
ruleEngineTemplate
配置好后,我们可以在业务代码中执行规则引擎,处理配置文件中配置的业务规则:
最为演示,我们将规则引擎的执行代码放到了 Application 的 run 方法中,程序启动后立即执行规则引擎:
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
RuleEngineTemplate ruleEngineTemplate;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) {
Person person = new Person();
Facts facts = new Facts();
facts.put("person", person);
ruleEngineTemplate.fire("demo", facts);
}
}
程序执行后可以看到控制台里打印了 Shop: Sorry, you are not allowed to buy alcohol
,这个内容对应的是我们在规则文件中的actions中配置的 "T(java.lang.System).out.println(\"Shop: Sorry, you are not allowed to buy alcohol\")"
,说明规则成功执行了。
来源:https://blog.csdn.net/haiyan_qi/article/details/129778299


猜你喜欢
- 介绍Java状态模式(State Pattern)是一种面向对象的设计模式,它将对象的状态封装成独立的状态对象,并将对象的行为与状态对象解耦
- fopen(打开文件)相关函数 open,fclose表头文件 #include<stdio.h>定义函数 FILE * fop
- 链表是一种复杂的数据结构,其数据之间的相互关系使链表分成三种:单链表、循环链表、双向链表,下面将逐一介绍。链表在数据结构中是基础
- 这几天恰好和朋友谈起了递归,忽然发现不少朋友对于“尾递归”的概念比较模糊,网上搜索一番也没有发现讲解地完整详细的资料,于是写了这么一篇文章,
- 前言前几天恰好面试一个应届生,问了一个很简单的问题:你了解过Mybatis中有几种传参方式吗?没想到其他问题回答的很好,唯独这个问题一知半解
- 本文实例为大家分享了java使用influxDB数据库的具体代码,供大家参考,具体内容如下1.pom.xml中导入jar包依赖<!--
- 本文实例讲述了Spring实战之属性占位符配置器用法。分享给大家供大家参考,具体如下:一 配置文件<?xml version=&quo
- 线程组线程组可以批量管理线程和线程组对象。一级关联例子如下,建立一级关联。public class MyThread43 implement
- 网络爬虫在信息检索与处理中有很大的作用,是收集网络信息的重要工具。接下来就介绍一下爬虫的简单实现。爬虫的工作流程如下爬虫自指定的URL地址开
- 为什么需要方法回调?方法回调是功能定义和功能分离的一种手段,是一种松耦合的设计思想。在JAVA中回调是通过接口来实现的。作为一种系统架构,必
- 背景:日常开发ERP系统,会有一些工单或者合同之类需要填写打印。我们就会将其word模板来通过系统自动化填写并转换为PDF格式(PDF文件打
- 前言在JDK当中给我们提供的各种并发工具当中,比如ReentrantLock等等工具的内部实现,经常会使用到一个工具,这个工具就是LockS
- Form1主窗体:public delegate void SetVisiableHandler();//定义委托类型private voi
- java.math.BigDecimal及加减乘除计算BigDecimal简介BigDecimal用来对需要更大或更小的数进行任意精度的运算
- 本文实例讲述了Android编程学习之抽象类AbsListView用法。分享给大家供大家参考,具体如下:一、继承关系public abstr
- 在app中图片的轮播显示可以说是非常常见的实现效果了,其实现原理不过是利用ViewPager,然后利用handler每隔一定的时间将View
- 在很多其他框架中,比如Python的Flask、node.js的KOA,Controller要想能够响应前端的请求都需要我们主动去注册到应用
- 本文实例讲述了Java实现过滤掉map集合中key或value为空的值。分享给大家供大家参考,具体如下:import java.util.C
- 错误展示:Information:java: Errors occurred while compiling module 'emp
- spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuc