SpringBoot集成easy-rules規(guī)則引擎流程詳解
合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護(hù)性。業(yè)界知名的開(kāi)源規(guī)則引擎有Drools,功能豐富,但也比較龐大。在一些簡(jiǎn)單的場(chǎng)景中,我們只需要簡(jiǎn)易的規(guī)則引擎就能滿(mǎn)足要求。
本文介紹一個(gè)小巧的規(guī)則引擎 easy-rules,作為一個(gè)lib庫(kù)提供,支持spring的SPEL表達(dá)式,可以很好的集成在spring項(xiàng)目中。
具體的代碼參照 示例項(xiàng)目 https://github.com/qihaiyan/springcamp/tree/master/spring-easy-rule
一、概述
通過(guò)將業(yè)務(wù)規(guī)則配置的配置文件中,可以精簡(jiǎn)代碼,同時(shí)已于維護(hù),當(dāng)規(guī)則修改時(shí),只需要修改配置文件即可。easy-rules是一個(gè)小巧的規(guī)則引擎,支持spring的SPEL表達(dá)式,同時(shí)還支持 Apache JEXL 表達(dá)式和 MVL 表達(dá)式。
二、項(xiàng)目中加入依賴(lài)
在項(xiàng)目的gradle中增加依賴(lài)關(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ī)則通過(guò) condition 進(jìn)行配置,當(dāng)滿(mǎn)足規(guī)則時(shí),會(huì)調(diào)用 actions 中配置的動(dòng)作。
示例項(xiàng)目使用了spring的SPEL表達(dá)式進(jìn)行規(guī)則配置,配置文件中配置了2個(gè)規(guī)則,第一個(gè)規(guī)則通過(guò) person
這個(gè)spring bean中的getAdult()判斷是否滿(mǎn)足規(guī)則,滿(mǎn)足規(guī)則時(shí)調(diào)用三個(gè)方法。
在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"
四、代碼中對(duì)規(guī)則引擎進(jìn)行配置
通過(guò) RuleEngineConfig
這個(gè)spring的配置類(lèi)對(duì)規(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
這個(gè)spring bean,通過(guò)ruleEngineTemplate觸發(fā)規(guī)則引擎的執(zhí)行。
五、執(zhí)行規(guī)則引擎
ruleEngineTemplate
配置好后,我們可以在業(yè)務(wù)代碼中執(zhí)行規(guī)則引擎,處理配置文件中配置的業(yè)務(wù)規(guī)則:
最為演示,我們將規(guī)則引擎的執(zhí)行代碼放到了 Application 的 run 方法中,程序啟動(dòng)后立即執(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í)行后可以看到控制臺(tái)里打印了 Shop: Sorry, you are not allowed to buy alcohol
,這個(gè)內(nèi)容對(duì)應(yīng)的是我們?cè)谝?guī)則文件中的actions中配置的 "T(java.lang.System).out.println(\"Shop: Sorry, you are not allowed to buy alcohol\")"
,說(shuō)明規(guī)則成功執(zhí)行了。
到此這篇關(guān)于SpringBoot集成easy-rules規(guī)則引擎流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成easy-rules內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea中同一SpringBoot項(xiàng)目多端口啟動(dòng)
本文主要介紹了idea中同一SpringBoot項(xiàng)目多端口啟動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Java實(shí)現(xiàn)在Word指定位置插入分頁(yè)符
在Word插入分頁(yè)符可以在指定段落后插入,也可以在特定文本位置處插入。本文將以Java代碼來(lái)操作以上兩種文檔分頁(yè)需求,需要的可以參考一下2022-04-04利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁(yè)數(shù)據(jù)查詢(xún)的過(guò)程詳解
這篇文章主要給大家介紹了關(guān)于利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁(yè)數(shù)據(jù)查詢(xún)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的操作方法
這篇文章主要介紹了IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的相關(guān)知識(shí),補(bǔ)充介紹了IDEA查找和替換實(shí)用正則,感興趣的朋友跟隨小編一起看看吧2024-01-01mybatis中使用oracle關(guān)鍵字出錯(cuò)的解決方法
這篇文章主要給大家介紹了關(guān)于mybatis中使用oracle關(guān)鍵字出錯(cuò)的解決方法,文中通過(guò)示例代碼將解決的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)
這篇文章主要介紹了SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01詳解springboot解決第三方依賴(lài)jar包的問(wèn)題
本篇文章主要介紹了詳解springboot解決第三方依賴(lài)jar包的問(wèn)題,解決了第三方依賴(lài)jar包的問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09