서버
- Rabbitmq 서버에 연결은 만든다
- 채널을 만든다.
- 채널이 생성되어 있는동안 큐를 선언해서 메시지를 클라이언트로 보낸다
using RabbitMQ.Client;
using System;
using System.Text;
namespace RabbitmqServer
{
class Program
{
static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "localhost" };//ip
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("send : {0}", message);
}
Console.WriteLine("[enter] exit");
Console.ReadLine();
}
}
}
[ 서버 코드 설명 ]
//rabbitmq 서버에 연결을 만든다.
var factory = new ConnectionFactory() { HostName = "localhost" };//ip
using (var connection = factory.CreateConnection())
//대부분 api가 있는 채널을 만든다.
using (var channel = connection.CreateModel())
{
//메시지 보내려면 큐가 있어야 함 그래서 여기서 큐를 선언함 그러면 큐에 메시지를 게시할 수 있게됨
//큐는 존재하지 않는 경우에만 생성(앞서 생성해 뒀으면 생성되어 있는 씀)
// queue : 큐 이름 durable : 지속성 exclusive: 연결 끊기면 삭제할지 여부 autoDelete :구독 끊겼을때 자동으로 큐 삭제할지 여부
channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
//메시지 내용은 바이트 배열이므로 원하는대로 인코딩 가능
string message = "hello World";
var body = Encoding.UTF8.GetBytes(message);
//publish 방식으로 클라이언트에 메시지 전송
channel.BasicPublish(exchange:"",routingKey:"hello",basicProperties: null, body: body);
Console.WriteLine("send : {0}", message);
}
클라이언트
- 채널을 열고 서버에서 메시지를 받아올 큐를 선언한다.
- 메시지를 받는다.
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace Rabbitmq
{
class Program
{
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 client = new EventingBasicConsumer(channel);
client.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received : {0}", message);
};
channel.BasicConsume(queue: "hello", autoAck: true, consumer: client);
Console.WriteLine("[enter] client exit");
Console.ReadLine();
}
}
}
}
[ 클라이언트 코드 설명 ]
//채널 열고 서버에서 메시지 받아올 큐를 선언한다.
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue:"hello",durable:false,exclusive:false,autoDelete:false,arguments:null);
//서버보다 클라이언트가 먼저 시작되어서 큐에서 메시지 보내기 전에 큐가 있는지 확인
//큐에서 메시지 전달하도록 서버에 지시하기위해 이벤트 핸들러 사용
var client = new EventingBasicConsumer(channel);
client.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Received : {0}", message);
};
channel.BasicConsume(queue: "hello", autoAck: true, consumer: client);
Console.WriteLine("[enter] client exit");
Console.ReadLine();
}
서버와 클라이언트를 각각 실행한다.
클라이언트와 서버가 연결되면 클라이언트에서 서버에서 보낸 hello world 라는 글자를 볼 수 있다.
Rabbitmq 클라이언트 화면에서 큐가 생긴것을 확인한다. “hello” 라는 이름의 큐 생성
Connections , Channles , Exchanges , Queues 확인하면 새롭게 생긴거 볼 수 있음
'Rabbitmq' 카테고리의 다른 글
[ Rabbitmq ] Rabbitmq 사용하기 2 _ Request, Response 방식 (0) | 2022.12.27 |
---|---|
[ Rabbitmq ] Rabbitmq 사용하기 1. Window 에 RabbitMQ 설치, management 띄우기 (0) | 2022.12.27 |
[ Rabbitmq ] Rabbitmq 통신: c# 과 파이썬 이용해서 rabbitmq publish , consume (0) | 2022.09.04 |
[ Rabbitmq ]Window에 Rabbitmq 설치, management 띄우기 (0) | 2022.09.02 |