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.发送消息

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

  • 相关阅读:
    多产品代码架构
    PMBOK 指南 第四章 项目整合管理(4.1-4.3)
    PMBOK 指南 第三章 项目经理的角色
    PMBOK 指南 第二章 项目运行环境
    初识PMP PMBOK初解(指南第一章引论)
    c++ 拷贝构造函数(重点在内含指针的浅拷贝和深拷贝)
    C++中的try throw catch 异常处理
    source insight 3 常用设置
    C++ map insert 另一个map的子集
    PMP学习笔记--11项目干系人管理
  • 原文地址:https://www.cnblogs.com/duyao/p/14329169.html
Copyright © 2011-2022 走看看