欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot整合Urule的方法步骤

程序员文章站 2023-11-17 11:23:04
摘要: urule决策引擎可简化开发校验、决策类代码,底层由java语言实现,可基于springboot快速配置,因为urule工具目前为非常用工具,网上关于spring...

摘要:

urule决策引擎可简化开发校验、决策类代码,底层由java语言实现,可基于springboot快速配置,因为urule工具目前为非常用工具,网上关于springboot整合urule资料匮乏,一直自己摸索,简单的环境搭建也费了些功夫,遇到些坑,作此记录

本次记录主要记录urule-serve端urule-client端分开部署的模式,这种使用场景也会更多;嵌入式成一个项目的配置和urule-server端一致。

一、urule-server端:

1.1、 基于maven的springboot基本环境搭建请参考springboot教程

1.2、引入urule相关依赖,urule-console-pro,开源版本可到

中心搜索,依赖如下:

<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、配置文件:两个,appplication.yml   ,    application.properties

appplication.yml,配置数据库信息(我们把urule项目存到数据库中)

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数据源
 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。根据springboot版本自行调整

application.properties,配置项目储存位置

#若为本地环境需配置此路径
#urule.repository.dir=f:/eclipsepractice/03_springcloud/repo4rule
#若为数据库,配置此项,两项均不配则系统指定默认地址
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、启动类:

@springbootapplication
@importresource({"classpath:urule-console-context.xml"})
public class application
{
 public static void main(string[] args)
 {
 springapplication.run(application.class, args);
 }
}

二、客户端调用:

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数据源
 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:
 ###服务端发现地址
 resporityserverurl: http://localhost:8081
 ###knowledgeupdatecycle为0时,不是检查缓存,每次都从服务端拉取,为1时,会先查找缓存
 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服务端发布的知识包,使用开源版本时删除或者注释这个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);
//参数,urule项目名/知识包名
   knowledgepackage knowledgepackage = knowledgeservice.getknowledge("letasa/pare");
   knowledgesession session = knowledgesessionfactory.newknowledgesession(knowledgepackage);
   integer integer = integer.valueof(data);
   map<string, object> param = new hashmap();
//参数,var,传入参数,和参数库中定义一致
   param.put("var", integer);
   session.firerules(param);
//result,返回参数,和参数库中定义一致
   integer result = (integer) session.getparameter("result");
   return string.valueof(result);
 }
}

2.4、启动类

@springbootapplication
@importresource({"classpath:urule-core-context.xml"})
public class application {
 public static void main(string[] args) {
  springapplication.run(application.class, args);
 }
}

urule项目配置

Springboot整合Urule的方法步骤

参数库

Springboot整合Urule的方法步骤

规则

Springboot整合Urule的方法步骤

知识包及发布

注:rrule-pro版本支持将知识包推送给具体客户端,客户端使用时先调用缓存,如无缓存则再到服务端拉去。但开源版本的urule不支持推送,客户端只能主动到服务端拉去数据。

最后访问客户端:http://localhost:8090/rule?data=67,或者data=25,分别得到100,20.

success!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。