Springboot整合Urule的方法步骤

摘要:

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,开源版本可到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、配置文件:两个,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项目配置

参数库

规则

知识包及发布

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

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

success!

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

(0)

相关推荐

  • spring boot整合CAS配置详解

    在下不才,以下是我花了好几天的时间才整合出来的在spring boot里面的CAS配置整合 为了帮助没搞定的人,毕竟自己踩了很多坑,一步一步爬过来的,有什么不足之处可以给建议  谢谢(小部分代码是整合他人的) 1.不多废话,直接上最重要的代码,以下代码整合cas的重要过程 import org.jasig.cas.client.authentication.AuthenticationFilter; import org.jasig.cas.client.session.SingleSignOu

  • springboot与mybatis整合实例详解(完美融合)

    简介 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目.它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用.Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合.大部分 Spring Boot 应用只需要非常少的配置就可以快速运行起来. Spring Boot 包含的特性如下: 创建可以独立运行的 Spring 应用. 直接嵌入 Tomc

  • spring boot整合Shiro实现单点登录的示例代码

    Shiro是什么 Shiro是一个Java平台的开源权限框架,用于认证和访问授权.具体来说,满足对如下元素的支持: 用户,角色,权限(仅仅是操作权限,数据权限必须与业务需求紧密结合),资源(url). 用户分配角色,角色定义权限. 访问授权时支持角色或者权限,并且支持多级的权限定义. Q:对组的支持? A:shiro默认不支持对组设置权限. Q:是否可以满足对组进行角色分配的需求? A:扩展Realm,可以支持对组进行分配角色,其实就是给该组下的所有用户分配权限. Q:对数据权限的支持? 在业务

  • 详解Spring Boot整合Mybatis实现 Druid多数据源配置

    一.多数据源的应用场景 目前,业界流行的数据操作框架是 Mybatis,那 Druid 是什么呢? Druid 是 Java 的数据库连接池组件.Druid 能够提供强大的监控和扩展功能.比如可以监控 SQL ,在监控业务可以查询慢查询 SQL 列表等.Druid 核心主要包括三部分: 1. DruidDriver 代理 Driver,能够提供基于 Filter-Chain 模式的插件体系. 2. DruidDataSource 高效可管理的数据库连接池 3. SQLParser 当业务数据量达

  • springboot+springmvc+mybatis项目整合

    介绍: 上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生,Pivotal团队提供了一款全新的框架,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者. 特点: 1. 创建独立的Spring应用

  • springboot整合Quartz实现动态配置定时任务的方法

    前言 在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能. 一.新建一个springboot工程,并添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency

  • 浅谈Springboot整合RocketMQ使用心得

    一.阿里云官网---帮助文档 https://help.aliyun.com/document_detail/29536.html?spm=5176.doc29535.6.555.WWTIUh 按照官网步骤,创建Topic.申请发布(生产者).申请订阅(消费者) 二.代码 1.配置: public class MqConfig { /** * 启动测试之前请替换如下 XXX 为您的配置 */ public static final String PUBLIC_TOPIC = "test"

  • springboot整合freemarker详解

    前提: 开发工具:idea 框架:spring boot.maven 1.pom文件添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>1.4.1.RELEASE</version> </dependency>

  • springboot整合redis进行数据操作(推荐)

    redis是一种常见的nosql,日常开发中,我们使用它的频率比较高,因为它的多种数据接口,很多场景中我们都可以用到,并且redis对分布式这块做的非常好. springboot整合redis比较简单,并且使用redistemplate可以让我们更加方便的对数据进行操作. 1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starte

  • Springboot整合Urule的方法步骤

    摘要: Urule决策引擎可简化开发校验.决策类代码,底层由java语言实现,可基于SpringBoot快速配置,因为Urule工具目前为非常用工具,网上关于SpringBoot整合Urule资料匮乏,一直自己摸索,简单的环境搭建也费了些功夫,遇到些坑,作此记录 本次记录主要记录Urule-Serve端Urule-Client端分开部署的模式,这种使用场景也会更多:嵌入式成一个项目的配置和Urule-Server端一致. 一.Urule-Server端: 1.1. 基于maven的SpringB

  • Springboot整合activemq的方法步骤

    今天呢心血来潮,也有很多以前的学弟问到我关于消息队列的一些问题,有个刚入门,有的有问题都来问我,那么今天来说说如何快速入门mq. 一.首先说下什么是消息队列? 1.消息队列是在消息的传输过程中保存消息的容器. 二.为什么要用到消息队列? 主要原因是由于在高并发环境下,由于来不及同步处理,请求往往会发生堵塞,比如说,大量的insert,update之类的请求同时到达 MySQL ,直接导致无数的行锁表锁,甚至最后请求会堆积过多,从而触发too many connections错误.通过使用消息队列

  • SpringBoot整合之SpringBoot整合MongoDB的详细步骤

    目录 一.创建项目,选择依赖 二.引入相关依赖(非必要) 三.如果是第一次使用MongoDB,首先先创建用户 四.定义核心配置文件 六.创建dao层,这里的dao层有两种写法 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.本文介绍SpringBoot整合之SpringBoot整合MongoDB的步骤. 一

  • SpringBoot整合Ehcache3的实现步骤

    目录 前言 缓存配置 maven引用 个性化配置 代码注入配置 缓存操作 缓存预热 更新操作 查询操作 缓存与数据库数据一致性 前言 公司部门老项目要迁移升级java版本,需要进行缓存相关操作,原框架未支持这部分,经过调研java相关缓存方案大致分为ehcache和redis两种,redis的value最大值为500mb且超过1mb会对存取有性能影响,业务系统需要支持列表查询缓存就不可避免的涉及到大量的数据存取过滤,ehcache支持内存+磁盘缓存不用担心缓存容量问题,所以框架初步版本决定集成e

  • SpringBoot整合Thymeleaf的方法

    简介: 在目前的企业级应用开发中 前后端分离是趋势,但是视图层技术还占有一席之地, Spring Boot 对视图层技术提供了很好的支持,官方推荐使用的模板引擎是 Thymeleaf 不过像 FreeMarker 也支持, JSP 技术在这里并不推荐使用. Thymeleaf 是新一代 Java 模板引擎,类似于 Velocity.FreeMarker 等传统 Java 模板引擎.与传统 Java 模板引擎不同的是 Thymeleaf 支持 HTML 原型,既可 以让前端工程师在浏览器中直接打

  • SpringBoot整合Flyway的方法(数据库版本迁移工具)

    Flyway是什么 Flyway是一款开源的数据库版本管理工具,Flyway可以独立于应用实现管理并跟踪数据库的变更,Flyway根据自己的约定,不需要复杂的配置就可以实现数据的Migrate.Migrations可以写成SQL脚本,也可以写在Java代码中,Flyway还支持Spring Boot. 简介 在团队开发当中,有可能每个人都是使用自己本地的数据库.当数据库的表或者字段更新时,往往需要告知团队的其他同事进行更新. Flyway数据库版本迁移工具,目的就是解决该问题而诞生的(我自己想的

  • SpringBoot整合spring-data-jpa的方法

    jpa是JavaEE定义的一种规范,常用的实现一般是Hibernate,而spring-data-jpa则是对jpa的又一层封装,提供了更多便捷的方法. 这里不会深入讲解spring-data-jpa的使用,只是讲解怎么快速的整合使用,目的是帮助那些想学,但是在整合上老是翻车的同学 导入依赖 <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> &

  • 使用sts工具、SpringBoot整合mybatis的详细步骤

    SpringBoot 集成 Mybatis 框架 一.1.SpringBoot 集成 Mybatis 的基本步骤 第一步:添加依赖: 第二步:配置数据源: 第三步:扫描接口包. 二.详细的集成步骤如下: 1.第一步:添加依赖: 添加依赖:除了常规依赖外,需要加入 Mybatis 代码如下(示例): <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XM

  • springBoot整合rabbitMQ的方法详解

    引入pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0

  • SpringBoot整合Drools的实现步骤

    Drools有什么用 从我个人所待过的公司,其中做智能酒店这个项目时就用到规则引擎Drools,将它用于处理优惠劵规则. SpringBoot整合Drools初步实战 1.导入Maven依赖 <properties> <drools.version>7.14.0.Final</drools.version> </properties> <!-- drools --> <dependency> <groupId>org.dr

随机推荐