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   荣锋亮  阅读(752)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-05-29 使用terraform 生成自签名证书

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示