流程控制容器:idempotentConsumer
幂等消费
type idempotentConsumer
按幂等键去重,同一消息只执行一次子链
使用说明
要点:按幂等键去重,同一 key 只执行一次子链。默认使用进程内 MemoryIdempotentRepository,重启后记录丢失。
容器节点:子链写在指定属性里,生成器与设计器会按容器结构展开。详见 Flow JSON 协议。
属性
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
expression幂等键 |
表达式 | 必填 | — | {lang: header, script: id} |
repository仓库 Bean |
文本 | 可选 | — | 缺省为内存仓库 |
removeOnFailure失败时移除 key |
布尔 | 可选 | false |
|
skipDuplicate跳过重复消息 |
布尔 | 可选 | true |
|
nodes子链 |
子链 | 必填 | — |
示例流程
可直接保存为 demo-idempotentConsumer.json 并部署;非入口节点默认套了一个 REST 入口。
{
"id": "demo-idempotentConsumer",
"desc": "幂等消费 示例",
"group": "示例/流程控制",
"schemaVersion": 1,
"nodes": [
{
"id": "n1",
"type": "rest",
"props": {
"method": "POST",
"path": "/demo/idempotentConsumer",
"desc": "示例入口"
}
},
{
"type": "idempotentConsumer",
"props": {
"expression": {
"lang": "header",
"script": "messageId"
},
"repository": "",
"removeOnFailure": false,
"skipDuplicate": true,
"nodes": [
{
"type": "log",
"props": {
"message": "首次处理消息 ${header.messageId}",
"level": "INFO"
},
"id": "n2"
}
]
},
"id": "n1"
}
]
}
curl -X POST 'http://localhost:8086/flow' \
-H 'Content-Type: application/json' \
-H 'X-Flow-Token: change-me' \
--data-binary @demo-idempotentConsumer.json
调用方式
curl -X POST 'http://localhost:8086/demo/idempotentConsumer' \
-H 'Content-Type: application/json' \
-d '{}'
调用时加 -H "messageId: msg-001",重复调用只执行第一次。