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

最近公司准备启动一个风险系统【Springboot + urule 】

最近公司准备启动一个风险系统【Springboot + urule 】

Springboot 集成 urule

最近公司准备启动一个风险系统, 架构中用到 urule 与 flowable, 捣鼓了好几天, 踩了很多坑, 算是初步写出一个 demo, 顺手记录一下

Urule2

URule 是一款基于 RETE 算法纯 Java 的开源规则引擎产品,提供了向导式规则集、脚本式规则集、决策表、决策树、评分卡及决策流共六种类型的规则定义方式,配合基于 WEB 的设计器,可快速实现规则的定义、维护与发布。用来替换原有的 drools 规则引擎, 有一部分原因是因为 URule 自带了配置规则的 UI 界面 本例中采用 urule 客户端与服务端分离的设计

Urule Server

urule 的 Server 端, 用来配置规则 (知识包), 并暴露给客户端, 本例中知识库存储在 mysql 数据库中 springboot 的配置可以详见 URule 官方文档

http://wiki.bsdn.org/display/urule2/

1. 配置 Urule Servlet

URuleServletRegistration.java

@Component
public class URuleServletRegistration {@Beanpublic ServletRegistrationBean registerURuleServlet(){return new ServletRegistrationBean(new URuleServlet(),"/urule/*");}
}

2. 配置 urule 知识库数据源、导入配置文件

application.yml

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/world?useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 1234jackson:default-property-inclusion: non_nullurule:repository:databasetype: mysqldatasourcename: datasourceserver:port: 8787

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();}
}

3. 启动 Application 类

Application.java

@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

访问地址:http://localhost:8787/urule/frame 即可看到 urule 的规则配置页面

77d8236741adf5bf1634cd33d31c2110.png


Urule Client

Urule 的客户端, 即调用规则的一方

1. 配置 urule 知识库地址

application.yml

urule:resporityServerUrl: http://localhost:8787knowledgeUpdateCycle: 1server:port: 7878

2. 引入 urule 配置文件

RuleConfig.java

@Configuration
@ImportResource({"classpath:urule-core-context.xml"})
public class RuleConfig {@Beanpublic PropertySourcesPlaceholderConfigurer propertySourceLoader() {PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();configurer.setIgnoreUnresolvablePlaceholders(true);configurer.setOrder(1);return configurer;}
}

3. 配置 KnowledgePackageReceiverServlet

此 Servlet 用于接收 Urule 服务端发布的知识包 (不想用这个功能可以不配)

URuleServletRegistration.java

@Component
public class URuleServletRegistration {@Beanpublic ServletRegistrationBean registerURuleServlet(){return new ServletRegistrationBean(new KnowledgePackageReceiverServlet(),"/knowledgepackagereceiver");}
}

4. 启动 Application 类

RuleApplication.java

@SpringBootApplication
public class RuleApplication {public static void main(String[] args) {SpringApplication.run(RuleApplication.class, args);}
}

到这里 Urule 的服务端和客户端就都配置完了。

测试规则

由于嫌官方文档的 sample 太麻烦, 这里我自己写了个简单的规则作为测试用途

1. 添加规则 & 发布

配置参数

d484a111eed51216dc66b9be44c05201.png

配置规则

大于 50 的数会乘以 10, 小于 50 的数会除以 2

71f49b40f8529ac4e9f38792811645d1.png

配置推送客户端

2a57a7a4414ae3a1bb15d3d96ea31981.png

发布知识包

bee69c7199a2ce0e1bfbb1371fba6b62.png

2. 编写 Controller 测试

RuleController.java

@RestController
public class RuleController {@RequestMapping("rule")public String rule(@RequestParam String data) throws IOException {//创建一个KnowledgeSession对象KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("aaa/bag");KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);Integer integer = Integer.valueOf(data);Map<String, Object> param = new HashMap();param.put("var", integer);session.fireRules(param);Integer result = (Integer) session.getParameter("var");return String.valueOf(result);}
}

访问 http://localhost:7878/rule?data=50 和 http://localhost:7878/rule?data=40可以看到页面上分别打印 500 和 20, 执行规则成功


源码地址

https://github.com/worstEzreal/urule_springboot


作者:worstezreal

来源链接:

https://blog.csdn.net/worstezreal/article/details/78968710

4bc6f52008eb1b7f70ba719af3393a4a.png


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

相关文章:

  • 理财2级风险
  • 一键启动的寿命
  • 一键启动和钥匙启动的区别
  • 一般风险
  • redis启动报错
  • Spring boot
  • Spring Security
  • SpringBoot项目
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,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尋找肇事司機