自学内容网 自学内容网

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

在这里插入图片描述


原文地址:https://blog.csdn.net/lmnotlm/article/details/139009765

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!