Chapitre 2 Fond SYS Répartis
Chapitre 2 Fond SYS Répartis
52
Sofiane Ouni 53
Modèles des communications :
Publish-subscribe
54
Sofiane Ouni 55
56
Rabbit MQ
57
In 2018, it has been used for more than 35 000 companies
58
Advanced Message Queuing Protocol (AMQP)
• The Advanced Message Queuing Protocol (AMQP) is an open standard
application layer protocol for message-oriented middleware.
• The defining features of AMQP are message orientation, queuing, routing
(including point-to-point and publish-and-subscribe), reliability and
security
These are the known open protocol specifications that cover the same or
similar space as AMQP:
• Streaming Text Oriented Messaging Protocol (STOMP), a text-based protocol
developed at Codehaus; uses the JMS-like semantics of 'destination'.
• Extensible Messaging and Presence Protocol (XMPP), the Extensible Messaging and
Presence Protocol.
• MQTT, a lightweight publish-subscribe protocol.
• OpenWire as used by ActiveMQ.
60
1. The user sends a PDF creation request to the web application.
2. The web application (the producer) sends a message to RabbitMQ,
including data from the request, like name and email.
3. An exchange accepts the messages from a producer application and
routes them to correct message queues for PDF creation.
4. The PDF processing worker (the consumer) receives the task and starts
the processing of the PDF. 61
Message flow in RabbitMQ
1. The producer publishes a message to an
exchange. When you create the exchange,
you have to specify the type of it.
2. The exchange receives the message and is
now responsible for the routing of the
message. The exchange takes different
message attributes into account, such as
routing key, depending on the exchange
type.
3. Bindings have to be created from the
exchange to queues. In this case, we see
two bindings to two different queues from
the exchange. The Exchange routes the
message into the queues depending on
message attributes.
4. The messages stay in the queue until they
are handled by a consumer
5. The consumer handles the message.
62
63
Types of exchanges
• Direct: A direct exchange delivers messages to
queues based on a message routing key. In a
direct exchange, the message is routed to the
queues whose binding key exactly matches the
routing key of the message. If the queue is
bound to the exchange with the binding key
pdfprocess, a message published to the
exchange with a routing key pdfprocess is
routed to that queue.
• Fanout: A fanout exchange routes messages to
all of the queues that are bound to it.
• Topic: The topic exchange does a wildcard
match between the routing key and the
routing pattern specified in the binding.
• Headers: Headers exchanges use the message
header attributes for routing.
64
65
the message headers as opposed to the routing key, may occur in any
order, and may be specified as matching any or all of the specified
headers.
they are represented in the bindings as { "x-match", "any" ..} or
{ “x-match”, “all” ..}.
66
RabbitMQ and server concepts
• Producer: Application that sends the messages.
• Consumer: Application that receives the messages.
• Queue: Buffer that stores messages.
• Message: Information that is sent from the producer to a consumer through RabbitMQ.
• Connection: A connection is a TCP connection between your application and the RabbitMQ
broker.
• Channel: A channel is a virtual connection inside a connection. When you are publishing or
consuming messages from a queue - it's all done over a channel.
• Exchange: Receives messages from producers and pushes them to queues depending on rules
defined by the exchange type. To receive messages, a queue needs to be bound to at least one
exchange.
• Binding: A binding is a link between a queue and an exchange.
• Routing key: The routing key is a key that the exchange looks at to decide how to route the
message to queues. The routing key is like an address for the message.
• AMQP: AMQP (Advanced Message Queuing Protocol) is the protocol used by RabbitMQ for
messaging.
67
Send-receive RabbitMQ Simple
receive hello message
Send hello message
68
69
70
Lambda Expressions in Java 8 : interface implementation
71
Publish/subscribe pattern
fanout exchange
72
Publish/subscribe pattern : Fanout
73
Using Exchange, routing key, Binding Key
74
Emitting logs simple
• We'll use this model for our logging system. We will supply the log severity as a
routing key. That way the receiving program will be able to select the severity it
wants to receive.
• We assume that 'severity' can be one of 'info', 'warning', 'error'.
75
• we need to create an exchange first. we'll send messages to a direct
exchange :
76
EmitLogDirect info some_Informations
77
ReceiveLogsDirect info warning
78
Read message
without
DeliverCallBack
function
rabbitmq-java-client API
> 4.x.x
79
Example TD1
Réaliser un broker qui permet de classifier les messages
arrivant dans une file en deux files : une prioritaire et une autre
moins prioritaire.
80
81
Réception des messages prioritaires avant !
82
Header exchange simple
83
84
85
86
87
88
89
Acknowledgment in RabbitMQ
90
Delivery guarantee problem
• How to make the delivery
guarantee form publisher to
subscriber ?
• Delivery means that the
message is received for one or
may and processed correctly
91
Transaction support with RabbitMQ
A consumed message from an RabbitMQ AMQP queue,
an unexpected error occurred.
=> This will result in a state that the message was consumed from the
queue but it was not used for the intended purpose.
92
93
• setting autoAcknowledgement as false when consuming the
message, so that message remains in the queue and then start to
process the message.
• During processing, I’m checking for a condition. If that condition is
not satisfied I roll back the transaction, and if it is satisfied, I commit
the transaction by sending an acknowledgement.
94
ACK management
95
Acknowledging Multiple Deliveries at Once
• Manual acknowledgements can be batched to reduce network traffic. This is done by setting the multiple field of
acknowledgement methods (see above) to true.
• For example, given that there are delivery tags 5, 6, 7, and 8 unacknowledged on channel Ch, when an acknowledgement
frame arrives on that channel with delivery_tag set to 8 and multiple set to true, all tags from 5 to 8 will be acknowledged.
If multiple was set to false, deliveries 5, 6, and 7 would still be unacknowledged.
• To acknowledge multiple deliveries with RabbitMQ Java client, pass true for the multiple parameter to Channel#basicAck :
96
Negative Acknowledgement and Requeuing of Deliveries
• Sometimes a consumer cannot process a delivery immediately but other instances
might be able to. In this case it may be desired to requeue it and let another
consumer receive and handle it
97
Channel Prefetch Setting (QoS)
98
Security : TLS(Transport Layer Security) Support
99
This very basic example will show a simple client
connecting to a RabbitMQ server over TLS :
- Without validating the server certificate,
- and without presenting any client certificate
to the server.
100
https://www.rabbitmq.com/ssl.html
RabbitMQ Multi-tenant system and Access Control
• RabbitMQ is multi-tenant system:
• connections, exchanges, queues, bindings, user permissions, policies and some other things belong to
virtual hosts, logical groups of entities.
• virtual hosts : If you are familiar with virtual hosts in Apache or server blocks in Nginx, the idea is similar.
• Different users can be granted access only to specific virtual hosts. Their permissions in each virtual host
also can be limited.
1. Creating a vhost :
2. Adding a User:
https://en.wikipedia.org/wiki/Regular_expression
^ : Matches the starting position within the string.
$ : Matches the ending position of the string
101
https://stackoverflow.com/questions/7840283/how-can-queues-be-made-private-secure-in-rabbitmq-in-a-multitenancy-system
Static RabbitMQ Cluster Reference Architecture
• Each RabbitMQ broker runs on a Host node that could be a vm with
network connectivity to the other Host nodes in the cluster.
102
RabbitMQ in cluster
103
RabbitMQ Cluster running in Docker
Containers
• We can run the RabbitMQ brokers
clustered across Docker Containers on
a single host. One of the bigger
differences is that Docker is handling
network routing at the Host-level to
take traffic from a set of TCP ports:
5672, 5673, and 5674 and maps them
to each internal Container’s port
where the RabbitMQ broker listens on
TCP port 5672.
https://docs.docker.com/compose/gettingstarted/ 104
• docker run -d --hostname rabbit1 --name rabbit1 -e RABBITMQ_ERLANG_COOKIE='rabbitcluster' -
p 30000:5672 -p 30001:15672 rabbitmq:management
now there are three RabbitMQ running instances. We can go to the UI management console
for all of those instances available as docker containers, for example
http://192.168.99.100:30001 (rabbitmq). Each instance is available on its independent cluster
https://tanzu.vmware.com/developer/guides/messaging-and-integration/rabbitmq-gs/ 105
Reverse proxy for load distribution
• The idea here is to create a RabbitMQ cluster composed by 3 nodes, having a
reverse proxy sitting in front of the cluster distributing the load between the
nodes.
http://throughaglass.io/technology/RabbitMQ-cluster-with-Docker-and-Docker-Compose.html
106
Additional Features
• Routing: Directing messages to specific queues based on criteria.
• Persistence: Storing messages even if RabbitMQ restarts.
• Clustering: Scaling horizontally by distributing queues across nodes.
• Message Acknowledgement: Ensuring that messages are successfully processed.
• Priority Queues: Handling high-priority messages first.
• Plugins: Extending functionality as needed.
108
1-Réaliser le schéma représentant les files de messages et les exchanges de broker RabbitMQ. Dans ce cas
on suppose, l’utilisation du mode Fanout pour l’exchange. (l’hexagone pour représenter l’exchange, le
losange pour représenter une file). Pour la solution, on réalise une seule file par application ou module et
le mode fanout d’exchange par événement.
109
2- La même question que la précédente. Utiliser le mode topics pour l’exchange du broker RabbitMQ.
110
Other MOM (message-oriented middleware) :
ZeroMQ
• ZeroMQ is an open-source, high-performance messaging library.
• operates without a dedicated message broker
• ZeroMQ is a service provider of messaging. Using these providers, a
messaging API is required to send and receive messages.
• ZeroMQ is especially suited for high performance and low latency
scenarios.
111
Components of ZeroMQ
• Sockets: Using sockets, the user communicates with ZeroMQ. The
sockets are much like TCP sockets; the difference between them is
that each socket is able to handle multiple peer communication.
• Worker Thread: Various objects are residing in the worker thread.
112
Basis of Comparison ZeroMQ RabbitMQ
To implement
Developed by a broad AMQP(Advanced
Origin contributor group, which iMatix Messaging Queueing
created. Protocol).
113
114