springboot整合规则引擎URule

相比其他规则引擎,urule有如下特点:
响应毫秒级
支持规则在client的缓存,并自动比对更新时间,减少网络IO,但客户端需要引入jar
原生支持动态规则和两种规则持久化方式(文件,数据库)
支持决策树,决策表等
集成了可视化规则管理界面,管理规则方便
社区活跃度低,技术支持需要付费
功能强大,但开源版功能有限
这里介绍一下如何将URule整合到springBoot项目中

官方文档:http://www.bstek.com/resources/doc/ 

 先来瞅瞅他的管理界面长啥样!

 按层级从上往下是 项目(就是图中的UCTest)--知识包--决策表/决策树等--库

1.库是最低的层级: 在这里创建需要校验的基本数据或者POJO, 如图上我就是想对UserParam进行校验.将名称和全类名添加进去.

2.决策集\决策树就是一个个具体的规则了.

在这里以库为校验对象创建规则.

3.知识包是多个决策树/决策集的集合,创建知识包之后再加入决策集

4.项目下面包含多个知识包,可以将知识包暴露给多个客户端

直接在树菜单上右键即可操作.

服务端配置:
 pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
                <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
                
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
           </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        
          <!-- urule规则引擎 -->
        <dependency>
            <groupId>com.bstek.urule</groupId>
            <artifactId>urule-console</artifactId>
            <version>2.1.6</version>
        </dependency>
        
        
    </dependencies>
需要两个核心类:

1.config.java

@Configuration
@ImportResource({"classpath:urule-console-context.xml"})
@PropertySource(value = {"classpath:urule-console-context.properties"})
public class Config {
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourceLoader() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setIgnoreUnresolvablePlaceholders(true);
        configurer.setOrder(1);
        return configurer;
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource datasource() {
        return DataSourceBuilder.create().build();
    }
}
这里其实就是引入jar包中的配置文件:

classpath:urule-console-context.xml

classpath:urule-console-context.properties

非maven项目参考: https://blog.csdn.net/su1573/article/details/82178544

2.URuleServletRegistration.java 

package com.example.demo.ur;
 
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
 
import com.bstek.urule.console.servlet.URuleServlet;
 
@Component
public class URuleServletRegistration {
    @Bean
    public ServletRegistrationBean registerURuleServlet(){
        return new ServletRegistrationBean(new URuleServlet(),"/urule/*");
    }

3. 配置文件:application.yml(当然,这些配置你想放到properties文件或者配置中心都可以), 主要配置持久化的数据库地址

spring:
  datasource:
    jdbc-url: 这里写数据库地址
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 1234
  jackson:
    default-property-inclusion: non_null
 
server:
  port: 8755
然后直接用springBoot启动类启动就好了,再输入http://localhost:8755/urule/frame

就可以看到管理界面了

客户端配置
 pom.xml

<!-- urule规则引擎 -->
        <dependency>
            <groupId>com.bstek.urule</groupId>
            <artifactId>urule-console</artifactId>
            <version>2.1.6</version>
        </dependency>
application.yml配置服务端地址:

urule:
  resporityServerUrl: http://localhost:8755
  knowledgeUpdateCycle: 1
如果“urule.knowledgeUpdateCycle”属性值大于1,那么客户端会首先检查本地缓存中是否存在指定的知识包,如果存在,那么就拿当前时间与本地缓存中的知识包的时间戳进行比较,如果小于“urule.knowledgeUpdateCycle”属性值,那么就直接取这个知识包,如果大于它,那么就到URule Server上通过时间戳检查当前知识包有没有更新,如果有更新则取到客户端,同时更新客户端缓存里对应的知识包;如果没有更新,那么就直接采用当前客户端缓存里的知识包。

客户端调用:

package tt;
import rete.test.Dept;
import rete.test.Employee;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;
 
public class Invoke {
    public void doTest() throws Exception{
        //从Spring中获取KnowledgeService接口实例
        KnowledgeService service=(KnowledgeService)Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        //通过KnowledgeService接口获取指定的资源包"项目名/知识包名"
        KnowledgePackage knowledgePackage=service.getKnowledge("UCTest/UCBag");
        //通过取到的KnowledgePackage对象创建KnowledgeSession对象
        KnowledgeSession session=KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
 
        Employee employee=new Employee();
        Dept dept=new Dept();
        dept.setLevel(12);
        employee.setDept(dept);
        employee.setSalary(111000);
        //将业务数据对象Employee插入到KnowledgeSession中
        session.insert(employee);
        //执行所有满足条件的规则
        session.fireRules();
    }
}
可以直接把上面这段代码嵌入web项目的controller

getKnowledge(..)方法必须传入的是"项目名/知识包名",我之前就没有创建知识包,写的是规则名所以获取服务失败了.

新增修改知识包之后必须先发布知识包,再将其推送至客户端才可以使用

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Spring Boot + URule的Demo: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.bstek.urule</groupId> <artifactId>urule-core</artifactId> <version>2.0.26.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 接着,创建一个规则文件,例如test1.xml,内容如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <rule-set xmlns="http://www.bstek.com.cn/urule" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bstek.com.cn/urule http://www.bstek.com.cn/schema/urule.xsd"> <rule name="test1"> <if> <and> <var compare="equal" left="param1" right="'value1'"/> <var compare="greaterThan" left="param2" right="'10'"/> </and> </if> <then> <action var="result">"true"</action> </then> <else> <action var="result">"false"</action> </else> </rule> </rule-set> ``` 3. 在application.properties文件中添加以下配置: ```properties # 配置规则文件存放路径 urule.fileRepository=com/bstek/urule/demo urule.datapath=classpath:urule ``` 4. 创建一个Controller,用于测试规则: ```java @RestController public class TestController { @Autowired private RuleRuntimeService ruleRuntimeService; @GetMapping("/test") public String test(@RequestParam String param1, @RequestParam int param2) { Map<String, Object> paramMap = new HashMap<>(); paramMap.put("param1", param1); paramMap.put("param2", param2); List<RuleExecutionResult> results = ruleRuntimeService.executeRules("test1", paramMap); for (RuleExecutionResult result : results) { return result.getActionResult().getValue().toString(); } return "false"; } } ``` 其中,@GetMapping("/test")表示该接口为GET请求,参数param1和param2分别对应规则文件中的param1和param2变量,"test1"为规则文件名称。 5. 启动Spring Boot应用,访问http://localhost:8080/test?param1=value1&param2=20,可以看到返回结果为"true"。 希望这个Demo能够对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值