Transfer.RabbitMQ - dotnet-shashlik/shashlik.eventbus GitHub Wiki

RabbitMQ

NuGet:Shashlik.EventBus.RabbitMQ

注册方式

方式一:手动配置

services.AddEventBus()
    .AddRabbitMQ(r =>
    {
        r.Host = "localhost";
        r.UserName = "guest";
        r.Password = "guest";
    });

方式二:通过配置文件

services.AddEventBus()
    .AddRabbitMQ(configuration.GetSection("RabbitMQ"));

配置文件示例:

RabbitMQ:
  Host: "localhost"
  UserName: "guest"
  Password: "guest"
  VirtualHost: "/"
  Port: 5672
  Exchange: "shashlik.eventbus"
  ConfirmTimeout: 5
  ConsumerPoolSize: 4

配置项 EventBusRabbitMQOptions

配置项 默认值 说明
Exchange shashlik.eventbus 交换机名称
Host localhost 服务端地址
UserName guest 用户名
Password guest 密码
VirtualHost / VirtualHost
Port 5672 端口号
ConfirmTimeout 5 发送消息确认超时时间(秒)
ConsumerPoolSize 4 每个 EventHandler 的消费者池大小
ConnectionFactory null 自定义连接创建工厂,优先使用此属性

内部机制

  • 生产者使用连接池复用,发送后等待 broker 确认
  • 消费者按 ConsumerPoolSize 创建多个 EventingBasicConsumer,QOS prefetch 保证有序消费
  • 消息处理成功后才确认 (ACK),失败则不确认,由 broker 重发