自学内容网 自学内容网

C# 操作Rabbitmq

下载安装rabbitmq

参考

安装依赖包

下面的生产者和消费者都需要安装

RabbitMQ.Client 7.0.0

创建生产者

using RabbitMQ.Client;
using System.Threading.Channels;

namespace RabbitMQStu01_Product
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("RabbitMQ生产者");
            // 1、建立RabbitMQ连接
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.HostName = "127.0.0.1";
            connectionFactory.Port = 5672;
            connectionFactory.UserName = "guest";
            connectionFactory.Password = "guest";
            connectionFactory.VirtualHost = "/";
            IConnection connection = connectionFactory.CreateConnectionAsync().Result;

            // 2、创建连接通道
            var channel = connection.CreateChannelAsync().Result;
            //发送商品数据。
            var queueName = "test20241111";
            // 3、发送商品数据
            // 3.1、创建队列
            var queueDeclareOk = channel.QueueDeclareAsync(queueName, false, false, false).Result;
            if (queueDeclareOk == null)
            {
                Console.WriteLine("创建队列成功");
                return;
            }
            // 3.2、发送数据
            byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
            var props = new BasicProperties();
            props.ContentType = "text/plain";
            props.DeliveryMode = DeliveryModes.Persistent;
            channel.BasicPublishAsync("", queueName,
                mandatory: true, basicProperties: props, body: messageBodyBytes);
            Console.WriteLine("发送成功");
            Console.ReadKey();
        }
    }
}

创建消费者

using RabbitMQ.Client;
using System.Text;

namespace RabbitMQStu01_Consumer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("RabbitMQ消费者");
            // 1、建立RabbitMQ连接
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.HostName = "127.0.0.1";
            connectionFactory.Port = 5672;
            connectionFactory.UserName = "guest";
            connectionFactory.Password = "guest";
            connectionFactory.VirtualHost = "/";
            IConnection connection = connectionFactory.CreateConnectionAsync().Result;

            // 2、创建连接通道
            var channel = connection.CreateChannelAsync().Result;
            var queueName = "test20241111";
            // 3、消费商品
            // 3.1、实现IBasicConsumer
            var basicConsumer = new RabbitMQ.Client.Events.AsyncEventingBasicConsumer(channel);
            // 3.2、接受商品数据
            basicConsumer.ReceivedAsync += async (mode, data) => {
                // 3.3、消费商品
                var productByte = data.Body;
                string productString = Encoding.UTF8.GetString(productByte.ToArray());
                Console.WriteLine("消费了一条消息:"+ productString);
            };

            channel.BasicConsumeAsync(queueName,
                                true,
                                "",
                                 false,
                                 false,
                                 new Dictionary<string, object>(),
                                 basicConsumer).Wait();
            Console.ReadKey();
        }
    }
}

生产数据
在这里插入图片描述
消费数据
在这里插入图片描述

注意:消费者监听之后可以一直消费,生产者需要一件一件的生产


原文地址:https://blog.csdn.net/qq_36437991/article/details/143692179

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