easy-rules spring boot starter 支持v4.0

最近几天easy-rules发布了4.0 变动还是挺多的(api,以及核心),对于原有spring boot starter 的一些修改
以支持4.0 ,以下是一个说明

参考代码地址

https://github.com/rongfengliang/easy-rules-spring-boot-starer

使用说明

具体的使用没有变动,只是新版本api 的一些变动(spel 变动有点大,但是还好,后边格式就统一了,升级只需要修改的主要是格式)

  • maven 引用
    repo
 
<repository>
    <id>ossrh</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>

引用

   <dependency>
    <groupId>com.github.rongfengliang</groupId>
    <artifactId>easy-rules-spring-boot-starter</artifactId>
    <version>2.0-SNAPSHOT</version>
</dependency>
  • spring boot 配置
easyrules:
  skipOnFirstAppliedRule: false
  skipOnFirstNonTriggeredRule: false
  priorityThreshold: 1000000
  rules:
    - rulesId: "userlogin"
      rulesLocation: "rules-json.json"
      contentType: JSON

rules-json
注意格式: conditon 以及action 我们不需要手工添加#{...} 了,模版已经处理了,这点使用以前的新版本的运行就会报错

 
[{
  "name": "1",
  "description": "1",
  "priority": 1,
  "compositeRuleType": "UnitRuleGroup",
  "composingRules": [
    {
      "name": "2",
      "description": "2",
      "condition": "#biz.age >18",
      "priority": 2,
      "actions": [
        "@myService.setInfo(#biz)",
        "T(com.dalong.easyrulesv4.UserServiceImpl).doAction4(#biz)"
      ]
    }
  ]}
]
  • 代码使用
@RestController
public class UserApi {
    @Autowired
    Map<String,Rules> configRules;
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public  Object info(@RequestBody User user) throws Exception {
        Rules rules = configRules.get("userlogin");
        Facts facts = new Facts();
        // 生成一个唯一id,方便基于数据id规则流程查询
        user.setUniqueId(UUID.randomUUID().toString());
        facts.put("biz",user);
        //  默认模式
        // myEngine.fire(rules,facts);
        // 应该使用原型模式
        SpringBeanUtil.getBean("rulesEngine",RulesEngine.class).fire(rules,facts);
        User userResult=  facts.get("biz");
        System.out.println("result from final ruls"+userResult.toString());
        return userResult;
    }
}

几个说明

新版的fact 很好用,是类型安全的,我们可以方便的进行类型处理,同时不用管rule 返回值了,同时代码也简洁了好多,
上一个版本,我专门还包装了一个FinalRule处理最后的结果,新版的已经不需要了,整体来说升级还是比较方便的,改动
很少,很快就可以运行起来了,后边还是赶紧搞prometheus metrics 的集成吧

参考资料

https://github.com/rongfengliang/easy-rules-spring-boot-starer
https://github.com/j-easy/easy-rules

posted on 2020-05-29 21:16  荣锋亮  阅读(710)  评论(0编辑  收藏  举报

导航