easyrules 例子 从 nacos里读取规则
所属分类 easyrules
浏览量 189
org.jeasy:easy-rules-core:4.1.0
org.jeasy:easy-rules-support:4.1.0
org.jeasy:easy-rules-mvel:4.1.0
com.alibaba.nacos:nacos-client:2.2.0
test_rule_001
yaml 规则
name: "alcohol rule"
description: "children are not allowed to buy alcohol"
priority: 1
condition: "person.age < 18"
actions:
- "System.out.println(\"Shop: Sorry, you are not allowed to buy alcohol\");"
public class Person {
private String name;
private int age;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.jeasy.rules.support.reader.YamlRuleDefinitionReader;
import java.io.StringReader;
import java.util.Properties;
public class RuleDemo {
public static void main(String[] args) throws Exception {
// 从 nacos 读取规则
Properties properties = new Properties();
properties.setProperty("serverAddr", "127.0.0.1:8848");
ConfigService configService = NacosFactory.createConfigService(properties);
String dataId = "test_rule_001";
String group = "DEFAULT_GROUP";
long timeoutMs = 3000;
String rule = configService.getConfig(dataId,group,timeoutMs);
System.out.println(rule);
System.out.println("---------------");
// 创建规则 mvel 脚本
MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
Rule alcoholRule = ruleFactory.createRule(new StringReader(rule));
// create a rule set
Rules rules = new Rules();
rules.register(alcoholRule);
//create a default rules engine and fire rules on known facts
RulesEngine rulesEngine = new DefaultRulesEngine();
//create a person instance (fact)
Person tom = new Person("Tom", 15);
Facts facts = new Facts();
facts.put("person", tom);
System.out.println("Tom: Hi! can I have some Vodka please?");
// 运行规则引擎
rulesEngine.fire(rules, facts);
}
}
nacos 配置安装
nacos Java客户端使用
https://gitee.com/dyyx/work2024/tree/master/demo/easyrules-nacos-demo
上一篇
下一篇
grafana 图表 变量
easyrules 简介
easyrules 例子 基于注解
统计学术语
apache math库计算 z值表
正态分布简介