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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM