Share
Embed
PHP Microservice Development by the Example of a Mail Service
This talk was given on 2016-09-29 for the PHP Usergroup Munich at Scandio.
The main topic of this talk are queue systems in various PHP frameworks and how they differ.
Max Kleucker
September 29, 2016
Transcript
38 slides
-
PHP Microservice Development by the Example of a Mail Service
PHP UG Munich / 2016-09-28 / Max Kleucker / Scandio GmbH -
Application 1 Ƙ { Mailer Service REST Endpoint Business Logic
Mail Delivery SMTP -
Why a Queue? • Substituting the Mail Delivery with a
Mail Catcher • Additional Delivery mechanisms • Asynchronous if there are larger mail batches to be sent -
$connection = new AMQPStreamConnection('rabbitmq', 5672, 'guest', 'guest'); $channel = $connection->channel();
$channel->queue_declare('send_mail', false, false, false, false); $msg = new AMQPMessage('{"property": "value"}'); $channel->basic_publish($msg, '', 'send_mail'); $channel->close(); $connection->close(); PHP Enqueue a Message -
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel();
$channel->queue_declare('send_mail', false, false, false, false); $callback = function($msg) { echo "Received ". $msg->body. "\n"; }; $channel->basic_consume('send_mail', '', false, true, false, false, $callback); while(count($channel->callbacks)) { $channel->wait(); } PHP Process a Message -
Laravel → RabbitMQ # .env QUEUE_DRIVER=rabbitmq RABBITMQ_HOST=rabbit1 RABBITMQ_PORT=5672 RABBITMQ_VHOST=/ RABBITMQ_LOGIN=guest
RABBITMQ_PASSWORD=guest RABBITMQ_QUEUE=mailqueue Laravel -
Job Definition namespace App\Jobs; class SendEmail implements ShouldQueue { use
InteractsWithQueue, Queueable, SerializesModels; protected $mail; public function __construct(Mail $mail) { $this->mail = $mail; } } Laravel -
$mail = new \App\Mail(); $mail->receiver = '[email protected]'; $mail->subject = 'This
is an email!'; $mail->body = 'Moar awesome text.'; $job = new \App\Jobs\SendEmail($mail); dispatch($job->onQueue('mailqueue')); Laravel Enqueue a Message -
Laravel Enqueue a Message $mail = new \App\Mail(); $mail->receiver =
'[email protected]'; $mail->subject = 'This is an email!'; $mail->body = 'Moar awesome text.'; $job = new \App\Jobs\SendEmail($mail); dispatch($job->onQueue('mailqueue')); -
Laravel Enqueue a Message $mail = new \App\Mail(); $mail->receiver =
'[email protected]'; $mail->subject = 'This is an email!'; $mail->body = 'Moar awesome text.'; $job = new \App\Jobs\SendEmail($mail); dispatch($job->onQueue('mailqueue')); -
Laravel Enqueue a Message $mail = new \App\Mail(); $mail->receiver =
'[email protected]'; $mail->subject = 'This is an email!'; $mail->body = 'Moar awesome text.'; $job = new \App\Jobs\SendEmail($mail); dispatch($job->onQueue('mailqueue')); -
{ "job": "Illuminate\\Queue\\CallQueuedHandler@call", "data": { "commandName": "App\\Jobs\\SendEmail", "command": "O:18:\"App\\Jobs\\SendEmail\":5:{s: 7:\"\u0000*\u0000mail\";O:45:\"Illuminate\\Contracts\\Database\
\ModelIdentifier\":2:{s:5:\"class\";s:8:\"App\\Mail\";s:2:\"id\";N;}s: 6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";s: 9:\"mailqueue\";s:5:\"delay\";N;}" } } Laravel Message Payload -
Laravel Message Payload { "job": "Illuminate\\Queue\\CallQueuedHandler@call", "data": { "commandName": "App\\Jobs\\SendEmail",
"command": "O:18:\"App\\Jobs\\SendEmail\":5:{s: 7:\"\u0000*\u0000mail\";O:45:\"Illuminate\\Contracts\\Database\ \ModelIdentifier\":2:{s:5:\"class\";s:8:\"App\\Mail\";s:2:\"id\";N;}s: 6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";s: 9:\"mailqueue\";s:5:\"delay\";N;}" } } -
Laravel Process a Message class SendEmail implements ShouldQueue { use
InteractsWithQueue, Queueable, SerializesModels; protected $mail; public function __construct(Mail $mail) { $this->mail = $mail; } public function handle() { $payload = $this→mail; } } -
class SendEmail implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; protected
$mail; public function __construct(Mail $mail) { $this->mail = $mail; } public function handle() { $payload = $this->mail; } } $ php artisan queue:work Laravel Process a Job -
• Job-Object is used for creating and processing messages •
Difficult if you want to use it between different applications • Data is persisted in the database, not available in the message • Low configuration overhead, easy to use Laravel Summary -
Symfony Symfony → RabbitMQ # app/config/config.yml old_sound_rabbit_mq: connections: default: host:
'rabbitmq' port: 5672 user: 'guest' password: 'guest' vhost: '/' lazy: false connection_timeout: 3 read_write_timeout: 3 producers: send_mail: connection: default exchange_options: {name: 'send-mail', type: direct} queue_options: {name: 'send-mail'} consumers: send_mail: connection: default exchange_options: {name: 'send-mail', type: direct} queue_options: {name: 'send-mail'} callback: send_mail_service -
Symfony Enqueue a Message $mail = new Mail(); $mail->receiver =
'[email protected]'; $mail->subject = 'This is an email!'; $mail->body = 'Moar awesome text.'; $this->get('old_sound_rabbit_mq.send_mail_producer') ->publish(serialize($mail)); -
Symfony Enqueue a Message $mail = new Mail(); $mail->receiver =
'[email protected]'; $mail->subject = 'This is an email!'; $mail->body = 'Moar awesome text.'; $this->get('old_sound_rabbit_mq.send_mail_producer') ->publish(serialize($mail)); -
namespace AppBundle\Consumer; class SendMailConsumer implements ConsumerInterface { public function execute(AMQPMessage
$msg) { $payload = $msg->getBody(); } } Symfony Process a Message • Must be registered as Service in Symfony -
namespace AppBundle\Consumer; class SendMailConsumer implements ConsumerInterface { public function execute(AMQPMessage
$msg) { $payload = $msg->getBody(); } } Symfony Process a Message $ bin/console rabbitmq:consumer -w send_mail -
• More configuration necessary • Able to create and handle
"bare" messages • Better suited when talking with other software Symfony Summary -
Queues Summary • As always: Differentiation between comfort (Laravel) and
power (Vanilla PHP, Symfony) • Personal Opinion: Better to stay close to independent messages