17.与SpringBoot集成

使用

URule Pro规则引擎与spring boot集成好的项目我们可以到 https://oss.sonatype.org 上下载,打开 https://oss.sonatype.org, 搜索关键字“urule-springboot”下载最新的urule-springboot包即可。

下载好urule-springboot的jar包,将这个jar放在一个非中文目录下,在我们系统的D盘下创建一个名为repo目录(用于存储规则相关文件),打开操作系统命令行,切换到urule-springboot的jar包所在目录,输入如下命令即可运行这个spring boot项目:

java -jar urule-springboot-[version].jar

通过之前的章节介绍我们知道,URule Pro控制台在运行时需要指定资源库存储的目录路径或存储到数据库时的配置文件,对于这里提供的urule-springboot包来说,默认情况下, 它采用的是D盘的repo目录下存储资源库,所以我们需要创建在启动urule-springboot包前需要在D盘创建好repo目录。

以上命令要求我们的系统当中已安装好1.8或以上版本的JDK,同时也已配置好JDK环境参数,否则执行上述命令将会报找到不命令关键字的错误。

启动好后,打开浏览器,输入 http://localhost:8080/urule/frame,就可以看到URule Pro提供的控制台页面。

URule Pro提供的spring boot集成包,采用的是Tomcat,默认采用的是8080端口号,如果需要更改,那么可以在启动命令后添加--server.port参数, 比如--server.port=80,这就表示采用80端口,启动后,直接浏览http://localhost就可以看到URule控制台,就不需要再输入端口号了,更多启动参数可以到spring boot官网上查看。

二次开发

默认提供的URule Pro与spring boot集成的版本功能相对简单,如果您需要URule Pro与spring boot集成的版本能实现在数据库中存储资源库、为URule控制台添加安全限制等,同时您又熟悉spring boot, 那么可以到https://gitee.com/youseries/urule/tree/master/urule-springboot上下载URule Pro与spring boot集成的项目源码做二次开发即可。

这个项目比较简单,除了与spring boot相关的一些配置外,在com.bstek.urule.springboot包中只有两个类。 Application类是spring boot启动的入口类,在这个类中,我们通过ImportResource这个annotation加载了URule Console Pro的spring配置文件classpath:urule-console-context.xml,如下源码所示:

package com.bstek.urule.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
 * @author Jacky.gao
 * @since 2016年10月12日
 */
@SpringBootApplication
@ImportResource({"classpath:context.xml"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

这里加载的位于classpath下的context.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    <import resource="classpath:urule-console-context.xml"/>
    <context:property-placeholder ignore-unresolvable="true" order="2" location="classpath:configure.properties"/>
</beans>

从这个spring的xml配置文件中可以看到,里面首先通过import标记加载URule Pro的Spring配置文件,然后加载位于classpath下的名为configure.properties的属性文件,我们如果需要配置其它的URule Pro中提供的属性, 只需要打开这个configure.properties文件,在其中添加相应的属性即可。

URuleServletRegistration类中注册了URuleServlet这个Servlet,这个Servlet是URule Console Pro入口,所以必须要注册,URuleServletRegistration类源码如下:

package com.bstek.urule.springboot;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.bstek.urule.console.servlet.URuleServlet;
/**
 * @author Jacky.gao
 * @since 2016年10月12日
 */
@Component
public class URuleServletRegistration {
    @Bean
    public ServletRegistrationBean registerURuleServlet(){
        return new ServletRegistrationBean(new URuleServlet(),"/urule/*");
    }
}

通过这个项目,我们可以了解到URule Pro与spring boot的集成方式,实际使用时可以直接基于此项目源码进行修改,也可以按照此项目的配置方式将URule Pro添加到一个正在使用的spring boot项目当中。

results matching ""

    No results matching ""