Upload
james-titcumb
View
566
Download
4
Tags:
Embed Size (px)
DESCRIPTION
RabbitMQ is a message broker – an application that allows communication between applications by way of a message queuing system. In this talk, we look at some of the basic concepts of RabbitMQ and how it can help effectively scale your applications.
Citation preview
What RabbitMQCan Do For You
James TitcumbPHPNW14 Unconference
James Titcumbwww.jamestitcumb.comwww.protected.co.ukwww.phphants.co.uk@asgrim
Who is this guy?
What is message queueing?
Separation of Concerns
Scaling with Rabbit
RabbitMQApplication
Background processing
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Background processing
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Background processing
Background processing
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Background processing
Background processing
Background processing
Some real world uses?
(on precise64, other OSs may vary)
Installing RabbitMQ
● add apt repo○ deb http://www.rabbitmq.com/debian/ testing main
● add signing key○ http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
● apt-get update● apt-get install rabbitmq-server● rabbitmq-plugins enable rabbitmq_management● sudo service rabbitmq-server restart
Using Apt
Basic Message Queuing
Objective: Basic Queuing
Producer Consumer
test_queue
1 2 3 4 5
composer.json{
"require": {
"videlalvaro/php-amqplib": "2.*"
}
}
then composer install
Please wait, connecting...use PhpAmqpLib\Connection\AMQPConnection;
$connection = new AMQPConnection(
'localhost',
5672,
'guest',
'guest',
'/'
);
$channel = $connection->channel();
basic/producer.phpuse PhpAmqpLib\Message\AMQPMessage;
$channel->queue_declare(
'test_queue',
false,
true,
false, false);
$message = new AMQPMessage('my test message');
$channel->basic_publish($message, '', 'test_queue');
basic/consumer.php$channel->basic_consume(
'test_queue', // Queue to consume
'', // Consumer identifier
false,
true, // No-ack means messages are "auto acknowledged"
false, // Exclusive - no other consumers can use the queue
false,
function(AMQPMessage $message) {
echo $message->body . "\n";
}
);
while (count($channel->callbacks)) {
$channel->wait();
}
What to expect...
Exchanges: Fanout
Objective: Fanout Exchange
test_exchange
amq.KfgPZ3PE
amq.cK5Cp3FC
Consumer
Consumer
Producer
1
1
2
2
3
3
4
4
5
5
fanout/producer.phpuse PhpAmqpLib\Message\AMQPMessage;
$channel->exchange_declare(
'test_exchange',
'fanout',
false, false, false);
$message = new AMQPMessage('my test message #' . $id);
$channel->basic_publish($message, 'test_exchange');
fanout/consumer.php$q = $channel->queue_declare(
'', // Lets RabbitMQ pick a name for queue
false, false, false,
true // Delete this queue
);
$queue_name = $q[0];
$channel->exchange_declare(
'test_exchange', 'fanout', false, false, false);
$channel->queue_bind($queue_name, 'test_exchange');
Temporary Queues
test_exchangeProducerMessages
go nowhere
Exchanges: Direct
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
BK = orange, banana, apple
Consumer
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
MESSAGEROUTING KEY= ORANGE
BK = orange, banana, apple
Consumer
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
MESSAGEROUTING KEY= BANANA
BK = orange, banana, apple
Consumer
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
MESSAGEROUTING KEY= APPLE
BK = orange, banana, apple
Consumer
direct/producer.php$channel->exchange_declare(
'test_direct', 'fanout', false, false, false);
$messageContent = 'my test message, key=' . $routingKey;
$message = new AMQPMessage($messageContent);
$channel->basic_publish($message, 'test_direct', $routingKey);
direct/consumer.php$q = $channel->queue_declare('', false, false, false, true);
$queue_name = $q[0];
$channel->exchange_declare(
'test_direct', 'direct', false, false, false);
// Bind for each routing key we want (BINDING KEY)
$channel->queue_bind($queue_name, 'test_direct', 'apple');
$channel->queue_bind($queue_name, 'test_direct', 'orange');
$channel->queue_bind($queue_name, 'test_direct', 'banana');
Exchanges: Topic
Objective:Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
ProducerBK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
Objective:Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
RED.VEGETABLE
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
Objective:Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
GREEN.VEGETABLE
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
Objective:Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
GREEN.GRASS.LONG
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
https://github.com/asgrim/rmq-slides
Have a go yourself!
Questions?
James Titcumb@asgrim
Thanks for watching!