Notes on using RabbitMQ in .net

Advantages of using RabbitMQ

        1. Comprehensive performance, rabbitmq has relatively comprehensive performance, and is the first choice for message middleware

        2. High concurrency, the rabbitmq implementation language is an erlang language that is inherently high in concurrency and high availability.

        3. Task asynchronous processing, the message queue notifies the message recipient for asynchronous processing of operations that do not require synchronous processing and take a long time, which improves the response time of the application.

        4. Application decoupling. MQ is equivalent to an intermediary. The producer interacts with the consumer through MQ, which decouples the application.

Core API interfaces and classes

IModel: represents an AMQP channel and provides most operations

IConnection: represents an AMQP connection

ConnectionFactory:: Construct an IConnection instance

IBasicConsumer: represents a message consumer

DefaultBasicConsumer: Commonly used base class for consumers

limit

The client does not support unsigned 64-bit integers (ulong), but supports signed 64-bit integers.

Introduction to use

namespace reference

using RabbitMQ.Client;

Connect to RabbitMQ

ConnectionFactory factory = new ConnectionFactory();
factory.UserName = user;
factory.Password = pass;
factory.VirtualHost = vhost;
factory.HostName = hostName;

IConnection conn = factory.CreateConnection();

open a channel

IModel channel = conn.CreateModel();

Disconnect  

channel.close();

conn.close();

After closing the connection, the channel will be automatically released

connection is a long connection, and the underlying protocol is optimized for long connections, which means that opening a new connection will have huge overhead. Channels are also long connections, and the cost of creating a new channel will be much smaller. Under normal circumstances, it is recommended that channels be reused and not created repeatedly.

Set the client name (to facilitate the identification of multiple clients, it is strongly recommended to set it)

factory.ClientProvidedName = "app:audit component:event-consumer";

Define switches and queues and bind them

channel.ExchangeDeclare(exchangeName, ExchangeType.Direct);
channel.QueueDeclare(queueName, false, false, false, null);
channel.QueueBind(queueName, exchangeName, routingKey, null);

Delete directly

channel.QueueDelete("queue-name", false, false);

Delete the queue if empty

channel.QueueDelete("queue-name", false, true);

Delete the queue if not in use (does not have any consumers)

channel.QueueDelete("queue-name", false, true);

Delete the queue and clear the messages

channel.QueueDelete("queue-name");

make an announcement

byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
channel.BasicPublish(exchangeName, routingKey, null, messageBodyBytes);

Fine-grained control over publishing messages

byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
IBasicProperties props = channel.CreateBasicProperties();
props.ContentType = "text/plain";
props.DeliveryMode = 2;
channel.BasicPublish(exchangeName, routingKey, props, messageBodyBytes);

Publish a message with header parameters

byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");

IBasicProperties props = channel.CreateBasicProperties();
props.ContentType = "text/plain";
props.DeliveryMode = 2;
props.Headers = new Dictionary<string, object>();
props.Headers.Add("latitude",  51.5252949);
props.Headers.Add("longitude", -0.0905493);

channel.BasicPublish(exchangeName, routingKey, props, messageBodyBytes);

Post a message with a deadline

byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");

IBasicProperties props = channel.CreateBasicProperties();
props.ContentType = "text/plain";
props.DeliveryMode = 2;
props.Expiration = "36000000";

channel.BasicPublish(exchangeName, routingKey, props, messageBodyBytes);

subscribe news

var consumer = new EventingBasicConsumer(channel);
consumer.Received += (ch, ea) =>
                {
                    var body = ea.Body.ToArray();
                    // copy or deserialise the payload
                    // and process the message
                    // ...
                    channel.BasicAck(ea.DeliveryTag, false);
                };
string consumerTag = channel.BasicConsume(queueName, false, consumer);

Start automatic reconnection

factory.AutomaticRecoveryEnabled = true;

//The default reconnection time is 5 seconds, here it is set to 10s

factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10);

Guess you like

Origin blog.csdn.net/yunxiaobaobei/article/details/132402212