RabbitMQ 生产者

原文连接:https://www.cnblogs.com/ysmc/p/16225118.html

项目需要引用 RabbitMQ.Client Nuget包

生产消息

 1 IAsyncConnectionFactory connectionFactory = new ConnectionFactory
 2 {
 3     HostName = _rabbitMqOptions.HostName,
 4     Port = _rabbitMqOptions.Port,
 5     UserName = _rabbitMqOptions.UserName,
 6     Password = _rabbitMqOptions.Password,
 7     VirtualHost = _rabbitMqOptions.VirtualHost,
 8     DispatchConsumersAsync = true
 9 };
10 
11 using (var connection = connectionFactory.CreateConnection())
12 {
13     using var channel = connection.CreateModel();
14 
15     var routingKey = queueName;
16 
17     channel.QueueDeclare(queueName, true, false, false, null);
18 
19     foreach (var message in messages)
20     {
21         var sendBody = Encoding.UTF8.GetBytes(message);
22 
23         channel.BasicPublish("", routingKey, null, sendBody);
24     }
25 }

 

posted @ 2022-05-05 16:08  一事冇诚  阅读(2309)  评论(0编辑  收藏  举报