RabbitMQ .NET

setup rabbitmq

docker run --name=rabbit -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=xxx -d rabbitmq:management

ip:15672
在这里插入图片描述

login
在这里插入图片描述

nuget RabbitMQ.Client

Send

//1.1.实例化连接工厂
var factory = new ConnectionFactory() { HostName = "localhost" };
//2. 建立连接
using (var connection = factory.CreateConnection())
{
    //3. 创建信道
    using (var channel = connection.CreateModel())
    {
        //4. 申明队列
        channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
        //5. 构建byte消息数据包
        string message = args.Length > 0 ? args[0] : "Hello RabbitMQ!";
        var body = Encoding.UTF8.GetBytes(message);
        //6. 发送数据包
        channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
        Console.WriteLine(" [x] Sent {0}", message);
    }
}

Received

//1.实例化连接工厂
var factory = new ConnectionFactory() { HostName = "localhost" };
//2. 建立连接
using (var connection = factory.CreateConnection())
{
    //3. 创建信道
    using (var channel = connection.CreateModel())
    {
        //4. 申明队列
        channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
        //5. 构造消费者实例
        var consumer = new EventingBasicConsumer(channel);
        //6. 绑定消息接收后的事件委托
        consumer.Received += (model, ea) =>
        {
            var message = Encoding.UTF8.GetString(ea.Body.ToArray());
            Console.WriteLine(" [x] Received {0}", message);
            Thread.Sleep(6000);//模拟耗时
            Console.WriteLine(" [x] Done");
        };
        //7. 启动消费者
        channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }
}


在这里插入图片描述

在这里插入图片描述

Err
RabbitMQ.Client.Exceptions.BrokerUnreachableException:“None of the specified endpoints were reachable”
IP host

Err
AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism

new ConnectionFactory() { HostName = "localhost" ,UserName="admin",Password="xxx"}

消息持久化

		//...
      //4. 申明队列(指定durable:true,告知rabbitmq对消息进行持久化)
      channel.QueueDeclare(queue: "hellooo", durable: true, exclusive: false, autoDelete: false, arguments: null);
      //将消息标记为持久性 - 将IBasicProperties.SetPersistent设置为true
      var properties = channel.CreateBasicProperties();
      properties.Persistent = true;
      //5. 构建byte消息数据包
      string message = args.Length > 0 ? args[0] : "Hello RabbitMQ!";
      var body = Encoding.UTF8.GetBytes(message);
      //6. 发送数据包(指定basicProperties)
      channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: properties, body: body);
      //6. 发送数据包
      channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
      Console.WriteLine(" [x] Sent {0}", message);

durable: true

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值