Dapr pub/sub

lua 复制代码
graph TD
    %% 外部系统
    PubSub[(Pub/Sub: RabbitMQ / Kafka / Redis)]

    %% Dapr Sidecar
    subgraph DAPR["Dapr Sidecar"]
        DaprPubSub["Pub/Sub Adapter"]
        GRPCStream["gRPC Stream"]
    end

    %% 应用程序
    subgraph APP["Your App Service"]
        SDK["Dapr SDK Client"]
        GoroutineReceive["Subscribe goroutine (Receive loop)"]
        Listener["WorkflowEventListener"]
        Handler["processCreated / processStop / ..."]
    end

    %% Publish 流程
    AppPub["PublishEvent() 调用"]
    AppPub --> SDK
    SDK --> DaprPubSub
    DaprPubSub --> PubSub

    %% Subscribe 流程
    PubSub --> DaprPubSub
    DaprPubSub --> GRPCStream
    GRPCStream --> GoroutineReceive
    GoroutineReceive --> Listener
    Listener --> Handler
    Handler --> GoroutineReceive
    GoroutineReceive --> DaprPubSub
    DaprPubSub --> PubSub
js 复制代码
sequenceDiagram
    participant PubSub as 外部 Pub/Sub 系统
    participant Dapr as Dapr Sidecar
    participant SDK as Dapr Go SDK
    participant Goroutine as Subscribe goroutine
    participant Handler as 业务 Handler (e.g. processCreated)

    Note over PubSub,Handler: 🟢 消息从发布到消费的全过程

    PubSub->>Dapr: 推送消息 (topic)
    Dapr->>SDK: gRPC 推送事件流
    SDK->>Goroutine: 启动 goroutine (for { s.Receive() })
    Goroutine->>Handler: 为每条消息再启动新的 handler goroutine
    Handler-->>Goroutine: 处理完成 (成功 / 失败)
    Goroutine->>SDK: 调用 respondStatus()
    SDK->>Dapr: Ack 消息状态
    Dapr->>PubSub: 确认消费完成

    Note over SDK,Handler: SDK 内部常驻 goroutine 循环 Receive(),<br>每条消息再开新 goroutine 并发执行 handler。

🧩 流程说明

  1. Pub/Sub → Dapr Sidecar
    外部消息系统(如 RabbitMQ、Kafka、Redis Streams)把消息推给 Dapr sidecar。
  2. Dapr Sidecar → SDK (gRPC 流)
    Dapr 通过 gRPC 双向流,把消息推送到你的应用。
  3. SDK → goroutine (接收循环)
    SubscribeWithHandler() 内部启动一个 goroutine,循环执行 Receive()
  4. 每条消息再开 goroutine 调用 handler
    当消息到达时,SDK 再为该消息启动一个新的 goroutine 调用你的 handler(如 processCreated)。
  5. handler 执行完成后 ack
    handler 执行完,SDK 通过 respondStatus() 向 Dapr 返回 ack。
  6. Dapr 通知外部系统确认消费
    Dapr 发送 ack 给 Pub/Sub 确认这条消息被成功处理。

🔑 关键特征

阶段 并发模型 是否阻塞
SDK 启动订阅循环 goroutine 常驻 阻塞在 Receive()
handler 执行 每条消息独立 goroutine 不阻塞主循环
ack 流程 异步返回 非阻塞
相关推荐
Victor3568 小时前
Hibernate(91)如何在数据库回归测试中使用Hibernate?
后端
Victor3568 小时前
MongoDB(1)什么是MongoDB?
后端
Victor35614 小时前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor35614 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
后端
灰子学技术16 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
Gogo81617 小时前
BigInt 与 Number 的爱恨情仇,为何大佬都劝你“能用 Number 就别用 BigInt”?
后端
fuquxiaoguang17 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
毕设源码_廖学姐17 小时前
计算机毕业设计springboot招聘系统网站 基于SpringBoot的在线人才对接平台 SpringBoot驱动的智能求职与招聘服务网
spring boot·后端·课程设计
野犬寒鸦19 小时前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
逍遥德20 小时前
如何学编程之01.理论篇.如何通过阅读代码来提高自己的编程能力?
前端·后端·程序人生·重构·软件构建·代码规范