# RabbitMQ開發教學筆記
## 1.安裝說明
開啟Docker輸入
```
docker run -d --hostname myrabbit --name RabbitMQ -p 8080:15672 rabbitmq:3-management
```

在執行http://localhost:8080/#/
預設帳密都是 guest
RabbitMQ Server就開啟成功了!

## 2.權限設定
按下 Add user建立receive使用者

點選上面qquest底下receive進入設定權限
底下兩個Set Permissions都要按

## 3.Net實作
1.建立console專案分別為接收端與傳送端

接收端代碼
```
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
internal class Program
{
private static void Main(string[] args)
{
//建立接收者連線資訊
ConnectionFactory factory = new ConnectionFactory();
factory.UserName = "receive";
factory.Password = "receive";
factory.VirtualHost = "/";
factory.HostName = "localhost";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
IConnection connection = factory.CreateConnection();
//產生通道
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
Console.WriteLine(" [*] Waiting for messages.");
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();
}
}
}
```
傳送端代碼
```
using RabbitMQ.Client;
using System.Text;
internal class Program
{
private static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.UserName = "send";
factory.Password = "send";
factory.VirtualHost = "/";
factory.HostName = "localhost";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
IConnection connection = factory.CreateConnection();
//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();
}
}
```
## 4.實際成果
傳送端:

接收端:

## 5.程式檔案
https://github.com/hungho0208/RabbitMQ.git