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 配置是一致的
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