RebbitMQ一 : Hello World

RebbitMQ一 : Hello World

相关文档

官网:http://www.rabbitmq.com

Erlang:http://www.erlang.org/downloads

RabbitMQ:http://www.rabbitmq.com/install-windows.html

RabbitMQ使用:http://www.rabbitmq.com/getstarted.html

RabbitMQ文档:http://www.rabbitmq.com/documentation.html

管理端

开启管理端:cd到rabbitmq安装目录的sbin目录下,执行命令:rabbitmq-plugins enable rabbitmq_management,初始用户密码为guest/guest,访问地址:http://host:15672

.NET 示例

示例一:helloworld

消息发布:

  1. 创建一个RabbitMQ服务器连接
  2. 创建一个频道
  3. 声明一个队列:声明队列是幂等的-只有在它不存在的情况下才会创建它。
  4. 发布消息
    class Send
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
                    //string message = "Hello World!";
                    string message = Console.ReadLine();
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
                    Console.WriteLine($"send:{message}");
                }
            }
            Console.WriteLine("Press [enter] to exit.");
            Console.Read();
        }
    }

消息接收:

说明:消费者接收来自RabbitMQ的推送消息。因此,与发布单个消息的发布者不同,我们会让消费者持续运行以监听消息并将其打印出来。

步骤:
同生产者,打开一个连接和频道,然后定义队列(与上述发布中的的队列定义匹配),再去接收消息。

    class Receive
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var conncetion = factory.CreateConnection())
            {
                using (var channel = conncetion.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
                    var consumer = new EventingBasicConsumer(channel);
                    //consumer.Received += Consumer_Received;
                    consumer.Received += (sender, e) =>
                    {
                        var body = e.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine($"Received: {message}");
                    };
                    channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
                    Console.WriteLine("Press [enter] to exit");
                    Console.Read();
                }
            }
        }

        private static void Consumer_Received(object sender, BasicDeliverEventArgs e)
        {
            var body = e.Body;
            var message = Encoding.UTF8.GetString(body);
            Console.WriteLine($"Received {message}");
        }
    }

附:

源码:GitHub
说明:本系列文章均来自官方文档

猜你喜欢

转载自blog.csdn.net/zhaobw831/article/details/81108705