当前位置: 首页 > 工具软件 > URule > 使用案例 >

urule客户端和服务器配置

扈韬
2023-12-01

urule服务端配置(包含数据库的配置):

pom文件:

<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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bstek.urule</groupId>
	<artifactId>urule-springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<java.version>1.7</java.version>
		<tomcat.version>8.5.5</tomcat.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<licenses>
		<license>
			<name>The Apache License, Version 2.0</name>
			<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
		</license>
	</licenses>
	<developers>
		<developer>
			<name>Gaojie</name>
			<email>jacky.gao@bstek.com</email>
			<organization>Bstek</organization>
			<organizationUrl>http://www.bstek.com</organizationUrl>
		</developer>
	</developers>
	<scm>
		<connection>https://github.com/youseries/urule.git</connection>
		<developerConnection>https://github.com/youseries/urule.git</developerConnection>
		<url>https://github.com/youseries/urule</url>
	</scm>
	<organization>
		<name>Bstek</name>
		<url>http://www.bstek.com</url>
	</organization>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath />
	</parent>
	<dependencies>
		<dependency>
			<groupId>com.ecms</groupId>
			<artifactId>cbc.account.urule</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.10</version>
		</dependency>
		<dependency>
			<groupId>com.bstek.urule</groupId>
			<artifactId>urule-console-pro</artifactId>
			<version>2.1.3</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-jdk14</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.bstek.urule</groupId>
					<artifactId>urule-core-pro</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<distributionManagement>
		<snapshotRepository>
			<id>ossrh</id>
			<url>https://oss.sonatype.org/content/repositories/snapshots</url>
		</snapshotRepository>
		<repository>
			<id>ossrh</id>
			<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
		</repository>
	</distributionManagement>

	<name>Urule Springboot Project</name>
	<url>https://github.com/youseries/urule/tree/master/urule-springboot</url>
	<issueManagement>
		<url>https://github.com/youseries/urule/issues</url>
	</issueManagement>
	<description>Urule Springboot Project</description>
</project>

urule-console-datasource.properties:

spring.datasource.name=mydatasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.103.11:3306/cbc?characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true
spring.datasource.username=cbc
spring.datasource.password=cbc@20180907

spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,slf4j

application.properties:

server.port=7888
#urule.repository.dir=d:/repo
urule.repository.datasourcename=mydatasource
urule.repository.databasetype=mysql

 config文件:

package com.bstek.urule.springboot;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import javax.sql.DataSource;

@Configuration
@ImportResource({"classpath:urule-console-context.xml"})
@PropertySource(value = {"classpath:urule-console-datasource.properties"})
public class Config {
    /**
     * PropertySourcesPlaceholderConfigurer:外部属性配置文件,可以替换xml中的属性配置,实现部分私密配置外部化,类似数据库配置
     * @return
     */
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourceLoader(){
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        //该属性为true,将会隐藏在占位符变量无法解析或者属性文件不存在是抛出的异常
        configurer.setIgnoreUnresolvablePlaceholders(true);
        configurer.setOrder(1);
        return configurer;
    }

    @Bean(name = "mydatasource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
    }
}

 对于urule需要用到的动作库,可以另外定义一个jar包,在服务端和客户端分别依赖该jar包,这样在客户端和服务端中都可以不在依赖urule的jar包。

indexServlet:

package com.bstek.urule.springboot;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jacky.gao
 * @since 2016年10月12日
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 9155627652423910928L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.sendRedirect(req.getContextPath()+"/urule/frame");
	}
}

UruleServletRegistration:

package com.bstek.urule.springboot;

import javax.servlet.http.HttpServlet;

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 {
	/**
	 * springboot自带的支持servlet的注册方法,接收两个参数,第一个是自定义的servlet,第二个是对符合路径的访问跳转到第一个配置的servlet上
	 * @return
	 */
	@Bean
	public ServletRegistrationBean<HttpServlet> registerURuleServlet(){
		return new ServletRegistrationBean<HttpServlet>(new URuleServlet(),"/urule/*");
	}
	@Bean
	public ServletRegistrationBean<HttpServlet> registerIndexServlet(){
		return new ServletRegistrationBean<HttpServlet>(new IndexServlet(),"/");
	}
	
}

 活动库项目(actionRepository):

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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ecms</groupId>
    <artifactId>cbc.account.urule</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>5.0.8.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.ecms</groupId>
            <artifactId>cbc-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.44</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>
        <dependency>
            <groupId>com.bstek.urule</groupId>
            <artifactId>urule-core-pro</artifactId>
            <version>2.1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-jdk14</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

活动库项目为简单的java项目,只需要定义具体的方法即可,方法的写法可以参考官方网站的说明。另外urule中也可以定义通用的工具类。

客户端:

客户端pom文件: 

<dependency>
            <groupId>com.ecms</groupId>
            <artifactId>cbc.account.urule</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
<dependency>
            <groupId>com.bstek.urule</groupId>
            <artifactId>urule-core-pro</artifactId>
            <version>2.1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-jdk14</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

关于urule的包只需要引入core-pro和自己定义的活动库项目的jar包,core-pro中因为引入了日志和web模块,但是我的项目中自己定义了需要的相关模块,所有启动报错,同理,对不同的项目,如果产生了jar包冲突,可以自行排除。

urule.properties:

urule.knowledgeUpdateCycle=1
urule.resporityServerUrl=http://localhost:7888

里面只需要指定服务端的url和知识库的拉取策略。

UruleConfig:

package executor.config;

import com.bstek.urule.KnowledgePackageReceiverServlet;
import com.bstek.urule.URulePropertyPlaceholderConfigurer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.ClassPathResource;

@Configuration
@ImportResource(locations = {"classpath:urule-core-context.xml"})
public class UruleConfig {

    @Bean
    public ServletRegistrationBean<KnowledgePackageReceiverServlet> servletRegistrationBean(){
        return new ServletRegistrationBean<KnowledgePackageReceiverServlet>(new KnowledgePackageReceiverServlet(),"/knowledgepackagereceiver");
    }

    @Bean
    public URulePropertyPlaceholderConfigurer uRuleConfig (){
        URulePropertyPlaceholderConfigurer config = new URulePropertyPlaceholderConfigurer();
        config.setLocation(new ClassPathResource("urule.properties"));
        return config;
    }

}

最后一步,在springboot的启动类中引入对自定义动作库的扫描即可。以上即为springboot整合urule的全部工作。

 

 

 

 

 

 

 类似资料: