Flow ServerStarter
流程控制容器:lock

分布式锁

type distributedLock

在设计器中打开 ↗

抢到 Redis 锁后执行子链,退出时自动释放

使用说明

要点:抢到分布式锁后执行子链,退出时自动释放。宿主有 Redis / Redisson / ZooKeeper 时自动装配对应实现。
可选依赖该节点需要宿主额外引入 spring-data-redis(或 redisson / curator-recipes),否则部署相关流程会在 Camel 启动路由阶段失败并回滚。
容器节点:子链写在指定属性里,生成器与设计器会按容器结构展开。详见 Flow JSON 协议。

属性

字段类型必填默认值说明
key
锁 key
表达式 必填 — {lang: simple, script: order-${body}}
ttlMillis
锁过期(毫秒)
数字 可选 30000
waitMillis
最长等待(毫秒)
数字 可选 0
onFailure
抢锁失败时
枚举 可选 throw
可选值:throw、skip、continue
nodes
子链
子链 必填 —

示例流程

可直接保存为 demo-distributedLock.json 并部署;非入口节点默认套了一个 REST 入口。

Flow JSON
{
  "id": "demo-distributedLock",
  "desc": "分布式锁 示例",
  "group": "示例/流程控制",
  "schemaVersion": 1,
  "nodes": [
    {
      "id": "n1",
      "type": "rest",
      "props": {
        "method": "POST",
        "path": "/demo/distributedLock",
        "desc": "示例入口"
      }
    },
    {
      "type": "distributedLock",
      "props": {
        "key": {
          "lang": "simple",
          "script": "order-${header.orderId}"
        },
        "ttlMillis": 30000,
        "waitMillis": 2000,
        "onFailure": "skip",
        "nodes": [
          {
            "type": "log",
            "props": {
              "message": "拿到锁,开始处理订单 ${header.orderId}",
              "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-distributedLock.json

调用方式

调用示例
curl -X POST 'http://localhost:8086/demo/distributedLock' \
  -H 'Content-Type: application/json' \
  -d '{}'

调用时加 -H "orderId: 1001";Redis 实现会按 ttlMillis / 3 自动续期。

相关节点