Drools是一个基于java的规则引擎,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启服务就可以在线上环境生效。
3.1.新建SpringBoot工程,导入以下maven依赖
<dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-spring</artifactId> <version>7.6.0.Final</version> </dependency>3.2.编写规则引擎配置类
@Configuration public class RuleEngineConfig { private static final Logger logger = LoggerFactory.getLogger(RuleEngineConfig.class); private static final String RULES_PATH="droolsRule/"; //规则文件路径 private final KieServices kieServices = KieServices.Factory.get(); @Bean public KieFileSystem kieFileSystem() throws IOException { KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] files = resourcePatternResolver.getResources(RULES_PATH + "*.*"); String path = null; for (Resource file : files) { path = RULES_PATH + file.getFilename(); logger.info("path="+path); kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8")); } return kieFileSystem; } @Bean public KieContainer kieContainer() throws IOException { KieRepository kieRepository = kieServices.getRepository(); kieRepository.addKieModule(kieRepository::getDefaultReleaseId); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem()); kieBuilder.buildAll(); return kieServices.newKieContainer(kieRepository.getDefaultReleaseId()); } @Bean public KieBase kieBase() throws IOException { return kieContainer().getKieBase(); } @Bean public KieSession kieSession() throws IOException { return kieContainer().newKieSession(); } @Bean public KModuleBeanFactoryPostProcessor kiePostProcessor() { return new KModuleBeanFactoryPostProcessor(); } }3.3.编写一个简单规则,举例:将年龄大于20的用户信息输出
package droolsRule; //对应3.2配置类中的规则路径,规则文件放于文件夹a下,这里就写package a import com.duang.droolsdemo.entity.Customer rule "paramCheckNotNull" salience 100 // 规则优先级,值越大优先级越高 when $customer:Customer(age >= 20) then System.out.println($customer.toString()); end3.4.编写测试类
@SpringBootTest class DroolsdemoApplicationTests { @Resource private KieSession kieSession; @Test void contextLoads() { } @Test public void testDrools(){ Customer customer = new Customer(); customer.setName("Alice"); customer.setAge(20); customer.setTime(new Timestamp(System.currentTimeMillis())); //调用规则引擎验证数据 kieSession.insert(customer); kieSession.fireAllRules(); } }刚开始接触规则引擎的时候有个疑问,像上面的例子直接使用if-else不是更简单快捷么?后来使用过程中发现了有一个好处就是如果对规则文件进行修改无需重新编译程序即可生效甚至你还可以将规则存于数据库更灵活的修改规则。总而言之,规则引擎的水很深,我也才刚接触它,写篇博客分享心得也给以后做个留念。写的很仓促,疏漏错误之处还请大家多多指教,加油