Top Banner
Practical Message Queueing Using RabbitMQ James Titcumb PHPem 3rd July 2014
59

Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Aug 28, 2014

Download

Software

James Titcumb

RabbitMQ is a message broker - an application that allows communication between applications by way of a message queuing system. In this talk, we’ll set up an RabbitMQ instance, take an intermediate-level look into the technical features it provides and also how you can apply RabbitMQ in your in applications to scale them efficiently.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Practical Message Queueing Using

RabbitMQJames Titcumb

PHPem3rd July 2014

Page 2: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

James Titcumbwww.jamestitcumb.comwww.protected.co.ukwww.phphants.co.uk@asgrim

Who is this guy?

Page 3: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Who are you?

https://www.flickr.com/photos/akrabat/10168019755/

Page 4: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

What is message queueing?

Page 5: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Separation of Concerns

Page 6: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Scaling with Rabbit

RabbitMQApplication

Background processing

Page 7: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Page 8: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Background processing

Page 9: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Background processing

Background processing

Page 10: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Background processing

Background processing

Background processing

Page 11: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

● Fast logging solution

Some real world uses...

Page 12: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

● Fast logging solution● Background Processing

Some real world uses...

Page 13: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

● Fast logging solution● Background Processing

○ Sending emails

Some real world uses...

Page 14: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

● Fast logging solution● Background Processing

○ Sending emails○ Sending SMS

Some real world uses...

Page 15: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

● Fast logging solution● Background Processing

○ Sending emails○ Sending SMS○ Analytics, reporting

Some real world uses...

Page 16: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

(on precise64, other OSs may vary)

Installing RabbitMQ

Page 17: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

● 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

Page 18: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

http://localhost:15672/

Page 19: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Basic Message Queuing

Page 20: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Objective: Basic Queuing

Producer Consumer

test_queue

1 2 3 4 5

Page 21: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

composer.json{

"require": {

"videlalvaro/php-amqplib": "2.*"

}

}

then composer install

Page 22: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Please wait, connecting...use PhpAmqpLib\Connection\AMQPConnection;

$connection = new AMQPConnection(

'localhost',

5672,

'guest',

'guest',

'/'

);

$channel = $connection->channel();

Page 23: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

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');

Page 24: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

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();

}

Page 25: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

What to expect...

Page 26: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Exchanges: Fanout

Page 27: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Objective: Fanout Exchange

test_exchange

amq.KfgPZ3PE

amq.cK5Cp3FC

Consumer

Consumer

Producer

1

1

2

2

3

3

4

4

5

5

Page 28: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

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');

Page 29: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

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');

Page 30: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

What to expect...

Page 31: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

A word on Temporary Queues

● Only the “current” flow● Specific use cases

test_exchangeProducerMessages

go nowhere

Page 32: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Exchanges: Direct

Page 33: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Objective: Direct Exchange

test_direct

BK = apple

BK = banana, apple

Consumer

Consumer

Producer

3

Message Routing Keys1 = orange2 = banana3 = apple

2 3

BK = orange, banana, apple

Consumer1 2 3

Page 34: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

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);

Page 35: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

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');

Page 36: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

What to expect...

Page 37: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Exchanges: Topic

Page 38: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Objective:Topic Exchange

test_topic

BK = *.vegetable

BK = #

Consumer

Consumer

Producer

1 2

Message Routing Keys1 = red.vegetable2 = green.vegetable3 = green.fruit4 = red.meat5 = green.grass.long

1 2 3 4 5

BK = green.#

Consumer2 3 5

BK = *.grass.* / *.*.long

Consumer5

Page 39: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Real World Example

Page 40: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)
Page 41: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Fetch message

Logging Sequence

ApplicationBrowser Log Server

HTTP request

JSON via AMQP

Error!

HTTP response

RabbitMQ

Page 42: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Flexibility!

● Temporary Queues○ e.g. inspect “debug” messages

Page 43: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Flexibility!

● Temporary Queues● Queues to log to DB

Page 44: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Flexibility!

● Temporary Queues● Queues to log to DB● Queues to email “alert/emergency”

Page 45: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Flexibility!

● Temporary Queues● Queues to log to DB● Queues to email “alert/emergency”● Get creative with routing keys

○ RK = app.api.error … BK = #.api.error○ RK = app.form.debug … BK = #.debug

Page 46: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Problem: SPOF

Page 47: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Solution 1: Clustering

Page 48: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Clustering

RabbitMQNode 1

RabbitMQNode 3

RabbitMQNode 2

RabbitMQNode 4

RabbitMQNode 5

RabbitMQNode 6

Load Balance / Floating IP / Low TTL DNS etc.

Page 49: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Clustering

● Everything replicated, except queues

Page 50: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Clustering

● Everything replicated, except queues● Types:

○ RAM○ Disk

Page 51: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Clustering

● Everything replicated, except queues● Types:

○ RAM○ Disk

● Configuration:○ CLI (rabbitmqctl)○ Configuration files

Page 52: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Creating a clusternode1$ rabbitmqctl cluster_status

Cluster status of node rabbit@node1 ...

[{nodes,[{disc,[rabbit@node1]}]},{running_nodes,[rabbit@node1]}]

...done.

node2$ rabbitmqctl cluster_status

Cluster status of node rabbit@node2 ...

[{nodes,[{disc,[rabbit@node2]}]},{running_nodes,[rabbit@node2]}]

...done.

node3$ rabbitmqctl cluster_status

Cluster status of node rabbit@node3 ...

[{nodes,[{disc,[rabbit@node3]}]},{running_nodes,[rabbit@node3]}]

...done.

Page 53: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

node2$ rabbitmqctl join_cluster --ram rabbit@node1

node3$ rabbitmqctl join_cluster rabbit@node2

node3$ rabbitmqctl cluster_status

Cluster status of node rabbit@node3 ...

[{nodes,[{disc,[rabbit@node3,rabbit@node1]},{ram,[rabbit@node2]}]},

{running_nodes,[rabbit@node2,rabbit@node1,rabbit@node3]}]

...done.

Creating a cluster

Page 54: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

node1$ rabbitmqctl stop_app

node2$ rabbitmqctl forget_cluster_node rabbit@node1

node1$ rabbitmqctl reset

node1$ rabbitmqctl start_app

node2$ rabbitmqctl cluster_status

Cluster status of node rabbit@node2 ...

[{nodes,[{disc,[rabbit@node3]},{ram,[rabbit@node2]}]},

{running_nodes,[rabbit@node2,rabbit@node3]}]

...done.

Managing Nodes● Stopping/starting nodes● Removing nodes:

Page 55: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Solution 2: HA

Page 56: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

HA + Queue Mirroring

RabbitMQNode 1

RabbitMQNode 2

Load Balance / Floating IP / Low TTL DNS etc.

Page 57: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

https://github.com/asgrim/rmq-slides

Have a go yourself!

Page 58: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

Questions?

Page 59: Practical Message Queuing Using RabbitMQ (PHPem, 3rd July 2014)

James Titcumbwww.jamestitcumb.comwww.protected.co.ukwww.phphants.co.uk@asgrim

Thanks for watching!