# 模块二 基础巩固 RabbitMQ HelloWorld

## 2.6.3 RabbitMQ -- HelloWorld <a href="#id-263-rabbitmq----helloworld" id="id-263-rabbitmq----helloworld"></a>

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

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

新建控制台项目 Sender，Receiver

添加 nuget 包：RabbitMQ.Client

### 发送端 <a href="#fa-song-duan" id="fa-song-duan"></a>

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

### 接收端 <a href="#jie-shou-duan" id="jie-shou-duan"></a>

```
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 <a href="#rabbitmq-container" id="rabbitmq-container"></a>

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

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

```
http://localhost:15672/#/
```

用户名密码默认为 guest

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

### 发送信息 <a href="#fa-song-xin-xi" id="fa-song-xin-xi"></a>

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

![](https://3083743005-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8gwpNo3eyzHkX0O40HRA%2Fuploads%2FdIXqxug6F0W6i1HEiLuX%2F220.jpg?alt=media\&token=7da36c76-497d-4414-80a0-d4162227ab5b)

发送多条信息

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

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

![](https://3083743005-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8gwpNo3eyzHkX0O40HRA%2Fuploads%2FKgDYUlGwrx7Tuc7tdc9W%2F221.jpg?alt=media\&token=70c058ac-4781-48d4-82d7-90524293240e)

### GitHub源码链接： <a href="#github-yuan-ma-lian-jie" id="github-yuan-ma-lian-jie"></a>

<https://github.com/MingsonZheng/ArchitectTrainingCamp>
