Springboot整合Urule的方法步驟
摘要:
Urule決策引擎可簡化開發(fā)校驗(yàn)、決策類代碼,底層由java語言實(shí)現(xiàn),可基于SpringBoot快速配置,因?yàn)閁rule工具目前為非常用工具,網(wǎng)上關(guān)于SpringBoot整合Urule資料匱乏,一直自己摸索,簡單的環(huán)境搭建也費(fèi)了些功夫,遇到些坑,作此記錄
本次記錄主要記錄Urule-Serve端Urule-Client端分開部署的模式,這種使用場(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,開源版本可到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ù)庫信息(我們把urule項(xiàng)目存到數(shù)據(jù)庫中)
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)目儲(chǔ)存位置
#若為本地環(huán)境需配置此路徑 #urule.repository.dir=F:/EclipsePractice/03_SpringCloud/repo4rule #若為數(shù)據(jù)庫,配置此項(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í)包,使用開源版本時(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ù)庫中定義一致
param.put("var", integer);
session.fireRules(param);
//result,返回參數(shù),和參數(shù)庫中定義一致
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ù)庫

規(guī)則

知識(shí)包及發(fā)布
注:Rrule-pro版本支持將知識(shí)包推送給具體客戶端,客戶端使用時(shí)先調(diào)用緩存,如無緩存則再到服務(wù)端拉去。但開源版本的Urule不支持推送,客戶端只能主動(dòng)到服務(wù)端拉去數(shù)據(jù)。
最后訪問客戶端:http://localhost:8090/rule?data=67,或者data=25,分別得到100,20.
success!
以上就是本文的全部內(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)文章
Spring RestTemplate簡化HTTP通信實(shí)現(xiàn)功能探究
這篇文章主要為大家介紹了Spring框架中的RestTemplate,如果你是個(gè)Java程序員,那么你肯定知道Spring框架的重要性,在Spring的眾多工具中,RestTemplate是用來簡化HTTP通信的一個(gè)強(qiáng)大工具2024-01-01
visual studio 2019安裝配置可編寫c/c++語言的IDE環(huán)境
這篇文章主要介紹了visual studio 2019安裝配置可編寫c/c++語言的IDE環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
SpringBoot自動(dòng)初始化數(shù)據(jù)庫的方法分享
我們?cè)陧?xiàng)目中應(yīng)該經(jīng)常遇到過初始化數(shù)據(jù)的場(chǎng)景,特別是項(xiàng)目部署或者交付的時(shí)候,那么有什么方式可以在項(xiàng)目啟動(dòng)的時(shí)候自動(dòng)初始化數(shù)據(jù)庫呢,下面小編就來和大家分享幾個(gè)方法吧2023-08-08
Java基礎(chǔ)篇之serialVersionUID用法及注意事項(xiàng)詳解
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)篇之serialVersionUID用法及注意事項(xiàng)的相關(guān)資料,SerialVersionUID屬性是用于序列化/反序列化可序列化類的對(duì)象的標(biāo)識(shí)符,我們可以用它來記住可序列化類的版本,以驗(yàn)證加載的類和序列化對(duì)象是否兼容,需要的朋友可以參考下2024-02-02
Java并發(fā)編程之Semaphore(信號(hào)量)詳解及實(shí)例
這篇文章主要介紹了Java并發(fā)編程之Semaphore(信號(hào)量)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06

