SpringBoot集成easy-rules規(guī)則引擎流程詳解
合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護性。業(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ī)則配置的配置文件中,可以精簡代碼,同時已于維護,當(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 MVC+Mybatis實現(xiàn)Mysql分頁數(shù)據(jù)查詢的過程詳解
這篇文章主要給大家介紹了關(guān)于利用Spring MVC+Mybatis實現(xiàn)Mysql分頁數(shù)據(jù)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的操作方法
這篇文章主要介紹了IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的相關(guān)知識,補充介紹了IDEA查找和替換實用正則,感興趣的朋友跟隨小編一起看看吧2024-01-01mybatis中使用oracle關(guān)鍵字出錯的解決方法
這篇文章主要給大家介紹了關(guān)于mybatis中使用oracle關(guān)鍵字出錯的解決方法,文中通過示例代碼將解決的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-08-08SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)
這篇文章主要介紹了SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01