© 2018 Rogue Wave Software, Inc. All Rights Reserved.
DEVELOPMICROSERVICESDEVELOPMICROSERVICES
INPHPINPHP
by
Senior Software Engineer
Inc.
, Milan (Italy), 30th Nov
Enrico Zimuel
Rogue Wave Software
Codemotion 2018
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
ABOUTMEABOUTME
Developer since 1996
Senior Software Engineer at
Inc.
Open source contributor ,
and
and international speaker
Professor at and
Research Programmer at
Co-founder of (Italy)
Rogue
Wave Software
Apigility
Expressive Zend Framework
TEDx
ITS-ICT Piemonte
Amsterdam
University
PUG Torino
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
BOOK:SVILUPPAREINPHP7BOOK:SVILUPPAREINPHP7
pp. 352, , 2017
ISBN 978-88-481-3120-9
in Italian
www.sviluppareinphp7.it
Tecniche Nuove
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
MICROSERVICEMICROSERVICE
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
...the microservice architectural style is an
approach to developing a single
application as a suite of small services,
each running in its own process and
communicating with lightweight
mechanisms, often an HTTP resource API
- Martin Fowler
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Source: Introduction to microservices
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Source: Introduction to microservices
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
BENEFITBENEFIT
Separation of concerns
Modularity
Encapsulation
Scalability
Horizontally scaling
Workload partitioning
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
CONSCONS
Network latency
Debugging
New architecture challenges:
Autodiscovery
Telemetry
Everything needs to be automated
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
PHPPHP
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
MICROSERVICESINPHPMICROSERVICESINPHP
PHP is easy to deploy
PHP 7 is super fast!
Big community
Libraries/Frameworks
Async in PHP (Swoole, ReactPHP, ...)
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
The PHP framework for middleware applications
PSR-7 support (using )
PSR-15 support and piping work ow (using
)
Features: routing, dependency injection, templating,
error handling
Support of out-of-the-box
zend-diactoros
zend-
stratigility
Swoole
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
ABASICWEBAPIABASICWEBAPI
use ZendDiactorosResponseJsonResponse;
use ZendExpressiveApplication;
$container = require 'config/container.php';
$app = $container->get(Application::class);
$app->pipe('/api/ping', function($request) {
return new JsonResponse(['ack' => time()]);
});
// $app->pipe('/api/ping', AppHandlerPingHandler::class);
$app->run();
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
REQUESTHANDLERREQUESTHANDLER
use PsrHttpMessageResponseInterface as Response; // PSR-7
use PsrHttpMessageServerRequestInterface as Request; // PSR-7
use PsrHttpServerRequestHandlerInterface as Handler; // PSR-15
use ZendDiactorosResponseJsonResponse;
class PingHandler implements Handler
{
public function handle(Request $request) : Response
{
return new JsonResponse(['ack' => time()]);
}
}
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
MIDDLEWAREMIDDLEWARE
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
MIDDLEWAREMIDDLEWARE
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
EXAMPLE:AUTHENTICATIONEXAMPLE:AUTHENTICATION
use PsrHttpServerMiddlewareInterface; // PSR-15
class AuthMiddleware implements MiddlewareInterface
{
public function process(Request $request, Handler $handler): Response
{
$user = $this->auth->authenticate($request);
if (null !== $user) {
return $handler->handle($request->withAttribute(
UserInterface::class,
$user
));
}
return $this->auth->unauthorizedResponse($request);
}
}
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
EXAMPLE:ROUTINGRESTAPIEXAMPLE:ROUTINGRESTAPI
$app->route('/api/users[/{id}]', [
AuthenticationAuthenticationMiddleware::class,
AuthorizationAuthorizationMiddleware::class,
ApiActionUserAction::class
], ['GET', 'POST', 'PATCH', 'DELETE'], 'api.users');
// or route each HTTP method
$app->get('/api/users[/{id}]', ..., 'api.users.get');
$app->post('/api/users', ..., 'api.users.post');
$app->patch('/api/users/{id}', ..., 'api.users.patch');
$app->delete('/api/users/{id}', ..., 'api.users.delete');
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
QUICKSTARTQUICKSTART
You can start using Expressive with :composer
composer create-project zendframework/zend-expressive-skeleton <dir>
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
LIBRARIESFORAPILIBRARIESFORAPI
HAL-JSON:
Problem details:
Filtering & validation:
Authentication (HTTP Basic, OAuth2):
Authorization (ACL, RBAC):
zend-expressive-hal
zend-problem-details
zend-input lter
zend-expressive-
authentication
zend-expressive-
authorization
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
SWOOLESWOOLE
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Swoole is an async programming framework for PHP 7
PHP extension, install:
Released under Apache license 2.0
More info at
pecl install swoole
swoole.co.uk
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
FEATURESFEATURES
Event-driven, asynchronous programming for PHP
Async TCP / UDP / HTTP / Websocket / HTTP2
client/server side API
IPv4 / IPv6 / Unixsocket / TCP/ UDP and SSL / TLS
support
and scalable
Fast serializer / unserializer
Milliseconds task scheduler
High performance
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
SWOOLEVS.PHP-FPMSWOOLEVS.PHP-FPM
Forks a number of worker processes based on CPU
core number
Fupports Long-live connections
Manage and reuse the status in memory
Executes Non-blocking code (async, coroutine)
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
HTTPSERVERHTTPSERVER
use SwooleHttpServer;
$http = new Server("127.0.0.1", 9501);
$http->on("start", function ($server) {
echo "Started at http://127.0.0.1:9501n";
});
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello Worldn");
});
$http->start();
Test: 16K req/sec on CPU i5-2500, 16 GB RAM, PHP 7.2.12, Swoole 4.2.9
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
EXPRESSIVEWITHSWOOLEEXPRESSIVEWITHSWOOLE
Install:
composer require zendframework/zend-expressive-swoole
Usage:
vendor/bin/zend-expressive-swoole start
Open your browser at localhost:8080
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
PHP+EXPRESSIVE+SWOOLEPHP+EXPRESSIVE+SWOOLE
Run a web application from CLI
Simplify the deploy (only 1 container)
A web server (nginx) can be used as load balancer
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
BENCHMARKBENCHMARK
2-4x faster than Nginx and Apache
Req/sec (mean)
Nginx 1418.23
Apache 1915.62
Swoole 4864.34
Testing environment:
Ubuntu 18.04, , PHP 7.2.12, Nginx 1.14 + FPM,
Apache 2.4.29 + mod_php, Swoole 4.2.9, CPU i5-2500, 16 GB RAM, HD SSD
Expressive Skeleton 3.2.3
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
REFERENCESREFERENCES
Michael Bryzek, , QCon 2018 talk
, QCon 2016 talk
Martin Fowler, , GOTO 2014 talk
, Nginx blog post
, Nginx ebook (free)
Richard Rodger, , Manning Pubblications, 2017
M.Amundsen, M.McLarty, R.Mitra, I.Nadareishvili,
, O'Reilly Media, 2016
Sam Newman, , O'Reilly Media, 2015
C.P.Sanchez, P.S.Vilariño, , Packt publisher, 2017
M.W.O'Phinney, E.Zimuel, , Zend ebook (free)
Design Microservice Architectures the Right Way
Mastering Chaos - A Net ix Guide to Microservices
Microservices
Introduction to Microservices
Designing and Deploying Microservices
The Tao of microservices
Microservice Architecture: Aligning
Principles, Practices, and Culture
Building Microservices: Designing Fine-Grained Systems
PHP Microservices
Expressive cookbook
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
THANKS!THANKS!
Contact me: enrico.zimuel (at) roguewave.com
Follow me: @ezimuel
This work is licensed under a
.
I used to make this presentation.
Creative Commons Attribution-ShareAlike 3.0 Unported License
reveal.js

Develop microservices in php

  • 1.
    © 2018 RogueWave Software, Inc. All Rights Reserved. DEVELOPMICROSERVICESDEVELOPMICROSERVICES INPHPINPHP by Senior Software Engineer Inc. , Milan (Italy), 30th Nov Enrico Zimuel Rogue Wave Software Codemotion 2018
  • 2.
    © 2018 RogueWave Software, Inc. All Rights Reserved. ABOUTMEABOUTME Developer since 1996 Senior Software Engineer at Inc. Open source contributor , and and international speaker Professor at and Research Programmer at Co-founder of (Italy) Rogue Wave Software Apigility Expressive Zend Framework TEDx ITS-ICT Piemonte Amsterdam University PUG Torino
  • 3.
    © 2018 RogueWave Software, Inc. All Rights Reserved. BOOK:SVILUPPAREINPHP7BOOK:SVILUPPAREINPHP7 pp. 352, , 2017 ISBN 978-88-481-3120-9 in Italian www.sviluppareinphp7.it Tecniche Nuove
  • 4.
    © 2018 RogueWave Software, Inc. All Rights Reserved. MICROSERVICEMICROSERVICE
  • 5.
    © 2018 RogueWave Software, Inc. All Rights Reserved. ...the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API - Martin Fowler
  • 6.
    © 2018 RogueWave Software, Inc. All Rights Reserved. Source: Introduction to microservices
  • 7.
    © 2018 RogueWave Software, Inc. All Rights Reserved. Source: Introduction to microservices
  • 8.
    © 2018 RogueWave Software, Inc. All Rights Reserved. BENEFITBENEFIT Separation of concerns Modularity Encapsulation Scalability Horizontally scaling Workload partitioning
  • 9.
    © 2018 RogueWave Software, Inc. All Rights Reserved. CONSCONS Network latency Debugging New architecture challenges: Autodiscovery Telemetry Everything needs to be automated
  • 10.
    © 2018 RogueWave Software, Inc. All Rights Reserved. PHPPHP
  • 11.
    © 2018 RogueWave Software, Inc. All Rights Reserved. MICROSERVICESINPHPMICROSERVICESINPHP PHP is easy to deploy PHP 7 is super fast! Big community Libraries/Frameworks Async in PHP (Swoole, ReactPHP, ...)
  • 12.
    © 2018 RogueWave Software, Inc. All Rights Reserved. The PHP framework for middleware applications PSR-7 support (using ) PSR-15 support and piping work ow (using ) Features: routing, dependency injection, templating, error handling Support of out-of-the-box zend-diactoros zend- stratigility Swoole
  • 13.
    © 2018 RogueWave Software, Inc. All Rights Reserved. ABASICWEBAPIABASICWEBAPI use ZendDiactorosResponseJsonResponse; use ZendExpressiveApplication; $container = require 'config/container.php'; $app = $container->get(Application::class); $app->pipe('/api/ping', function($request) { return new JsonResponse(['ack' => time()]); }); // $app->pipe('/api/ping', AppHandlerPingHandler::class); $app->run();
  • 14.
    © 2018 RogueWave Software, Inc. All Rights Reserved. REQUESTHANDLERREQUESTHANDLER use PsrHttpMessageResponseInterface as Response; // PSR-7 use PsrHttpMessageServerRequestInterface as Request; // PSR-7 use PsrHttpServerRequestHandlerInterface as Handler; // PSR-15 use ZendDiactorosResponseJsonResponse; class PingHandler implements Handler { public function handle(Request $request) : Response { return new JsonResponse(['ack' => time()]); } }
  • 15.
    © 2018 RogueWave Software, Inc. All Rights Reserved. MIDDLEWAREMIDDLEWARE
  • 16.
    © 2018 RogueWave Software, Inc. All Rights Reserved. MIDDLEWAREMIDDLEWARE
  • 17.
    © 2018 RogueWave Software, Inc. All Rights Reserved. EXAMPLE:AUTHENTICATIONEXAMPLE:AUTHENTICATION use PsrHttpServerMiddlewareInterface; // PSR-15 class AuthMiddleware implements MiddlewareInterface { public function process(Request $request, Handler $handler): Response { $user = $this->auth->authenticate($request); if (null !== $user) { return $handler->handle($request->withAttribute( UserInterface::class, $user )); } return $this->auth->unauthorizedResponse($request); } }
  • 18.
    © 2018 RogueWave Software, Inc. All Rights Reserved. EXAMPLE:ROUTINGRESTAPIEXAMPLE:ROUTINGRESTAPI $app->route('/api/users[/{id}]', [ AuthenticationAuthenticationMiddleware::class, AuthorizationAuthorizationMiddleware::class, ApiActionUserAction::class ], ['GET', 'POST', 'PATCH', 'DELETE'], 'api.users'); // or route each HTTP method $app->get('/api/users[/{id}]', ..., 'api.users.get'); $app->post('/api/users', ..., 'api.users.post'); $app->patch('/api/users/{id}', ..., 'api.users.patch'); $app->delete('/api/users/{id}', ..., 'api.users.delete');
  • 19.
    © 2018 RogueWave Software, Inc. All Rights Reserved. QUICKSTARTQUICKSTART You can start using Expressive with :composer composer create-project zendframework/zend-expressive-skeleton <dir>
  • 20.
    © 2018 RogueWave Software, Inc. All Rights Reserved. LIBRARIESFORAPILIBRARIESFORAPI HAL-JSON: Problem details: Filtering & validation: Authentication (HTTP Basic, OAuth2): Authorization (ACL, RBAC): zend-expressive-hal zend-problem-details zend-input lter zend-expressive- authentication zend-expressive- authorization
  • 21.
    © 2018 RogueWave Software, Inc. All Rights Reserved. SWOOLESWOOLE
  • 22.
    © 2018 RogueWave Software, Inc. All Rights Reserved. Swoole is an async programming framework for PHP 7 PHP extension, install: Released under Apache license 2.0 More info at pecl install swoole swoole.co.uk
  • 23.
    © 2018 RogueWave Software, Inc. All Rights Reserved. FEATURESFEATURES Event-driven, asynchronous programming for PHP Async TCP / UDP / HTTP / Websocket / HTTP2 client/server side API IPv4 / IPv6 / Unixsocket / TCP/ UDP and SSL / TLS support and scalable Fast serializer / unserializer Milliseconds task scheduler High performance
  • 24.
    © 2018 RogueWave Software, Inc. All Rights Reserved. SWOOLEVS.PHP-FPMSWOOLEVS.PHP-FPM Forks a number of worker processes based on CPU core number Fupports Long-live connections Manage and reuse the status in memory Executes Non-blocking code (async, coroutine)
  • 25.
    © 2018 RogueWave Software, Inc. All Rights Reserved. HTTPSERVERHTTPSERVER use SwooleHttpServer; $http = new Server("127.0.0.1", 9501); $http->on("start", function ($server) { echo "Started at http://127.0.0.1:9501n"; }); $http->on("request", function ($request, $response) { $response->header("Content-Type", "text/plain"); $response->end("Hello Worldn"); }); $http->start(); Test: 16K req/sec on CPU i5-2500, 16 GB RAM, PHP 7.2.12, Swoole 4.2.9
  • 26.
    © 2018 RogueWave Software, Inc. All Rights Reserved. EXPRESSIVEWITHSWOOLEEXPRESSIVEWITHSWOOLE Install: composer require zendframework/zend-expressive-swoole Usage: vendor/bin/zend-expressive-swoole start Open your browser at localhost:8080
  • 27.
    © 2018 RogueWave Software, Inc. All Rights Reserved. PHP+EXPRESSIVE+SWOOLEPHP+EXPRESSIVE+SWOOLE Run a web application from CLI Simplify the deploy (only 1 container) A web server (nginx) can be used as load balancer
  • 28.
    © 2018 RogueWave Software, Inc. All Rights Reserved. BENCHMARKBENCHMARK 2-4x faster than Nginx and Apache Req/sec (mean) Nginx 1418.23 Apache 1915.62 Swoole 4864.34 Testing environment: Ubuntu 18.04, , PHP 7.2.12, Nginx 1.14 + FPM, Apache 2.4.29 + mod_php, Swoole 4.2.9, CPU i5-2500, 16 GB RAM, HD SSD Expressive Skeleton 3.2.3
  • 29.
    © 2018 RogueWave Software, Inc. All Rights Reserved. REFERENCESREFERENCES Michael Bryzek, , QCon 2018 talk , QCon 2016 talk Martin Fowler, , GOTO 2014 talk , Nginx blog post , Nginx ebook (free) Richard Rodger, , Manning Pubblications, 2017 M.Amundsen, M.McLarty, R.Mitra, I.Nadareishvili, , O'Reilly Media, 2016 Sam Newman, , O'Reilly Media, 2015 C.P.Sanchez, P.S.Vilariño, , Packt publisher, 2017 M.W.O'Phinney, E.Zimuel, , Zend ebook (free) Design Microservice Architectures the Right Way Mastering Chaos - A Net ix Guide to Microservices Microservices Introduction to Microservices Designing and Deploying Microservices The Tao of microservices Microservice Architecture: Aligning Principles, Practices, and Culture Building Microservices: Designing Fine-Grained Systems PHP Microservices Expressive cookbook
  • 30.
    © 2018 RogueWave Software, Inc. All Rights Reserved. THANKS!THANKS! Contact me: enrico.zimuel (at) roguewave.com Follow me: @ezimuel This work is licensed under a . I used to make this presentation. Creative Commons Attribution-ShareAlike 3.0 Unported License reveal.js