Skip to content

.NET 中使用 RabbitMQ

🏷️ RabbitMQ

  1. 新建一个发送消息的控制台工程 FirstRabbitMQSend

    点击查看代码
    cs
    using RabbitMQ.Client;
    using System;
    using System.Text;
    
    namespace FirstRabbitMQSend
    {
        class Program
        {
            // 交换器名
            const string EXCHANGE_NAME = "hello.exchange";
            // 队列名
            const string QUEUE_NAME = "hello.queue";
    
            static void Main(string args)
            {
                // 定义连接工厂
                var factory = new ConnectionFactory() {
                    HostName ="localhost",
                    UserName = "liujj",
                    Password = "AAA@111",
                };
                // 连接到 RabbitMQ
                using (var connection = factory.CreateConnection())
                {
                    // 获取信道
                    using (var channel  = connection.CreateModel())
                    {
                        // 声明交换器
                        channel.ExchangeDeclare(
                            exchange: EXCHANGE_NAME,
                            type: "direct",
                            durable: true);
                        // 声明队列
                        channel.QueueDeclare(
                            queue: QUEUE_NAME,
                            durable: true,
                            exclusive: false,
                            autoDelete: false,
                            arguments: null);
                        // 绑定交换器到队列
                        channel.QueueBind(
                            queue: QUEUE_NAME,
                            exchange: EXCHANGE_NAME,
                            routingKey: QUEUE_NAME);
                        // 创建消息
                        string message = "Hello, World!";
                        var body = Encoding.UTF8.GetBytes(message);
                        // 发布消息
                        channel.BasicPublish(
                            exchange: EXCHANGE_NAME,
                            routingKey: QUEUE_NAME,
                            basicProperties: null,
                            body: body);
                        Console.WriteLine("set {0}", message);
                    }
                }
            }
        }
    }
  2. 新建一个接收的控制台工程 FirstRabbitMQReceive

    点击查看代码
    cs
    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Text;
    
    namespace FirstRabbitMQReceive
    {
        class Program
        {
            // 交换器名
            const string EXCHANGE_NAME = "hello.exchange";
            // 队列名
            const string QUEUE_NAME = "hello.queue";
    
            static void Main(string args)
            {
                // 定义连接工厂
                var factory = new ConnectionFactory()
                {
                    HostName = "localhost",
                    UserName = "liujj",
                    Password = "AAA@111",
                };
                // 连接到 RabbitMQ
                using (var connection = factory.CreateConnection())
                {
                    // 获得信道
                    using (var channel = connection.CreateModel())
                    {
                        // 声明交换器
                        channel.ExchangeDeclare(
                            exchange: EXCHANGE_NAME,
                            type: "direct",
                            durable: true);
                        // 声明队列
                        channel.QueueDeclare(
                            queue: QUEUE_NAME,
                            durable: true,
                            exclusive: false,
                            autoDelete: false,
                            arguments: null);
                        // 绑定交换器到队列
                        channel.QueueBind(
                            queue: QUEUE_NAME,
                            exchange: EXCHANGE_NAME,
                            routingKey: QUEUE_NAME);
                        // 声明消费者
                        var consumer = new QueueingBasicConsumer(channel);
                        // 订阅消费者
                        channel.BasicConsume(
                            queue: QUEUE_NAME,
                            noAck: true,
                            consumer: consumer);
                        Console.WriteLine("waiting for message.");
                        while (true)
                        {
                            // 消费消息
                            var ea = consumer.Queue.Dequeue() as BasicDeliverEventArgs;
                            var body = ea.Body;
                            var message = Encoding.UTF8.GetString(body);
                            Console.WriteLine("Received {0}", message);
                        }
                    }
                }
            }
        }
    }

参考

  1. 在 Windows 上安装 Rabbit MQ 指南

  2. .NET 环境中使用 RabbitMQ