最近在做urle的规则引擎的整合,记录一下遇到的坑
客户端:
RuleConfig:
@Configuration
@ImportResource({"classpath:urule-core-context.xml"})
public class RuleConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourceLoader() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setOrder(1);
return configurer;
}
}
RuleController:
@RestController
public class RuleController {
@RequestMapping("rule")
public String rule(@RequestParam String data) throws IOException {
//创建一个KnowledgeSession对象
KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("aaa/bag");
KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
Integer integer = Integer.valueOf(data);
Map<String, Object> param = new HashMap();
param.put("var", integer);
session.fireRules(param);
Integer result = (Integer) session.getParameter("var");
return String.valueOf(result);
}
}
yml:
urule:
resporityServerUrl: http://localhost:9502
#0 -> 每次都从服务端拉取
#1 -> 缓存在本地
knowledgeUpdateCycle: 1
pom:
<dependency>
<groupId>com.bstek.urule</groupId>
<artifactId>urule-console</artifactId>
<version>2.1.7</version>
</dependency>
注:客户端一般没什么问题,但是我在配置的时候遇到jar包冲突的问题,是日志的jar包冲突,解决一下就好了
服务端:
dataSourceConfig:
@Configuration
@ImportResource({"classpath:urule-console-context.xml"})
@PropertySource(value = {"classpath:urule-console-context.properties"})
public class dataSourceConfig {
@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();
}
}
URuleServletRegistration:
@Component
public class URuleServletRegistration {
@Bean
public ServletRegistrationBean<HttpServlet> registerURuleServlet() {
return new ServletRegistrationBean<>(new URuleServlet(), "/urule/*");
}
}
yml:
urule:
repository:
# dir: d://测试
databasetype: mysql
datasourcename: datasource
这里要注意:spring的datasource属性的url要改成jdbc-url,不然会报错,感觉是因为datasource属性被引用后项目把他当作一个数据源了。
pom:
<dependency>
<groupId>com.bstek.urule</groupId>
<artifactId>urule-console</artifactId>
<version>2.1.7</version>
<exclusions>
<exclusion>
<artifactId>slf4j-jdk14</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
导入dbcp的jar包是因为配置类中需要dbcp的配置,导入io包的原因是在项目启动后,添加变量库保存文件时会报一个NoClassDefFoundError的错误,视情况而定