zoukankan      html  css  js  c++  java
  • .NET 云原生架构师训练营(模块二 基础巩固 RabbitMQ HelloWorld)--学习笔记

    2.6.3 RabbitMQ -- HelloWorld

    • 发送端
    • 接收端
    • rabbitmq container
    • 发送信息

    https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

    新建控制台项目 Sender,Receiver

    添加 nuget 包:RabbitMQ.Client

    发送端

    namespace Sender
    {
        class Sender
        {
            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);
    
                        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);
                    }
    
                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
        }
    }
    

    接收端

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

    rabbitmq container

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

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

    http://localhost:15672/#/
    

    用户名密码默认为 guest

    替换发送端,接收端的 localhost 为服务器地址

    发送信息

    先启动接收端,再启动发送端

    发送多条信息

    Sender

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

    先启动接收端,再启动发送端

    GitHub源码链接:

    https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp

    知识共享许可协议

    本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

    欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

    如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

  • 相关阅读:
    洛谷-P2430 严酷的训练
    Hackthebox网络不稳定的解决方案
    解压
    谷歌地图API密钥未授权利用
    关于读取heapdump踩的一个小坑
    RECON
    最近思考
    go get
    Js跳转
    Session
  • 原文地址:https://www.cnblogs.com/MingsonZheng/p/14256745.html
Copyright © 2011-2022 走看看