• 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)
  • 相关阅读:
    肩部肌肉劳损zt
    大屏幕手机上网页字体显示很小的问题
    SWT的Display
    The connection to adb is down, and a severe error has occured.
    [ZT]使用tmpfs缓存文件提高性能
    Mutex
    javascript阻塞加载问题【转】
    IE参考
    2台电脑网线对接注意的事项
    重建索引
  • 原文地址:https://www.cnblogs.com/qgbo/p/11322603.html
Copyright © 2020-2023  润新知