RabbitMQ etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
RabbitMQ etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

6 Ağustos 2021 Cuma

RabbitMQ Fanout Exchange - Ana Gemi Tüm Gemilere Mesaj Gönderir

Giriş
Fanout Exchange -  Tüm kuyruklara mesajı yönlendirir. Routing key dikkate alınmaz. Açıklaması şöyle
 A fanout exchange copies and routes a received message to all queues that are bound to it regardless of routing keys or patterns. The keys provided will simply be ignored.
Şeklen şöyle

Açıklaması şöyle
In the above diagram each consumer is independent of the others and receives its own copies of all the messages. To scale out the Consumer App 1, more instances of that application would need to be deployed, consuming from the same Queue 1.

Fanout exchanges are one of the fastest exchanges as they do not need to inspect any routing key or message header. Although an exchange may send a single message to multiple queues, in reality it doesn't necessarily duplicate the bits. It can persist the message to the message store and simply register a pointer to the message in each queue.


RabbitMQ Direct Exchange - Küçük Gemiler Ana Gemiye Mesaj Gönderir

Giriş
Routing key = Binding key ise mesajı kuyruğa yönlendirir. Açıklaması şöyle
Direct exchanges route messages to all queues with an exact match for something called a routing key. 
Bu şu anlama gelir. Direct Exchange'e bir kuyruk ya da birden fazla kuyruk takılabilir. 

Mesaj gönderirken kuyruk ismi routing key olarak belirtilir.

2 Temmuz 2021 Cuma

RabbitMQ Kurulum

Giriş
Windows'a kurulum için bir yazı burada. RabbitMQ Erlang ile çalıştığı için önce Erlang kurulumu yapmak gerekir.

Docker
İki tane image var
1. rabbitmq:3-management : Bu image ile management plugin etkin geliyor
2. rabbitmq

Örnek - docker
Şöyle yaparız. 1562 portu "http://localhost:15672/" yani dashboard için gerekir.
docker run -d --hostname my-rabbit --name rabbitmq-dlx
-p 15672:15672 -p 5672:5672 rabbitmq:3-management
Örnek
Şöyle yaparız
docker run -d --name some-rabbit 
  -p 4369:4369 
  -p 5671:5671 
  -p 5672:5672 
  -p 15672:15672 
  rabbitmq

# Enable management plugin
docker container exec -it some-rabbit 
  rabbitmq-plugins enable rabbitmq_management

Örnek - Docker
docker-compose.yml dosyasına şöyle yazarız
version: '3.7'
services:
  avc-rabbit:
    image: rabbitmq:3-management
    container_name: rabbitmq
    ports:
      - "15672:15672"
      - "5672:5672"
Çalıştırmak için şöyle yaparız
docker-compose up -d
Örnek - Komut Satırı
Şöyle yaparız
cd C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.7\sbin

rabbitmq-server.bat start
Web Management Console
C:\Program Files\RabbitMQ Server\rabbitmq_server-X\sbin\ altındaki şu komut çalıştırılır. Böylece artık management console'a giriş yapabiliriz.
rabbitmq-plugins.bat enable rabbitmq_management
Açıklaması şöyle. http://localhost:15672 adresine guest:guest olarak bağlanabiliyoruz.
15672 exposes a web management page that you can check out by opening a browser, navigating to http://localhost:15672, and using the credentials guest:guest.
Açıklaması şöyle
RabbitMQ has a user-friendly interface that lets you monitor and handle your RabbitMQ server from a web browser. Among other things, queues, connections, channels, exchanges, users and user permissions can be handled (created, deleted, and listed) in the browser, and you can monitor message rates and send/receive messages manually.
Exchanges Sekmesi
Şeklen şöyle. Burada yeni exchange de eklenebilir.


Shovel Plugin
Açıklaması şöyle. Yani iki tane datacenter, federation indenpendent clusters arasında mesajları kopyalar.
RabbitMQ Shovel is a message mover.

Think of it as a built-in RabbitMQ client that:

- Consumes messages from a source queue.
- Connects to another broker/cluster.
- Republishes the messages there.
- Handles reconnects, acknowledgements, and failures.
Bu eklenti ile kuyrukta çok fazla bekleyen mesajlar "Dead Letter Exchange" yani ölü mesaj kuyruğuna gönderilebilir.

11 Şubat 2021 Perşembe

RabbitMQ Exchange Çeşitleri

Giriş

Producer, Queue ile direkt konuşmaz. Arada Exchange yapısı vardır. Eğer tek bir queue varsa Exchange yapısı biraz mantıksız görünebilir. Exchange ile kuyruğun birbirine bağlanmasına binding denilir.

Producer routing key değeri ile mesaj gönderir. Exchange ise binding key ile karşılaştırarak eşleşen kuyruğa yönlendirir. Şeklen şöyle

Exchange çeşitleri şöyle

1. Direct Exchange
Direct Exchange yazısına taşıdım

2. Topic Exchange 
Routing key ve Binding key kısmen eşleşirse mesajı kuyruğa yönlendirir. Yani bu exchange'a bağlı bir sürü kuyruk varsa tüm uyan kuyruklara gönderir. Açıklaması şöyle
Topic exchange is similar to direct exchange, but the routing is done according to the routing pattern. Instead of using fixed routing key, it uses wildcards. Messages are routed to one or many queues based on a matching between a message routing key and pattern. The routing key must consist of list of words delimited by a period “.”.

For example, let’s suppose we have following routing patterns.
- order.logs.customer
- order.logs.international
- order.logs.personnel
- order.logs.customer.electronics
- order.logs.personnel.electronics
- order.logs.international.electronics

The routing patterns may also contain an asterisk (“*”) to match a word in a specific position of the routing key. For example, a routing pattern of “order.*.*.electronics” only match routing keys where the first word is “order” and the fourth word is “electronics”.

The routing pattern may also contain a pound symbol (“#”) which means a match of zero or more words. For example, a routing pattern of “order.logs.customer.#” matches any routing keys beginning with “order.logs.customer “.
3. Fanout Exchange
Fanout Exchange yazısına taşıdım

4. Header Exchange
Routing key yerine message header kullanılır. Açıklaması şöyle
A headers exchange routes messages based on arguments containing headers and optional values. It uses the message header attributes for routing.

A special argument named “x-match”, added in the binding between exchange and queue, specifies if all headers must match or just one. The “x-match” property can have two different values: “any” or “all”,where “all” is the default value. A value of “all” means all header pairs (key, value) must match, while value of “any” means at least one of the header pairs must match.
5. Default (nameless) Exchange
Eğer queue için Exchange tanımlanmazsa exchange otomatik yaratılır. Routing key = queue name ise mesaj kuyruğa yönlendirilir. Açıklaması şöyle
The default exchange is a pre-declared direct exchange that has no name. It is usually referred by an empty string. If you use default exchange your message is delivered to the queue with a name equal to the routing key of the message. Every queue is automatically bound to the default exchange with a routing key which is the same as the queue name.
6. Dead Letter Exchange
Açıklaması şöyle
If there is no matching queue for the message, the message is dropped. RabbitMQ provides an AMQP extension known as the “Dead Letter Exchange”. This exchange which provides the functionality to capture messages that are not deliverable.
Genel kullanım DLX için 3 tane sebep var. Açıklaması şöyle
In fact, in RabbitMQ, there are three forms of "death" in three messages:

- The message was rejected. By calling basic.reject or basic.nack and setting the requeue parameter to false.
- The message expired because the TTL was set.
- The message enters a queue that has reached its maximum length.

8 Mayıs 2019 Çarşamba

RabbitMQ

Giriş
2007 yılında geliştirilmeye başlandı ve Erlang ile geliştirildi. Bu yüzden maalesef JVM içinde gömülü olarak çalıştırılamıyor. Açıklaması şöyle.
One of the oldest open source message brokers can be used with various protocols. Many web developers like this software, because of its useful features, libraries, development tools, and instructions.

In 2007, Rabbit Technologies Ltd. had developed the system, which originally implemented AMQP. It's an open wire protocol for messaging with complex routing features. AMQP ensured cross-language flexibility of using message broking solutions outside the Java ecosystem. In fact, RabbitMQ perfectly works with Java, Spring, .NET, PHP, Python, Ruby, JavaScript, Go, Elixir, Objective-C, Swift, and many other technologies. The numerous plugins and libraries are the main advantage of the software.
2013 yılında Pivotal Software'e katıldı. Açıklaması şöyle
RabbitMQ was originally created by Rabbit Technologies Ltd. The project became part of Pivotal Software in May 2013.
Protokoller
RabbitMQ bir sürü protokolü gerçekleştirir - AMQP, MQTT, STOMP. Ama en önemlisi AMQP protokolünü gerçekleştirir. AMQP protokolünün JMS'e göre avantajı şöyle
While Java has messaging standards like JMS, it’s not helpful for non-Java applications that need distributed messaging which is severely limiting to any integration scenario, microservice or monolithic. With the advent of AMQP, cross-language flexibility became real for open source message brokers.
Çünkü AMQP aynı Kafka gibi sadece byte[] taşıyor. Gerisi ile ilgilenmiyor. Açıklaması şöyle
In AMQP the messages are opaque entities; AMQP does not provide any standard way to encode/decode them.

However, web applications very often use JSON as an application layer format, that is, the JavaScript serialization format that has become a de-facto standard; in this way, the RabbitMQ client Java library can include some utility functions for this task.

On the other side, this is not the only protocol; any application can choose its own protocol (XML, Google Protocol Buffers, ASN.1, or proprietary).
Kurulum
RabbitMQ Kurulum yazısına taşıdım

Push Model İle Message Order Kaybedilir
Açıklaması şöyle
In RabbitMQ, messages go through an exchange, land on a queue then gets distributed to consumers in a way that a message can go to only one consumer. It is super easy to increase the replica count of consumers, but in this topology, you loose message ordering. Because the second message can be processed before the first one is completed.
Şeklen şöyle


Push Model + Acknowledgement
RabbitMQ Broker mesajları dinleyenlere kendisi push'lar. Mesajı alan istemcinin acknowledgement göndermesi gerekir. Acknowledgement şöyledir. 

Positive Acknowledgement
Spring terminolojisinde buna auto ve manual acknowledgement deniliyor.
In RabbitMQ, messages are stored until a receiving application connects and receives a message off the queue. The client can either ack (acknowledge) the message when it receives it or when the client has completely processed the message. In either situation, once the message is acked, it’s removed from the queue.
Prefetch Limit
Prefetch Limit kullanılabilir. Açıklaması şöyle. Prefetch count istemciye kaç tane mesajın push'lanacağı anlamına gelir.
It’s important to configure a prefetch limit in order to prevent overwhelming the consumer (if messages arrive at the queue faster than the consumers can process them). Consumers can also pull messages from RabbitMQ, but it’s not recommended. 
Negative Acknowledgement
Gerekiyorsa Negative Acknowledgement verilebilir. Açıklaması şöyle
A RabbitMQ client can also nack (negative acknowledgement) a message when it fails to handle the message. The message will be returned to the queue it came from as if it were a new message; this is useful in case of a temporary failure on the consumer side.
Şeklen şöyle. Tüketen taraf çöktüğü için bir başkası mesajı işliyor.


Negative Acknowledgement + Retry
Açıklaması şöyle. Bazı eksikler var. 1. Retry için delay verilemiyor. 2. Retry sayısı verilemiyor. dolayısıyla kutudan gelen Nack kullanımı yetersiz.
RabbitMQ provides some kind of support for retries, out of the box, but not with delay. When consuming a message the client declares whether to use auto acknowledgement or not. Auto acknowledgement means the broker can discard messages once delivered to the consumer (but before processing). Manual acknowledgement means the broker should wait for the client (consumer) to explicitly acknowledge when the message processing completed (successfully or not) and can be discarded. Note that there are no timeouts. The message will not be redelivered to a consumer, unless a negative acknowledgement is received or the connection is closed. When using automatic acknowledgements, retries due to application errors are practically not applicable. When using manual acknowledgements, a message can be redelivered by RabbitMQ if it is rejected by the consumer (meaning a negative acknowledgement is sent) and the flag requeue is set to true.
Observability
Açıklaması şöyle. Eğer tüm kuyrukları dinlemek istersek binding key olarak "#" kullanılır
Events history
In AMQP once a message is delivered (and optionally acknowledged), it is removed from a queue, and you will never see it again. What if you need to investigate what happened at that stage?
I can think of several options, for example:
- Enabling debug level of logging in message bus.
- RabbitMQ Firehose.
- Subscribing to all messages and storing/redirecting them to separate storage or message bus (might be done via custom subscriber or via tools like Apache Flink, Kafka RabbitMQ connector, etc.).

We have chosen the last option, which works the following way:
- There is a queue, which is subscribed to all messages in topic exchange via “#” binding key (which means matching any routing key);
- There are consumers, which constantly read messages from that queue and store it in the appropriate format to persistent storage.
Durable Queue
Açıklaması şöyle. Durable kuyruklar broker yeniden başlatılsa bile tekrar yaratılırlar. Eğer kuyruktaki mesajlar da persistent ise onlar da tekrar eklenirler.
Durable queues are persisted to disk and thus survive broker restarts. Queues that are not durable are called transient. Not all scenarios and use cases mandate queues to be durable.

Durability of a queue does not make messages that are routed to that queue durable. If broker is taken down and then brought back up, durable queue will be re-declared during broker startup, however, only persistent messages will be recovered.
Örnek
Şöyle yaparız. Queue durable, aynı zamanda auto_delete özelliği false, Böylece en son consumer kapansa dahi kuyruk silinmez.
curl --user guest:guest \
-X PUT -H 'content-type: application/json' \
--data-binary '{"vhost":"/","name":"test-queue-01","durable":"true","auto_delete":"false",
"arguments":{"x-queue-type":"classic"}}' \
'http://localhost:15672/api/queues/%2F/test-queue-01'
AMQP Frame Tipleri
AMQP Frame Tipler yazısına taşıdım.

High Availability
Açıklaması şöyle
RabbitMQ has provided clustering and highly available queues for several major versions. Version 3.8.0 shipped with “Quorum Queues” which use the Raft consensus algorithm to provide data replication with higher performance than “classic” HA queues.
Subscription Çeşitleri Nedir
İki çeşit subscription var.

1. Durable Subscription
Consumer çalışmasa bile elle silininceye kadar abonelik yaşar

2. Ephemeral subscription
Consumer kapatılınca subscription ve beklemekte olan mesajları silinir.

Exchange Çeşitleri
Exchange Çeşitleri yazısına taşıdım

Lazy Queue
Açıklaması şöyle
People that are trying out RabbitMQ are probably not aware of the the feature lazy queues. Lazy queues are queues where the messages are automatically stored to disk, thereby minimizing the RAM usage, but extending the throughput time. In our experience, lazy queues create a more stable cluster with better predictive performance. If you are sending a lot of messages at once (e.g. processing batch jobs), or if you think that your consumers will not consistently keep up with the speed of the publishers, we recommend that you enable lazy queues.
RabbitMQ Single Active Consumer (SAC)
Açıklaması şöyle. { "x-single-active-consumer", true } parametresi ile sağlıyoruz.
The Single Active Consumer mode allows to have only one consumer at a time consuming from a queue and to fail over to another registered consumer in case the active one is cancelled or dies.