GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

消息中间件插件

go-wind-plugins 提供多种消息队列(Broker)适配器,统一 broker.Broker 接口,支持发布/订阅模式与 Request/Reply 请求-应答。

一、Broker 接口

type Broker interface {
    Name() string
    Options() Options
    Address() string

    Init(...Option) error
    Connect() error
    Disconnect() error

    Publish(ctx context.Context, topic string, msg *Message, opts ...PublishOption) error
    Subscribe(topic string, handler Handler, binder Binder, opts ...SubscribeOption) (Subscriber, error)

    // Request sends a request message and waits for a response
    Request(ctx context.Context, topic string, msg *Message, opts ...RequestOption) (*Message, error)
}

type Message struct {
    ID      string
    Headers Headers          // map[string]string
    Body    any              // 经 WithCodec 注册的编解码器序列化
    Key     string           // Kafka Key / RocketMQ Key / RabbitMQ RoutingKey...
    Metadata Metadata
}

除原生 Subscribe 外,还提供泛型订阅助手 broker.Subscribe[T](自动反序列化 payload)、泛型 TypedHandler,以及发布/订阅中间件链(WithPublishMiddlewares / WithSubscriberMiddlewares)。

二、适配器列表

适配器导入路径引擎
Kafkaplugins/broker/kafkasegmentio/kafka-go
RabbitMQplugins/broker/rabbitmqrabbitmq/amqp091-go
NATSplugins/broker/natsnats-io/nats.go(JetStream)
MQTTplugins/broker/mqtteclipse/paho.mqtt.golang
Pulsarplugins/broker/pulsarapache/pulsar-client-go
Redisplugins/broker/redisgomodule/redigo
RocketMQplugins/broker/rocketmqapache/rocketmq-client-go + rocketmq-clients
NSQplugins/broker/nsqnsqio/go-nsq
SQSplugins/broker/sqsaws/aws-sdk-go-v2
GCP Pub/Subplugins/broker/gcpubsubcloud.google.com/go/pubsub
Azure Service Busplugins/broker/azuresbazure-sdk-for-go
STOMPplugins/broker/stompgo-stomp/stomp

各引擎 SDK 差异较大,每个子模块独立定义配置选项,但均实现核心 broker.Broker 接口;通用选项(WithAddress / WithCodec / WithTLSConfig / WithTracerProvider / 中间件等)由家族统一提供。

三、Kafka

import (
    "github.com/tx7do/go-wind-plugins/broker"
    kafkaPlugin "github.com/tx7do/go-wind-plugins/broker/kafka"
)

b := kafkaPlugin.NewBroker(
    broker.WithAddress("localhost:9092"),
    broker.WithCodec("json"),
)

_ = b.Init()
_ = b.Connect()
defer b.Disconnect()

发布消息

msg := broker.NewMessage(
    map[string]any{"event": "user_login", "user_id": "123"},
    broker.WithPublishHeaders(map[string]string{
        "trace_id": traceID,
    }),
)

b.Publish(ctx, "user-events", msg)

订阅消息

sub, _ := b.Subscribe("user-events",
    func(ctx context.Context, event broker.Event) error {
        fmt.Println(event.Topic(), event.Message().Body)
        return nil  // 返回 nil 自动 ACK
    },
    func() any { return &map[string]any{} },  // Binder
)

YAML 配置

broker:
  kafka:
    addrs: ["localhost:9092"]
    group_id: "my-service"
    version: "3.0.0"
    topics:
      - name: user-events
        partitions: 6
        replication: 3
    consumer:
      initial_offset: latest    # latest | earliest
      session_timeout: 10s
      rebalance_timeout: 30s
    producer:
      acks: all                 # none | one | all
      compression: snappy       # none | gzip | snappy | lz4 | zstd
      batch_size: 16384

四、RabbitMQ

import (
    "github.com/tx7do/go-wind-plugins/broker"
    rabbitmqPlugin "github.com/tx7do/go-wind-plugins/broker/rabbitmq"
)

b := rabbitmqPlugin.NewBroker(
    broker.WithAddress("amqp://guest:guest@localhost:5672/"),
    rabbitmqPlugin.WithExchangeName("events"),
    rabbitmqPlugin.WithExchangeType("topic"),
    rabbitmqPlugin.WithDurableExchange(),
    rabbitmqPlugin.WithPrefetchCount(10),   // prefetch count
)

YAML 配置

broker:
  rabbitmq:
    addrs: ["amqp://guest:guest@localhost:5672/"]
    exchange: events
    exchange_type: topic
    durable: true
    auto_delete: false
    qos:
      prefetch_count: 10
      prefetch_global: false
    queues:
      - name: order-events
        routing_key: order.*
        durable: true

五、NATS

import (
    "github.com/tx7do/go-wind-plugins/broker"
    natsPlugin "github.com/tx7do/go-wind-plugins/broker/nats"
)

b := natsPlugin.NewBroker(
    broker.WithAddress("nats://localhost:4222"),
    broker.WithCodec("json"),
)

// JetStream 订阅:声明 Durable 与投递策略
sub, _ := b.Subscribe("events",
    handler,
    binder,
    broker.WithSubscribeQueueName("my-service"),
    natsPlugin.WithDurable("my-durable"),
    natsPlugin.WithDeliverAll(),
)

YAML 配置

broker:
  nats:
    addrs: ["nats://localhost:4222"]
    jetstream: true
    max_reconnects: 60
    reconnect_wait: 2s
    credentials_file: nats.creds

六、Redis Pub/Sub

import (
    "github.com/tx7do/go-wind-plugins/broker"
    redisBrokerPlugin "github.com/tx7do/go-wind-plugins/broker/redis"
)

b := redisBrokerPlugin.NewBroker(
    broker.WithAddress("localhost:6379"),
    broker.WithCodec("json"),
    redisBrokerPlugin.WithPassword("pass"),
)

// Streams 消费组(可选)
_ = b.Subscribe("events", handler, binder,
    redisBrokerPlugin.WithStreamGroup("my-group"),
    redisBrokerPlugin.WithStreamConsumer("consumer-1"),
)

YAML 配置

broker:
  redis:
    addr: localhost:6379
    db: 1
    channels:
      - events
      - notifications
    buffer_size: 1000

七、选择指南

需求推荐
日志/事件流Kafka(高吞吐、持久化)
任务队列RabbitMQ(ACK、路由)
微服务内部通信NATS(低延迟)
简单 Pub/SubRedis(无额外依赖)
IoT 设备消息MQTT(轻量、QoS)
金融/事务消息RocketMQ(事务消息)
大规模流处理Pulsar(多租户)
云上托管队列SQS / GCP Pub/Sub / Azure Service Bus
企业消息总线对接STOMP

八、多 Broker 组合

broker:
  kafka:          # 主事件流
    addrs: ["localhost:9092"]
    group_id: "event-processor"
  rabbitmq:       # 任务队列
    addrs: ["amqp://localhost:5672/"]
    exchange: tasks
  redis:          # 通知广播
    addr: localhost:6379
    channels:
      - notifications
import (
    _ "github.com/tx7do/go-wind-plugins/broker/kafka"
    _ "github.com/tx7do/go-wind-plugins/broker/rabbitmq"
    _ "github.com/tx7do/go-wind-plugins/broker/redis"
)

相关文档

  • 插件配置系统
  • 插件总览
  • 编码解码插件
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
传输协议插件
Next
编码解码插件