.NET에서 Docker를 사용하는 RabbitMQ 대기열

https://eduardstefanescu.dev/2020/03/14/rabbitmq-queue-with-docker-in-dotnet/에 원래 게시되었습니다.


이것은 RabbitMQQueue에 대한 4개의 시리즈 중 세 번째 기사입니다. 첫 번째 부분에는 Queue 개념에 대한 간략한 소개가 포함되어 있으며 두 번째 부분에서는 코드의 각 줄에 대해 설명합니다. RabbitMQ 핵심 개념이 제시된 첫 번째 기사와 Docker 및 Producer를 사용한 환경 설정이 단계별로 설명되어 있는 기사를 확인할 수 있습니다.

소개

The RabbitMQ  Queue  is positioned in the  Message Broker  section with the  Exchange , and can be seen as buffer storage, that receives messages through a stream based on the  binding key  from the  Exchange  which also receives the messages from one or many  Producers . They are acting like a Queue Data Structure, that can be  enqueued  and  dequeued , using the FIFO rule.\
Some tips about the  Queues , that I find useful are:

  • Queue  name can't start the  amq  name, because is restricted by the  Broker  for internal usages.
  • If the  Queue  name is not specified a random one will be assigned to it.
  • Queues  with the same name can be created on the same channel, by only the last one will be kept.
All the RabbitMQ  Queue  properties can be on their site:  https://www.rabbitmq.com/queues.html .

대기열 만들기

To start creating a  Queue  firstly the connection to the RabbitMQ server should be built using the  ConnectionFactory ; is the same setup as in the previous article about RabbitMQ  Exchange  using a  URI . But in the first article where the  Producer  is created, the connection was created using explicit properties of  ConnectionFactory  (i.e.  HostnameUserName  and  Password ).

연결 설정

As you can see in the below chunk code, all these properties are embedded into one  Uri  instance.

var factory = new ConnectionFactory
{
    Uri = new Uri("amqp://guest:guest@localhost"),
    ContinuationTimeout = TimeSpan.MaxValue
};
ConnectionTimeout  property was set to its maximum value because I didn't want the connection to expire, but this is just for this article purposes. If you are interested to find all the  ConnectionFactory  properties, they are well documented here with one example:  https://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.2.4/rabbitmq-dotnet-client-3.2.4-client-htmldoc/html/type-RabbitMQ.Client.ConnectionFactory.html . 이 문서를 읽을 때 버전 3.2.4에 대한 설명서입니다. .NET용 RabbitMQ 클라이언트가 업데이트될 수 있으므로 일부 속성이 변경, 사용 중단 또는 제거될 수 있습니다.

개발자 입력

You may wonder why the user or the developer input is involved in this, is because this series of articles about  RabbitMQ  is more educational rather than a solution-oriented approach. So I wanted to replicate one to one the  http://tryrabbitmq.com/ .NET을 이용한 RabbitMQ의 기본 원리 학습 개념.\
따라서 RabbitMQ 서버에 대한 연결이 구축된 후 사용자는 만들고자 하는 번호Queues를 입력하고 각각의 이름Queue과 이름routing key을 입력해야 합니다.

for (var queueIndex = 0; queueIndex < queuesCount; queueIndex++)
{
    Console.Write($"Queue[{queueIndex}] name: ");
    var queueName = Console.ReadLine();

    Console.Write($"Routing key for Queue[{queueIndex}]: ");
    var routingKey = Console.ReadLine();
...


연결 설정

When the required properties are entered, for each  Queue  a  Connection  and a communication  Channel  will be created. After that, the  Queue  is declared, and the bound to the  Exchange  that has the same name for the entire solution (i.e. test-exchange). If the creation and the binding were ended successfully, then a message is prompted informing the user that the  Queue  was created.

...
    using (IConnection connection = factory.CreateConnection())
    {
        using (channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: queueName,
                durable: false,
                exclusive: false,
                autoDelete: false,
                arguments: null);

            channel.QueueBind(queue: queueName,
                exchange: "test-exchange",
                routingKey: routingKey);
        }
    }

    Console.WriteLine(value: $"Queue {queueName} created.");
}

QueueDeclare 매개변수

Let's dig a little bit deep into the parameters of the  QueueDeclare :

  • queue  it's self-evident that it refers to the  Queue  name;
  • durable  represents the lifetime of the  Queue  in the  Broker , if it's set to false the  Queue  will end when the  Broker  does too;
  • autoDelete  is used to specify the lifetime of the  Queue  based on its subscriptions. The  Queue  will be deleted if the last  Consumer  subscribed to it, unsubscribes;
  • arguments  are used to sent information about the  Queue  (e.g. length limit) to the  Broker ;

QueueBind 's parameters are self-explanatory, the first one refers to the  queueName , the second to the  exchangeName  and the last one to the  routingKey .

결과

After the  Queues  are created, a message is prompted to the user with how many  Queues  were created.



생성된 Queues 는 RabbitMQ 관리 페이지에서도 볼 수 있습니다.



이제 Consumer를 생성하기만 하면 전체 토폴로지를 사용할 수 있습니다. Consumer는 다음 기사에서 작성되며 이 시리즈의 마지막 기사입니다.

이 기사를 읽어 주셔서 감사합니다. 흥미로웠다면 동료 및 친구들과 공유하십시오. 또는 개선할 수 있는 부분이 있으면 알려주세요.

좋은 웹페이지 즐겨찾기