February 21, 2023 | Author: Anonymous | Category: N/A
PHP Microservices
Transit from monolithic architectures to highly available, scalable, and fault-tolerant microservices
Carlos Pérez Sánchez Pablo Solar Vilariño
BIRMINGHAM - MUMBAI
PHP Microservices Copyright © 2017 Packt Publishing All rights reserved. No part of this t his book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this t his information. First published: March 2017 Production reference: 1240317 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B32PB, UK.
ISBN 978-1-78712-537-7 www.packtpub.com
Credits Authors Carlos Pérez Sánchez Pablo Solar Vilariño
Copy Editor Shaila Kusanale
Reviewers Gabor Zelei
Project Coordinator Vaidehi Sawant
Commissioning Editor Aaron Lazar
Proofreader Safis Editing
Acquisition Editor Divya Poojari
Indexer Mariammal Chettiyar
Content Development Editor Anurag Ghogre
Graphics Jason Monteiro
Technical Editor Jijo Maliyekal Subhalaxmi Nadar
Production Coordinator Melwyn Dsa
About the Authors Carlos Pérez Sánchez is a backend web developer with more than 10 years of experience in working with the PHP language. He loves finding the best solution for every single problem on web applications and coding, looking to push forward the best practices for development, ensuring a high level of attention to detail.
He has a bachelors degree in computer engineering from the University of Alicante in Spain, and he has worked for different companies in the United Kingdom and Spain. He has also worked for American companies and is currently working for Pingvalue. You can connect with him on LinkedIn at https://www.linkedin.com/in/mrcarlosdev. To my girlfriend, Becca, family, and friends—thanks for your unconditional support in every single stage of my life.
Pablo Solar Vilariño is a software developer who became interested in web development when PHP 4 started becoming a popular language.
Over the last few years, he has worked extensively with web, cloud, and mobil mobilee technologies for medium-to-large companies and is currently an e-commerce developer at NITSNETS. He has a passion for new technologies, code standards, scalability, performance, performance, and open source projects. Pablo can be approached online at https://pablosolar.es/. To everyone who has ever believed in me.
About the Reviewer Gabor Zelei is a polyglot software engineer with a versatile background in both software engineering and operations. He’s had a decade-long love for PHP and LAMP/LEMP stacks in general; he enjoys working with upcoming technologies and methodologies and is a big fan of clean, well-structured and standards-compliant standards-compliant code.
During his career, he has worked for small startups as well as large enterprise companies. Currently, he lives in Dublin, Ireland, where he works as a senior software developer for a leading online marketplace company.
www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the t he eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
[email protected] for more details. At www.PacktPub.com , you can also also read a collection collection of free free technical articles, articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video video courses, as as well as industry-leading industry-leading tools to help you plan plan your personal development and advance your career.
Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on tthis his book's Amazon page at https://www.amazon.com/dp/1787125378. If you'd like to join our team of regular reviewers, you can e-mail us at
[email protected] . We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Table of Contents Preface
1
Chapter 1: What are Microservices?
6
Monolithic versus microservices Service Oriented Architectures versus microservices Microservices characteristics Successful cases Disadvantages of microservices How to focus your development on microservices Always create create small logical logical black boxes Network latency is your hidden enemy Always think think about scalability Use a lightweight communication protocol Use queues to reduce a service load or make async executions Be ready for the worst case scenario Each service is different, so keep different repositories and build environments Advantages of using PHP on microservices A short history history of PHP PHP PHP milestones Version 4.x Version 5.x Version 6.x Version 7.x
Advantages Advantages Disadvantages Summary
Chapter 2: Development Environment Design and architecture to build the basic platform for microservices Requirements to start working on microservices Docker installation Installing Docker on macOS Docker fo for M Ma ac (a (alias, na native im implementation) v ve ersus Do Docker tto oolbox Minimum requirements Docker for Mac installation process Installing Docker on Linux
7 10 11 12 13 14 14 15 15 15 15 16 16 16 17 18 18 18 18 19 19 21 22 23 24 25 26 26 27 27 27 28
CentOS/RHEL Minimum requirements Installing Docker using yum
29 29 29
Post-install setup – creating a Docker group Installing Docker on Ubuntu Minimum requirements Installing Docker using apt Common issues on Ubuntu UFW forwarding DNS server Post-install setup – creating a Docker group Starting Docker on boot Installing Docker on Windows Minimum requirements Installing the Docker tools
How to check your Docker engine, compose, and machine versions Quick example to check your Docker installation Common management tasks
Version control – Git versus SVN
30 30 31 31 32 33 33 34 34 35 35 35 37 37 38 39 39 42 42 43
Git Hosting GitHub BitBucket
Version control strategies
43 43 43 44 44 45
Centralized work Feature branch workflow Gitflow workflow Forking workflow Semantic versioning
Setting up a development environment for microservices Autodiscovery Autodisco very service Microservice base core – NGINX and PHP-FPM Frameworks for microservices
45 47 48 56 56 57 58 58 59 59 59 60 60
PHP-FIG PSR-7 Messages Headers Host header Streams Request targets and URIs Server-side requests Uploaded files
Middleware Available frameworks frameworks
61 65 65
Phalcon
[ ii ]
Slim framework Lumen Zend Expressive
66 66 67
Silex (based on Symfony)
67 68
Summary
Chapter 3: Application Design
69
Microservices structure Microservice patterns API gateway gateway Service discovery and registry Shared database or database per service Database per service Shared database
69 71 71 72 73 73 74
RESTful conventions Security Standards
74
Consumer amenities Caching strategy General caching strategy HTTP caching Static files caching Domain-driven design How domain-driven design works Using domain-driver design in microservices Event-driven architecture Event-driven architecture in microservices Continuous integration, continuous delivery, and tools Continuous integration – CI
76 77
75 75
What is continous integration? Benefits of CI Tools for continuous integration
Continuous delivery
Benefits of continuous delivery Tools for a continuous delivery pipeline
Summary
77 80 82 83 83 86 87 88 91 91 91 92 92 93 94 95 95
Chapter 4: Testing and Quality Control The importance of using tests in your application Testing in microservices Test-driven development How to do TDD?
[ iii ]
96 96 97 98 99
Why should I use TDD? TDD algorithm
100 102 102 103 103
Red – writing the unit tests Green – make the code work Refactor – eliminate redundancy
Behavior-driven development What is BDD? How does it work?
104 104 104 104
Cucumber as DSL for BDD
Acceptance test-driven development User stories ATDD algorithm algorithm Discuss Distill Develop Demo
Tools
106 107 108 108 109 109 109 110
Composer PHPUnit Unit testing Running the tests Assertions assertArrayHasKey assertClassHasAttribute assertArraySubset assertClassHasStaticAttribute assertContains() assertDirectory() and assertFile() assertString() assertRegExp() assertJson() Boolean assertions Type assertions Other assertions Unit testing from scratch
110 110 112 113 114 114 115 116 116 117 117 118 119 119 120 120 120 121
Behat
128 128 128 129
Installation Test execution Behat example from scratch
Selenium
132 133 133
Selenium WebDriver Selenium IDE
Summary
133
Chapter 5: Microservices Development
[ iv ]
134
Dependency management Routing
134
Postman Middleware Implementing a microservice call Request life cycle Communication between microservices with Guzzle Database operations Error handling Validation Manage exceptions Async and queue Caching Summary
139 141
136
Chapter 6: Monitoring
142 145 146 149 158 158 161 164 167 171 172
Debugging and profiling What is debugging? What is profiling? Debugging and profiling in PHP with Xdebug Debugging installation Debugging setup Debugging the output Profiling installation Profiling setup Analyzing the output file
Error handling What is error handling? Why is error handling important? Challenges when managing error handling with microservices
172 172 173 173 173 175 176 177 178 178 179 179 179
Basic die() function
180 180
Custom error handling Report method Render method Error handling with Sentry
181 182 182 183
Application logs Challenges Challenge s in microservices Logs in Lumen Application monitoring Monitoring by levels
188 189 190 191 192 192 193
Application level level Datadog
[v]
Infrastructure level Prometheus Weave Scope
194 195 198
Hardware/hypervisor monitoring
200 200
Summary
Chapter 7: Security
201
Encryption in microservices Database encryption
201 202 203 205 206
Encryption in MariaDB InnoDB encryption Performance overhead
TSL/SSL protocols How the TSL/SSL protocol works
TSL/SSL termination TSL/SSL with NGINX Authentication OAuth 2
206 207 208 209 211 215 215 215 216 217
How to use OAuth 2 on Lumen OAuth 2 installation Setup Let's try OAuth2
JSON Web Token
219 219 220 224
How to use JWT on Lumen Setting up JWT Let's try JWT
Access Control List What is ACL? How to use ACL Security of the source code Environment variables External services
225
Tracking and monitoring Best practices File permissions and ownership PHP execution locations Never trust users SQL injection Cross-site scripting XSS Session hijacking Remote files Password storage
229 230
225 226 228 228 229
[ vi ]
230 231 231 231 231 231 232 232
Password policies Source code revelation
232 233 233 233
Directory traversal Summary
Chapter 8: Deployment
234
Dependency management Composer require-dev The .gitignore file Vendor folder Deployment workflows
234 234 235 235 235 235 236
Vendor folder on repository Composer in production
Frontend dependencies
236 236 238 238
Grunt Gulp SASS Bower
239 240
Deploy automation Simple PHP script Ansible and and Ansistrano Ansistrano
240 241 242 242 243 243 245
Ansible requirements requirements Ansible installation installation What is Ansistrano? How does Ansistrano work? Deploying with Ansistrano
Other deployment tools Advanced deployment techniques Continuous integration with Jenkins Blue/Green deployment Canary releases Immutable infrastructure infrastructure Backup strategies What is backup? Why is it important? What and where we need to back up Backup types Full backup Incremental and differential backups
Backup tools
247 248 248 249 250 251 252 252 252 253 253 253 254 254 254 255 256
Bacula Percona xtrabackup Custom scripts
[ vii ]
Validating backups Be ready for the apocalypse
256 256
Summary
257 258
Refactor strategies Stop diving Divide frontend and backend Extraction services
258
Chapter 9: From Monolithic to Microservices
258 260 262 262
How to extract a module
Tutorial: From monolithic to microservices Stop diving Divide frontend and backend Extraction services Summary
Chapter 10: Strategies for Scalability Capacity planning Knowing the limits of your application Availability math Load testing Apache JMeter JMeter Installing Apache JMeter Executing load tests with Apache JMeter
Load testing with Artillery Installing Artillery Executing loading tests with Artillery Creating Artillery scripts Advanced scripting scripting
Load testing with siege Installing siege on RHEL, CentOS, and similar operating systems Installing siege on Debian or Ubuntu Installing siege on other OS Quick siege example
Scalability plan Step #0 Step #1 Step #2 Step #3 Step #4 Step #5 Summary
263 264 270 275 284 285 285 286 289 292 292 293 294 302 302 303 305 306 307 307 308 308 308 309 311 311 312 312 313 313 313
[ viii ]
Chapter 11: Best Practices and Conventions
314
Code versioning best practices
314
Caching best practices Performance impact Handle cache misses Group requests Size of elements to be stored in cache Monitor your cache Choose your cache algorithm carefully Performance best practices Minimize HTTP requests Minimize HTML, CSS, and JavaScript Image optimization
315 315
Use sprites Use lossless image compression Scale your images Use data URIs Cache, cache, and more cache Avoid bad requests requests Use Content Delivery Networks (CDNs)
Dependency management Semantic versioning How semantic versioning works Semantic versioning in action We have been told to add a new feature to the project We have been told that there is a bug in our project We have been asked for a big change
Error handling Client request successful Request redirected Client request incomplete Server errors Coding practices Dealing with strings Single quotes versus double quotes Spaces versus tabs Regular expressions Connection and queries to a database Using the === operator Working with release branches Quick example
[ ix ]
315 316 316 316 316 317 317 318 319 319 319 320 320 321 322 322 322 323 324 324 324 324 325 325 325 326 326 327 327 327 328 328 328 329 329 329 330
Summary
331
Chapter 12: Cloud and DevOps
332 332 333
What is Cloud? Autoscalable Autoscala ble and elastic Lower management efforts Cheaper Grow faster Time to market Select your Cloud provider Amazon Web Web Services Services (AWS) Microsoft Azure Rackspace DigitalOcean Joyent Google Compute Engine
334 334 334 335 335 335 336 337 337 338 338
Deploying your application to the Cloud Docker Swarm Installing Docker Swarm Adding services to our Swarm Scaling your services in Swarm
Apache Mesos Mesos and DC/OS Kubernetes Deploying to Joyent Triton What is DevOps? Summary
339 339 340 343 346 347 347 349 350 352
Index
353
[x]