如何用 LRA coordinator 与 Camel 4 的 LRA 组件交互

解读

面试官抛出这道题,核心想验证三件事:

  1. 你是否真的在 Camel 4 里亲手用过 LRA(Long Running Action),而不是停留在“听过”层面;
  2. 能否把Narayana、MicroProfile LRA、Camel LRA 组件三条线串起来,讲清楚“谁负责协调、谁负责补偿、谁负责暴露接口”;
  3. 对国内金融、电信等强一致性场景下的坑(双活机房、网络闪断、事务悬挂)有没有踩过,能否给出可落地的降级方案。
    答得太浅会被当成“背概念”,答得太深又容易“过度设计”,所以要把**“Camel 4 的 LRA 组件如何与外部 LRA coordinator 互通”**拆成“配置-编码-运维”三步,既体现细节,又保持节奏。

知识点

  1. MicroProfile LRA 规范:定义了 @LRA、@Compensate、@Complete、@Status、@Forget 五类注解, coordinator 通过 HTTP 回调参与者。
  2. Camel 4 LRA 组件(camel-lra):内部封装了上述注解,把“开始 LRA”、“加入 LRA”、“补偿”全部映射成 EIP 节点,路由即参与者。
  3. Narayana LRA Coordinator:国内落地最多,支持MySQL/PostgreSQL 持久化事务日志,提供 /lra-coordinator 根路径的 REST API;在 K8s 环境通常以 StatefulSet + headless service 部署,通过 Ingress-NGINX 暴露 https 端口。
  4. Camel 4 新特性:
    • 全面切换到 Jakarta EE 9+ 包名,老 javax.ws.rs 全部换成 jakarta.ws.rs;
    • LRA 组件默认用 Vert.x HTTP Client,连接池参数 maxPoolSize 与 keepAliveTimeout 可独立调优;
    • 支持 YAML 路由,国内习惯用 Nacos 配置中心下发,热更新时 LRA 路由会自动重新注册到 coordinator,无需重启 Pod。
  5. 国内常见坑:
    • 双活机房场景下 coordinator 的 public URL 必须带机房后缀,否则参与者回调会跨机房漂移,导致补偿失败;
    • 事务超时时间 lra.timelimit 如果大于 F5 默认 300 s 空闲断开,会被防火墙 RST,需要显式在 Ingress 加 proxy-read-timeout 3600s;
    • 金融合规要求 事务日志保存 15 年以上,Narayana 的 ObjectStore 必须挂载 CephFS 只读快照,定期转冷存。

答案

第一步:让 Camel 4 能找到 coordinator
在 application.properties 里给出 绝对可达的公网地址,切忌用 K8s 内部 service 名:

camel.lra.coordinator-url=https://lra-bj.narayana.mybank.com/lra-coordinator
camel.lra.local-participant-url=https://trade-service.mybank.com/camel/lra
camel.lra.timelimit=600S

注意:local-participant-url 是 Camel 自身暴露的回调地址,必须能被 coordinator 反向回调,所以要在 Ingress 里配好 /camel/lra/* 路径转发到 Route 的 lra 分支。

第二步:在 YAML 路由里声明 LRA 作用域

- route:
    id: payment-route
    from:
      uri: "direct:pay"
      steps:
        - to:
            uri: "lra:pay-lra"
            parameters:
              coordinatorUrl: "{{camel.lra.coordinator-url}}"
              participantUrl: "{{camel.lra.local-participant-url}}"
              timeout: 600000
        - to: "direct:deduct-account"
        - to: "direct:send-kafka"

lra:pay-lra 节点会自动向 coordinator 发起 POST /lra-coordinator/start,返回的 LRAId 被 Camel 塞进 Exchange.LRA_ID 头;后续任何需要加入同一事务的子路由,只要拿到该头再调 lra:join 即可。

第三步:实现补偿与完成逻辑
Camel 4 把补偿路由当成普通 direct 端点,命名规则固定:

  • 补偿:direct:lra:pay-lra:compensate
  • 完成:direct:lra:pay-lra:complete

示例:

@Component
public class LraParticipant extends RouteBuilder {
    @Override
    public void configure() {
        from("direct:lra:pay-lra:compensate")
            .routeId("lra-compensate")
            .log("回滚订单 ${header.orderId}")
            .to("sql:update t_order set status='CANCEL' where order_id=:#${header.orderId}");

        from("direct:lra:pay-lra:complete")
            .routeId("lra-complete")
            .log("订单 ${header.orderId} 已最终成功")
            .to("sql:update t_order set status='DONE' where order_id=:#${header.orderId}");
    }
}

coordinator 在事务正常结束时回调 complete,异常或超时时回调 compensate;Camel 内部用 Vert.x HTTP Client 做回调,默认重试 3 次,间隔 1 s,可通过 camel.lra.client.retry-max-attempts 调整。

第四步:运维验证

  1. 用 Postman 直连 coordinator 手动开启一个 LRA,拿到 id 后调业务接口,再手动 close 或 cancel,观察补偿 SQL 是否生效;
  2. 在 Grafana 大盘 加两个核心指标:camel_lra_participant_completed_total 与 camel_lra_participant_compensated_total,红线陡增代表事务回滚风暴;
  3. 灰度发布时,先把 ** coordinator 升级到最新 Narayana 版本**,再滚动重启 Camel Pod,验证 旧事务在新版本 coordinator 里仍能正常回调,防止协议差异导致悬挂。

拓展思考

  1. 如果企业已经用 阿里云全局事务服务 GTS 或者 Seata,可以把 coordinator 的 “二阶段”协议桥接到 Camel:写一个自定义 LRAClient 实现,把 startLRA、joinLRA、closeLRA 调用转成 Seata 的 GlobalTransaction 接口,Camel 侧代码一行不改,就能让老系统渐进迁移。
  2. 在 跨云双活 场景,给 coordinator 做 主备仲裁:北京云做主,上海云做备,用 Redis Redlock 抢主节点;Camel 路由里加 circuitBreaker,发现 coordinator 返回 5xx 立即切换到备地址,事务上下文通过 JWT 传递,保证机房级故障 30 s 内自动恢复。
  3. 性能压测时发现 ** coordinator 回调瓶颈**(TPS 到 800 时回调延迟飙到 2 s),可以把补偿路由从 direct 换成 seda 队列,再横向扩容 Camel Pod,** coordinator 侧开启 HTTP/2** 与 批量回调,实测 TPS 能压到 3000 以上,99th 延迟降到 200 ms 以内,满足国内大行双 11 支付峰值。