RabbitMq 开始<一>

power shell 执行:

dotnet new console --name Send
mv Send/Program.cs Send/Send.cs
dotnet new console --name Receive
mv Receive/Program.cs Receive/Receive.cs

cd Send
dotnet add package RabbitMQ.Client
dotnet restore
cd ../Receive
dotnet add package RabbitMQ.Client
dotnet restore

send 代码:

class Send
{
    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);

         

while (true)
{
channel.BasicPublish(exchange: "",
            routingKey: "hello",
            basicProperties: null,
            body: Encoding.UTF8.GetBytes("Hello World!"+DateTime.Now.ToString("hh:mm:ss")));

Thread.Sleep(2000);
}




  


        }

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }
}

receive:代码

 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);

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    Console.WriteLine(" [x] Received {0}", Encoding.UTF8.GetString(ea.Body));
                };
                channel.BasicConsume(queue: "hello",
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
        }

启动docker:

docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

为什么要加上  :3-management  ?

可以开几个 send  或者receive

http://localhost:15672 打开可以查看  。默认密码用户名 guest

气功波(18037675651)
原文地址:https://www.cnblogs.com/qgbo/p/11322603.html