codecamp

17.与SpringBoot集成

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目录。如果我们不想使用D:\repo目录来存储资源库,那么可以在运行命令后添加–urule.repository.dir参数来改变存储目录,如下所示:

java -jar urule-springboot-[version].jar --urule.repository.dir=C:\repo

上面的命令中,通过在命令后面添加–urule.repository.dir参数,将资源库存储目录修改为C盘下的repo目录。

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

启动好后,打开浏览器,输入http://localhost:8080,就可以看到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://github.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.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
 * @author Jacky.gao
 * @since 2016年10月12日
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource({"classpath:urule-console-context.xml"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }


    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        return tomcat;
    }
}

IndexServlet类是一个普通的Servlet,用来实现当用户输入根路径时可以直接跳到urule/frame这个页面,如果你不希望这样,那么就需要修改这个类。

URuleServletRegistration类中注册了两个Servlet,一个是IndexServlet,另一个就是URuleServlet,这个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/*");
    }
    @Bean
    public ServletRegistrationBean registerIndexServlet(){
        return new ServletRegistrationBean(new IndexServlet(),"/");
    }
}

最后一个类是PropertiesConfiguration,但这个类实际没用被用到,因此其类级别的@Component被注释掉了,所以这个类实际上是个示例类,用来说明如果我们需要覆盖URule Pro中默认参数该怎么做。比如上面的代码当中,如果我们需要采用数据库来存储资源库,那么就可以通过urule.repository.xml属性来指定一个数据库配置的xml。做好之后打开其类级别的@Component注释即可,这样spring boot在启动时就会加载这个类,从而实现将URule Pro中默认参数覆盖的目的。

package com.bstek.urule.springboot;
import java.util.Properties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import com.bstek.urule.URulePropertyPlaceholderConfigurer;
/**
 * @author Jacky.gao
 * @since 2016年10月12日
 */
//@Component
public class PropertiesConfiguration extends URulePropertyPlaceholderConfigurer implements InitializingBean{
    @Override
    public void afterPropertiesSet() throws Exception {
        Properties props=new Properties();
        props.setProperty("urule.repository.xml", "classpath:mysql.xml");   
        setProperties(props);
    }
}

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

16.客户端服务器配置
18.调试信息输出
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }