SpringBoot集成easy-rules規(guī)則引擎流程詳解
合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護(hù)性。業(yè)界知名的開源規(guī)則引擎有Drools,功能豐富,但也比較龐大。在一些簡單的場景中,我們只需要簡易的規(guī)則引擎就能滿足要求。
本文介紹一個小巧的規(guī)則引擎 easy-rules,作為一個lib庫提供,支持spring的SPEL表達(dá)式,可以很好的集成在spring項目中。
具體的代碼參照 示例項目 https://github.com/qihaiyan/springcamp/tree/master/spring-easy-rule
一、概述
通過將業(yè)務(wù)規(guī)則配置的配置文件中,可以精簡代碼,同時已于維護(hù),當(dāng)規(guī)則修改時,只需要修改配置文件即可。easy-rules是一個小巧的規(guī)則引擎,支持spring的SPEL表達(dá)式,同時還支持 Apache JEXL 表達(dá)式和 MVL 表達(dá)式。
二、項目中加入依賴
在項目的gradle中增加依賴關(guān)系。
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()
}
三、配置文件
示例程序?qū)I(yè)務(wù)規(guī)則放到配置文件中,業(yè)務(wù)規(guī)則配置文件(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\")"
配置文件中的規(guī)則通過 condition 進(jìn)行配置,當(dāng)滿足規(guī)則時,會調(diào)用 actions 中配置的動作。
示例項目使用了spring的SPEL表達(dá)式進(jìn)行規(guī)則配置,配置文件中配置了2個規(guī)則,第一個規(guī)則通過 person這個spring bean中的getAdult()判斷是否滿足規(guī)則,滿足規(guī)則時調(diào)用三個方法。
在spring-boot本身的配置文件中 application.yml 配置規(guī)則文件:
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"
四、代碼中對規(guī)則引擎進(jìn)行配置
通過 RuleEngineConfig這個spring的配置類對規(guī)則引擎進(jìn)行配置:
@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觸發(fā)規(guī)則引擎的執(zhí)行。
五、執(zhí)行規(guī)則引擎
ruleEngineTemplate配置好后,我們可以在業(yè)務(wù)代碼中執(zhí)行規(guī)則引擎,處理配置文件中配置的業(yè)務(wù)規(guī)則:
最為演示,我們將規(guī)則引擎的執(zhí)行代碼放到了 Application 的 run 方法中,程序啟動后立即執(zhí)行規(guī)則引擎:
@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);
}
}
程序執(zhí)行后可以看到控制臺里打印了 Shop: Sorry, you are not allowed to buy alcohol,這個內(nèi)容對應(yīng)的是我們在規(guī)則文件中的actions中配置的 "T(java.lang.System).out.println(\"Shop: Sorry, you are not allowed to buy alcohol\")",說明規(guī)則成功執(zhí)行了。
到此這篇關(guān)于SpringBoot集成easy-rules規(guī)則引擎流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成easy-rules內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring很常用的@Conditional注解的使用場景和源碼解析
今天要分享的是Spring的注解@Conditional,@Conditional是一個條件注解,它的作用是判斷Bean是否滿足條件,本文詳細(xì)介紹了@Conditional注解的使用場景和源碼,需要的朋友可以參考一下2023-04-04
Java枚舉類實現(xiàn)Key-Value映射的多種實現(xiàn)方式
在 Java 開發(fā)中,枚舉(Enum)是一種特殊的類,本文將詳細(xì)介紹 Java 枚舉類實現(xiàn) key-value 映射的多種方式,有需要的小伙伴可以根據(jù)需要進(jìn)行選擇2025-04-04
spring聲明式事務(wù)@Transactional開發(fā)常犯的幾個錯誤及最新解決方案
使用聲明式事務(wù)@Transactional進(jìn)行事務(wù)一致性的管理,在開發(fā)過程中,發(fā)現(xiàn)很多開發(fā)同學(xué)都用錯了spring聲明式事務(wù)@Transactional或使用不規(guī)范,導(dǎo)致出現(xiàn)各種事務(wù)問題,這篇文章主要介紹了spring聲明式事務(wù)@Transactional開發(fā)常犯的幾個錯誤及解決辦法,需要的朋友可以參考下2024-02-02
關(guān)于web項目讀取classpath下面文件的心得分享
這篇文章主要介紹了關(guān)于web項目讀取classpath下面文件的心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

