RabbitMQ具体使用

上篇简述了 RabbitMQ环境准备/环境搭建,本片在上篇的基础上讲述RabbitMQ具体使用

1、新建.NET Framwork console控制台项目ConsoleRabbitMQ项目(生产者,即产生消息的)

2、对控制台项目ConsoleRabbitMQ项目使用NuGet程序管理包添加RabbitMQ.Client

3、控制台项目ConsoleRabbitMQ项目的Program代码如下

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace ConsoleRabbitMQConsume
{
    class Program
    {
        static void Main(string[] args)
        {
            var connectFactory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "guest",
                Password = "guest"
            };
            using (var conn = connectFactory.CreateConnection())
            {
                using (var model = conn.CreateModel())
                {
                    Console.WriteLine($"消费者连接成功");
                    model.QueueDeclare("queue", false, false, false, null);
                    var consume = new EventingBasicConsumer(model);
                    //consume.Received += Consume_Received;
                    consume.Received += (a, b) => { Console.WriteLine($"这是消费者据接收数据:{Encoding.UTF8.GetString(b.Body)}"); };
                    model.BasicConsume("queue", false, consume);
                    Console.Read();
                }
            }
        }

        private static void Consume_Received(object sender, BasicDeliverEventArgs e)
        {
            Console.WriteLine($"接收数据:{Encoding.UTF8.GetString(e.Body)}");
        }
    }
}

 

4、新建.NET Framwork console控制台项目ConsoleRabbitMQConsume项目(消费者,即接收消息的)

5、对控制台项目ConsoleRabbitMQConsume项目使用NuGet程序管理包添加RabbitMQ.Client

6、控制台项目ConsoleRabbitMQConsume项目的Program代码如下

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace ConsoleRabbitMQConsume
{
    class Program
    {
        static void Main(string[] args)
        {
            var connectFactory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "guest",
                Password = "guest"
            };
            using (var conn = connectFactory.CreateConnection())
            {
                using (var model = conn.CreateModel())
                {
                    Console.WriteLine($"消费者连接成功");
                    model.QueueDeclare("queue", false, false, false, null);
                    var consume = new EventingBasicConsumer(model);
                    //consume.Received += Consume_Received;
                    consume.Received += (a, b) => { Console.WriteLine($"这是消费者据接收数据:{Encoding.UTF8.GetString(b.Body)}"); };
                    model.BasicConsume("queue", false, consume);
                    Console.Read();
                }
            }
        }

        private static void Consume_Received(object sender, BasicDeliverEventArgs e)
        {
            Console.WriteLine($"接收数据:{Encoding.UTF8.GetString(e.Body)}");
        }
    }
}

 

7、启动RabbitMQ服务

 

输入命令:rabbitmq-service start

浏览器中输入:http://localhost:15672/#/,点击ConnectionsChannels,Queues可以查看相关信息,如下

8、分别启动ConsoleRabbitMQ.exe和ConsoleRabbitMQConsume.exe这个两个项目,运行效果如下

 

 

posted @ 2020-04-14 12:49  龙骑科技  阅读(292)  评论(0编辑  收藏  举报