Springboot整合Urule的方法步驟
摘要:
Urule決策引擎可簡(jiǎn)化開(kāi)發(fā)校驗(yàn)、決策類代碼,底層由java語(yǔ)言實(shí)現(xiàn),可基于SpringBoot快速配置,因?yàn)閁rule工具目前為非常用工具,網(wǎng)上關(guān)于SpringBoot整合Urule資料匱乏,一直自己摸索,簡(jiǎn)單的環(huán)境搭建也費(fèi)了些功夫,遇到些坑,作此記錄
本次記錄主要記錄Urule-Serve端Urule-Client端分開(kāi)部署的模式,這種使用場(chǎng)景也會(huì)更多;嵌入式成一個(gè)項(xiàng)目的配置和Urule-Server端一致。
一、Urule-Server端:
1.1、 基于maven的SpringBoot基本環(huán)境搭建請(qǐng)參考SpringBoot教程
1.2、引入U(xiǎn)rule相關(guān)依賴,urule-console-pro,開(kāi)源版本可到https://search.maven.org
中心搜索,依賴如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.bstek.urule</groupId>
<artifactId>urule-console-pro</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
1.3、配置文件:兩個(gè),appplication.yml , application.properties
appplication.yml,配置數(shù)據(jù)庫(kù)信息(我們把urule項(xiàng)目存到數(shù)據(jù)庫(kù)中)
server: port: 8081 spring: application: name: UruleServer datasource: name: datasource jdbc-url: jdbc:mysql://127.0.0.1:3306/urule?useUnicode=true&characterEncoding=utf-8 username: root password: 666666 # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
注意,我這此刻DataSource下不jdbc-url而不是url。根據(jù)SpringBoot版本自行調(diào)整
application.properties,配置項(xiàng)目?jī)?chǔ)存位置
#若為本地環(huán)境需配置此路徑 #urule.repository.dir=F:/EclipsePractice/03_SpringCloud/repo4rule #若為數(shù)據(jù)庫(kù),配置此項(xiàng),兩項(xiàng)均不配則系統(tǒng)指定默認(rèn)地址 urule.repository.databasetype=mysql urule.repository.datasourcename=datasource ignore-unresolvable=true order=1
1.4、初始化bean
datesource
@Configuration
public class configuration {
@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();
}
}
serverlet
@Component
public class URuleServletRegistration
{
@Bean
public ServletRegistrationBean<HttpServlet> registerURuleServlet()
{
return new ServletRegistrationBean(new URuleServlet(), new String[] { "/urule/*" });
}
}
1.5、啟動(dòng)類:
@SpringBootApplication
@ImportResource({"classpath:urule-console-context.xml"})
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
二、客戶端調(diào)用:
2.1、配置類
application.yml server: port: 8090 spring: application: name: UruleClient datasource: name: datasource url: jdbc:mysql://127.0.0.1:3306/myland?useUnicode=true&characterEncoding=utf-8 username: root password: 666666 # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 urule: ###服務(wù)端發(fā)現(xiàn)地址 resporityServerUrl: http://localhost:8081 ###knowledgeUpdateCycle為0時(shí),不是檢查緩存,每次都從服務(wù)端拉取,為1時(shí),會(huì)先查找緩存 knowledgeUpdateCycle: 1
2.2、初始化bean
@Configuration
public class RuleConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourceLoader() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setOrder(1);
return configurer;
}
}
@Component
public class URuleServletRegistration {
//此Servlet用于接收Urule服務(wù)端發(fā)布的知識(shí)包,使用開(kāi)源版本時(shí)刪除或者注釋這個(gè)bean
@Bean
public ServletRegistrationBean registerURuleServlet(){
return new ServletRegistrationBean(new KnowledgePackageReceiverServlet(),"/knowledgepackagereceiver");
}
}
2.3、controller:
@RestController
public class TestController {
@RequestMapping("/rule")
public String getRara(@RequestParam String data)throws IOException{
KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
//參數(shù),Urule項(xiàng)目名/知識(shí)包名
KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("letasa/pare");
KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
Integer integer = Integer.valueOf(data);
Map<String, Object> param = new HashMap();
//參數(shù),var,傳入?yún)?shù),和參數(shù)庫(kù)中定義一致
param.put("var", integer);
session.fireRules(param);
//result,返回參數(shù),和參數(shù)庫(kù)中定義一致
Integer result = (Integer) session.getParameter("result");
return String.valueOf(result);
}
}
2.4、啟動(dòng)類
@SpringBootApplication
@ImportResource({"classpath:urule-core-context.xml"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Urule項(xiàng)目配置

參數(shù)庫(kù)

規(guī)則

知識(shí)包及發(fā)布
注:Rrule-pro版本支持將知識(shí)包推送給具體客戶端,客戶端使用時(shí)先調(diào)用緩存,如無(wú)緩存則再到服務(wù)端拉去。但開(kāi)源版本的Urule不支持推送,客戶端只能主動(dòng)到服務(wù)端拉去數(shù)據(jù)。
最后訪問(wèn)客戶端:http://localhost:8090/rule?data=67,或者data=25,分別得到100,20.
success!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot與mybatis整合實(shí)例詳解(完美融合)
- 淺談Springboot整合RocketMQ使用心得
- springboot+springmvc+mybatis項(xiàng)目整合
- springboot整合redis進(jìn)行數(shù)據(jù)操作(推薦)
- 詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置
- spring boot整合CAS配置詳解
- springboot整合freemarker詳解
- spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼
- springboot整合Quartz實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法
相關(guān)文章
使用@SpringBootTest注解進(jìn)行單元測(cè)試
這篇文章主要介紹了使用@SpringBootTest注解進(jìn)行單元測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Netty分布式高性能工具類recycler的使用及創(chuàng)建
這篇文章主要為大家介紹了Netty分布式高性能工具類recycler的使用和創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
java算法導(dǎo)論之FloydWarshall算法實(shí)現(xiàn)代碼
這篇文章主要介紹了算法導(dǎo)論之FloydWarshall算法實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
SpringBoot項(xiàng)目啟動(dòng)健康檢查的操作方法
在現(xiàn)代的微服務(wù)架構(gòu)中,容器化技術(shù)已經(jīng)成為一種主流的部署方式,Docker 作為容器化技術(shù)的代表,提供了一種輕量級(jí)、可移植的解決方案,然而,僅僅將應(yīng)用容器化是不夠的,我們還需要確保這些容器在運(yùn)行時(shí)能夠保持健康狀態(tài),這就是健康檢查發(fā)揮作用的地方2024-12-12
java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包代碼示例
在Java應(yīng)用程序中有時(shí)我們需要從多個(gè)URL地址下載文件,并將這些文件打包成一個(gè)Zip文件進(jìn)行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包的相關(guān)資料,需要的朋友可以參考下2023-11-11
SpringMVC文件上傳請(qǐng)求問(wèn)題分析
這篇文章主要介紹了SpringMVC文件上傳請(qǐng)求,我們發(fā)的請(qǐng)求默認(rèn)都是由DispatcherServlet類的doDispatch()來(lái)處理,這個(gè)方法的邏輯處理的第一步就是處理文件上傳的請(qǐng)求,我們一起來(lái)看看是怎么處理的吧2024-07-07
Spring中事務(wù)幾個(gè)常見(jiàn)的問(wèn)題解決
這篇文章主要介紹了Spring中事務(wù)幾個(gè)常見(jiàn)的問(wèn)題解決,事務(wù)這個(gè)概念是數(shù)據(jù)庫(kù)層面的,Spring只是基于數(shù)據(jù)庫(kù)中的事務(wù)進(jìn)行擴(kuò)展,以及提供了一些能讓程序員更新方便操作事務(wù)的方式2022-08-08

