event-ruler 目前event-ruler 似乎还没有发布 maven 仓库,可以自己构建
构建命令
mvn clean package install -Dmaven.test.skip
参考使用
package com.dalong;
import software.amazon.event.ruler.Machine;
import software.amazon.event.ruler.RuleCompiler;
import software.amazon.event.ruler.Ruler;
import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
String rule1 = "{ \"a\" : [ { \"prefix\": \"zoo\" } ] }";
String rule2 = "{ \"b\" : [ { \"prefix\": \"child\" } ] }";
// use Machine
Machine machine = new Machine();
machine.addRule("r1", rule1);
machine.addRule("r2", rule2);
String[][] events = {
{"a", "\"zookeeper\""},
{"a", "\"zoo\""},
{"b", "\"childlike\""},
{"b", "\"childish\""},
{"b", "\"childhood\""}
};
for (String[] event : events) {
List<String> rules = machine.rulesForEvent(event);
System.out.println(rules.toString());
}
// use Ruler static function
Boolean result = Ruler.matchesRule("{\"a\": \"zookeeper\"}", "{ \"a\" : [{ \"prefix\": \"zoo\" }]}");
System.out.println(result);
// compiler
String compileResult = RuleCompiler.check("{ \"a\" : [{ \"prefix\": \"zoo\" }]}");
System.out.println(compileResult);
}
}
说明
实际上对于基于json 的处理,json schema,都是不错的选择,但是很多时候我们需要的规则可能比较复杂,同时也需要一个不错的性能,基于event-ruler 是一个不错的
选择,比如我们可以集成此rule 在gateway 中,实现一个灵活的业务校验以及数据路由