zoukankan      html  css  js  c++  java
  • 2.6.3 RabbitMQ--HelloWorld

    参考链接:https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

    1.RabbitMQ Container 快速安装

    docker run -d -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
    

    运行成功后可以访问(localhost 替换为服务器地址)

    http://localhost:15672/#/
    

    用户名密码默认为 guest,替换发送端,接收端的 localhost 为服务器地址

    2.新建控制台项目,Nuget引入RabbitMQ.Client,Sender(发送端)

    using System;
    using System.Text;
    using RabbitMQ.Client;
    
    namespace Sender
    {
        class Program
        {
            static void Main(string[] args)
            {
                var factory = new ConnectionFactory() { HostName = "test.guanjieerp.cn" };
                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!";
                        //var body = Encoding.UTF8.GetBytes(message);
    
                        //channel.BasicPublish(exchange: "",
                        //    routingKey: "hello",
                        //    basicProperties: null,
                        //    body: body);
                        //Console.WriteLine(" [x] Sent {0}", message);
    
    
                        //发送多条信息
                        channel.QueueDeclare(queue: "hello",
                            durable: false, // 持久化
                            exclusive: false, // 排它
                            autoDelete: false, // 自动删除
                            arguments: null);
    
                        Console.WriteLine("Please input your message with enter:");
                        string message = Console.ReadLine();
                        while (message != "EXIT")
                        {
                            var body = Encoding.UTF8.GetBytes(message);
    
                            channel.BasicPublish(exchange: "",
                                routingKey: "hello",
                                basicProperties: null,
                                body: body);
                            Console.WriteLine(" [x] Sent {0}", message);
    
                            Console.WriteLine("Please input your message with enter:");
                            message = Console.ReadLine();
                        }
                    }
                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
        }
    }

    3.新建控制台项目,Nuget引入RabbitMQ.Client,Receiver(接收端)

    namespace Receiver
    {
        class Receiver
        {
            public static void Main()
            {
                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);
    
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body.ToArray();
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x] Received {0}", message);
                    };
                    channel.BasicConsume(queue: "hello",
                        autoAck: true,
                        consumer: consumer);
    
                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
        }
    }

    4.发送消息

    启动接收端,再启动发送端,发送多条消息

  • 相关阅读:
    加入创业公司有什么利弊
    Find Minimum in Rotated Sorted Array II
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Find Minimum in Rotated Sorted Array
    Remove Duplicates from Sorted Array
    Spiral Matrix
    Spiral Matrix II
    Symmetric Tree
    Rotate Image
  • 原文地址:https://www.cnblogs.com/duyao/p/14329169.html
Copyright © 2011-2022 走看看