当前位置: 首页>編程日記>正文

springboot整合规则引擎URule

springboot整合规则引擎URule

前言: 项目中要使用规则引擎,所以本人有尝试使用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 {@Beanpublic 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 {@Beanpublic ServletRegistrationBean registerURuleServlet(){return new ServletRegistrationBean(new URuleServlet(),"/urule/*");}
} 

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

spring:datasource:jdbc-url: 这里写数据库地址driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 1234jackson:default-property-inclusion: non_nullserver: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:8755knowledgeUpdateCycle: 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(..)方法必须传入的是"项目名/知识包名",我之前就没有创建知识包,写的是规则名所以获取服务失败了.

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


https://www.fengoutiyan.com/post/15272.html

相关文章:

  • springboot启动流程简单
  • SpringBootApplication
  • springboot流程
  • Springboot注解
  • SpringBoot项目
  • Spring boot
  • bootstrapvue整合
  • springboot与vue整合
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機