easy-rules-centraldogma-spring-boot-starter 使用說明
easy-rules-centraldogma-spring-boot-starter 是直接利用了centraldogma進行easy-rules 配置規則管理
可以方便的多版本以及實時更新問題,利用centraldogma強大的git 能力,可以方便的進行rule 的版本管
理,同時centraldogma還支援一種方便的映象能力,可以方便的進行gitrepo-> centraldogma 的定時同步
同時實現按需的近實時規則更新,而且centraldogma的ha 以及整合還是很方便的,以下是基於centraldogma
的easy-rules spring boot starer 使用
環境準備
- centraldogma 環境
version: "3"
services:
app:
image: line/centraldogma
ports:
- "36462:36462"
注意需要初始建立demo 專案,以及demo repo,同時新增easy-rules 的一個規則配置,後邊有說明
- 引入starter
需要自己構建,還沒有釋出私服,參考連結
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalong</groupId>
<artifactId>batchapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>batchapp</name>
<description>mybatch app</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.rongfengliang</groupId>
<artifactId>easy-rules-centraldogma-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.linecorp.centraldogma</groupId>
<artifactId>centraldogma-client-spring-boot-starter</artifactId>
<version>0.51.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
</project>
配置
application.yml
easyrules:
skipOnFirstAppliedRule: false
skipOnFirstNonTriggeredRule: false
priorityThreshold: 1000000
project: demo
repo: demo
contentType: json
confName: /rules2.json
centraldogma:
hosts:
- "127.0.0.1:36462" // 如需認證的,可以按照參考連結配置
server:
port: 9000
centraldogma demo repo /rules2.json 檔案內容(rulesId 是結合業務使用的規則使用的spel 格式)
[
{
"rulesId": "demoapp",
"rulesContent": [
{
"name": "1",
"description": "1ssssss",
"priority": 1,
"compositeRuleType": "UnitRuleGroup",
"composingRules": [
{
"name": "2",
"description": "2",
"condition": "#biz.age >18", // fact 資料age > 18 會進行action
"priority": 2,
"actions": [
"@myService.setInfo(#biz)" // 具體action 會age 的資料重新賦值
]
}
]
}
]
},
{
"rulesId": "demoapp222",
"rulesContent": [
{
"name": "1",
"description": "1ssssss",
"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)"
]
}
]
}
]
}
]
程式碼整合(一個簡單的rest api)
@RestController
public class RuleApi {
@RequestMapping(value = "/myrule", method = RequestMethod.POST)
public Object info(@RequestBody User user) throws Exception {
SpringBeanUtil.centralDogmaRules().forEach(new BiConsumer<String, Rules>() {
@Override
public void accept(String s, Rules rules) {
System.out.println(s);
rules.forEach(new Consumer<Rule>() {
@Override
public void accept(Rule rule) {
System.out.println(rule.getDescription());
}
});
}
});
Rules rules = SpringBeanUtil.centralDogmaRules().get("demoapp");
Facts facts = new Facts();
// 生成一個唯一id,方便基於資料id規則流程查詢
user.setUniqueId(UUID.randomUUID().toString());
facts.put("biz",user);
SpringBeanUtil.getBean("rulesEngine", RulesEngine.class).fire(rules,facts);
User userResult= facts.get("biz");
System.out.println("result from final ruls"+userResult.toString());
return userResult;
}
}
規則依賴的spring bean
@Component("myService")
public class MyService {
public User setInfo(User biz){
System.out.println("call bean method");
System.out.println(biz.toString());
biz.setAge(33333);
return biz;
}
}
執行效果
easy-rules-centraldogma-spring-boot-starter 相關配置引數說明
具體的參考github 實際上說明了,以下簡單說明下
配置引數是也easy-rules engine 配置是一致的
<wiz_code_mirror>easyrules:
skipOnFirstAppliedRule: false
skipOnFirstNonTriggeredRule: false
priorityThreshold: 1000000
project: demo
repo: demo
contentType: json // 當前只支援json格式的,後續擴充套件其他的
confName: /rules2.json
centraldogma:
hosts:
- "127.0.0.1:36462"
server:
port: 9000
呼叫說明
因為easy-rules 的特性,facts不是執行緒安全的,而且ruleengine 也不是的,所以需要使用原型bean 進行資料處理,所以
包裝了一個通用的utils 可以參考上邊rest api 部分的程式碼,同時關於centraldogma安全以及ha 部分可以參考官方文件,或
者我寫的文章
參考資料
https://line.github.io/centraldogma/
https://www.cnblogs.com/rongfengliang/p/15168860.html
https://www.cnblogs.com/rongfengliang/p/15135247.html
https://github.com/rongfengliang/easy-rules-centraldogma-spring-boot-starter