42
What RabbitMQ Can Do For You James Titcumb PHPNW14 Unconference

What RabbitMQ can do for you (phpnw14 Uncon)

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

Page 1: What RabbitMQ can do for you (phpnw14 Uncon)

What RabbitMQCan Do For You

James TitcumbPHPNW14 Unconference

Page 2: What RabbitMQ can do for you (phpnw14 Uncon)

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

Who is this guy?

Page 3: What RabbitMQ can do for you (phpnw14 Uncon)

What is message queueing?

Page 4: What RabbitMQ can do for you (phpnw14 Uncon)

Separation of Concerns

Page 5: What RabbitMQ can do for you (phpnw14 Uncon)

Scaling with Rabbit

RabbitMQApplication

Background processing

Page 6: What RabbitMQ can do for you (phpnw14 Uncon)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Page 7: What RabbitMQ can do for you (phpnw14 Uncon)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Background processing

Page 8: What RabbitMQ can do for you (phpnw14 Uncon)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Background processing

Background processing

Page 9: What RabbitMQ can do for you (phpnw14 Uncon)

Scaling with Rabbit

RabbitMQApplication

Background processing

Background processing

Background processing

Background processing

Background processing

Page 10: What RabbitMQ can do for you (phpnw14 Uncon)

Some real world uses?

Page 11: What RabbitMQ can do for you (phpnw14 Uncon)

(on precise64, other OSs may vary)

Installing RabbitMQ

Page 12: What RabbitMQ can do for you (phpnw14 Uncon)

● 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 13: What RabbitMQ can do for you (phpnw14 Uncon)
Page 14: What RabbitMQ can do for you (phpnw14 Uncon)

Basic Message Queuing

Page 15: What RabbitMQ can do for you (phpnw14 Uncon)

Objective: Basic Queuing

Producer Consumer

test_queue

1 2 3 4 5

Page 16: What RabbitMQ can do for you (phpnw14 Uncon)

composer.json{

"require": {

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

}

}

then composer install

Page 17: What RabbitMQ can do for you (phpnw14 Uncon)

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

$connection = new AMQPConnection(

'localhost',

5672,

'guest',

'guest',

'/'

);

$channel = $connection->channel();

Page 18: What RabbitMQ can do for you (phpnw14 Uncon)

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 19: What RabbitMQ can do for you (phpnw14 Uncon)

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 20: What RabbitMQ can do for you (phpnw14 Uncon)

What to expect...

Page 21: What RabbitMQ can do for you (phpnw14 Uncon)

Exchanges: Fanout

Page 22: What RabbitMQ can do for you (phpnw14 Uncon)

Objective: Fanout Exchange

test_exchange

amq.KfgPZ3PE

amq.cK5Cp3FC

Consumer

Consumer

Producer

1

1

2

2

3

3

4

4

5

5

Page 23: What RabbitMQ can do for you (phpnw14 Uncon)

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 24: What RabbitMQ can do for you (phpnw14 Uncon)

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 25: What RabbitMQ can do for you (phpnw14 Uncon)
Page 26: What RabbitMQ can do for you (phpnw14 Uncon)

Temporary Queues

test_exchangeProducerMessages

go nowhere

Page 27: What RabbitMQ can do for you (phpnw14 Uncon)

Exchanges: Direct

Page 28: What RabbitMQ can do for you (phpnw14 Uncon)

Objective: Direct Exchange

test_direct

BK = apple

BK = banana, apple

Consumer

Consumer

Producer

BK = orange, banana, apple

Consumer

Page 29: What RabbitMQ can do for you (phpnw14 Uncon)

Objective: Direct Exchange

test_direct

BK = apple

BK = banana, apple

Consumer

Consumer

Producer

MESSAGEROUTING KEY= ORANGE

BK = orange, banana, apple

Consumer

Page 30: What RabbitMQ can do for you (phpnw14 Uncon)

Objective: Direct Exchange

test_direct

BK = apple

BK = banana, apple

Consumer

Consumer

Producer

MESSAGEROUTING KEY= BANANA

BK = orange, banana, apple

Consumer

Page 31: What RabbitMQ can do for you (phpnw14 Uncon)

Objective: Direct Exchange

test_direct

BK = apple

BK = banana, apple

Consumer

Consumer

Producer

MESSAGEROUTING KEY= APPLE

BK = orange, banana, apple

Consumer

Page 32: What RabbitMQ can do for you (phpnw14 Uncon)

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 33: What RabbitMQ can do for you (phpnw14 Uncon)

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 34: What RabbitMQ can do for you (phpnw14 Uncon)
Page 35: What RabbitMQ can do for you (phpnw14 Uncon)

Exchanges: Topic

Page 36: What RabbitMQ can do for you (phpnw14 Uncon)

Objective:Topic Exchange

test_topic

BK = *.vegetable

BK = #

Consumer

Consumer

ProducerBK = green.#

Consumer

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

Consumer

Page 37: What RabbitMQ can do for you (phpnw14 Uncon)

Objective:Topic Exchange

test_topic

BK = *.vegetable

BK = #

Consumer

Consumer

Producer

RED.VEGETABLE

BK = green.#

Consumer

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

Consumer

Page 38: What RabbitMQ can do for you (phpnw14 Uncon)

Objective:Topic Exchange

test_topic

BK = *.vegetable

BK = #

Consumer

Consumer

Producer

GREEN.VEGETABLE

BK = green.#

Consumer

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

Consumer

Page 39: What RabbitMQ can do for you (phpnw14 Uncon)

Objective:Topic Exchange

test_topic

BK = *.vegetable

BK = #

Consumer

Consumer

Producer

GREEN.GRASS.LONG

BK = green.#

Consumer

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

Consumer

Page 40: What RabbitMQ can do for you (phpnw14 Uncon)

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

Have a go yourself!

Page 41: What RabbitMQ can do for you (phpnw14 Uncon)

Questions?

Page 42: What RabbitMQ can do for you (phpnw14 Uncon)

James Titcumb@asgrim

Thanks for watching!