SpringBoot集成easy-rules规则引擎流程详解

 更新时间:2023年03月28日 09:58:42   作者:QiHY  
这篇文章主要介绍了SpringBoot集成easy-rules规则引擎流程,合理的使用规则引擎可以极大的减少代码复杂度,提升代码可维护性。业界知名的开源规则引擎有Drools,功能丰富,但也比较庞大

合理的使用规则引擎可以极大的减少代码复杂度,提升代码可维护性。业界知名的开源规则引擎有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\")",说明规则成功执行了。

到此这篇关于SpringBoot集成easy-rules规则引擎流程详解的文章就介绍到这了,更多相关SpringBoot集成easy-rules内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 初次体验MyBatis的注意事项

    初次体验MyBatis的注意事项

    今天给大家带来的是关于MyBatis的相关知识,文章围绕着MyBatis的用法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • JDK动态代理过程原理及手写实现详解

    JDK动态代理过程原理及手写实现详解

    这篇文章主要为大家介绍了JDK动态代理过程原理及手写实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • xxl-job的部署及springboot集成使用示例详解

    xxl-job的部署及springboot集成使用示例详解

    XXL-Job是一个分布式任务调度平台,可进行任务调度、管理和监控,并提供任务分片、失败重试、动态分配等功能,这篇文章主要介绍了xxl-job的部署及springboot集成使用,需要的朋友可以参考下
    2023-06-06
  • Spring很常用的@Conditional注解的使用场景和源码解析

    Spring很常用的@Conditional注解的使用场景和源码解析

    今天要分享的是Spring的注解@Conditional,@Conditional是一个条件注解,它的作用是判断Bean是否满足条件,本文详细介绍了@Conditional注解的使用场景和源码,需要的朋友可以参考一下
    2023-04-04
  • Java枚举类实现Key-Value映射的多种实现方式

    Java枚举类实现Key-Value映射的多种实现方式

    在 Java 开发中,枚举(Enum)是一种特殊的类,本文将详细介绍 Java 枚举类实现 key-value 映射的多种方式,有需要的小伙伴可以根据需要进行选择
    2025-04-04
  • btrace定位生产故障的方法示例

    btrace定位生产故障的方法示例

    这篇文章主要介绍了btrace定位生产故障的方法示例,文中通过示例代码介绍的很详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-02-02
  • spring声明式事务@Transactional开发常犯的几个错误及最新解决方案

    spring声明式事务@Transactional开发常犯的几个错误及最新解决方案

    使用声明式事务@Transactional进行事务一致性的管理,在开发过程中,发现很多开发同学都用错了spring声明式事务@Transactional或使用不规范,导致出现各种事务问题,这篇文章主要介绍了spring声明式事务@Transactional开发常犯的几个错误及解决办法,需要的朋友可以参考下
    2024-02-02
  • 关于web项目读取classpath下面文件的心得分享

    关于web项目读取classpath下面文件的心得分享

    这篇文章主要介绍了关于web项目读取classpath下面文件的心得,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Springboot消除switch-case过程解析

    Springboot消除switch-case过程解析

    这篇文章主要介绍了Springboot消除switch-case过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • java实现微信抢红包算法

    java实现微信抢红包算法

    这篇文章主要为大家详细介绍了java实现微信抢红包算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09

最新评论