(This page has no text content)
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 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 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 h t t p s ://w w w . l i n k e d i n . c o m /i n /m r c a r l o s d e v . 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 mobile 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, and open source projects. Pablo can be approached online at h t t p s ://p a b l o s o l a r . e s /. 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 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 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 service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you 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 this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /d p /1787125378. If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. 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 7 Service Oriented Architectures versus microservices 10 Microservices characteristics 11 Successful cases 12 Disadvantages of microservices 13 How to focus your development on microservices 14 Always create small logical black boxes 14 Network latency is your hidden enemy 15 Always think about scalability 15 Use a lightweight communication protocol 15 Use queues to reduce a service load or make async executions 15 Be ready for the worst case scenario 16 Each service is different, so keep different repositories and build environments 16 Advantages of using PHP on microservices 16 A short history of PHP 17 PHP milestones 18 Version 4.x 18 Version 5.x 18 Version 6.x 18 Version 7.x 19 Advantages 19 Disadvantages 21 Summary 22 Chapter 2: Development Environment 23 Design and architecture to build the basic platform for microservices 24 Requirements to start working on microservices 25 Docker installation 26 Installing Docker on macOS 26 Docker for Mac (alias, native implementation) versus Docker toolbox 27 Minimum requirements 27 Docker for Mac installation process 27 Installing Docker on Linux 28
[ ii ] CentOS/RHEL 29 Minimum requirements 29 Installing Docker using yum 29 Post-install setup – creating a Docker group 30 Installing Docker on Ubuntu 30 Minimum requirements 31 Installing Docker using apt 31 Common issues on Ubuntu 32 UFW forwarding 33 DNS server 33 Post-install setup – creating a Docker group 34 Starting Docker on boot 34 Installing Docker on Windows 35 Minimum requirements 35 Installing the Docker tools 35 How to check your Docker engine, compose, and machine versions 37 Quick example to check your Docker installation 37 Common management tasks 38 Version control – Git versus SVN 39 Git 39 Hosting 42 GitHub 42 BitBucket 43 Version control strategies 43 Centralized work 43 Feature branch workflow 43 Gitflow workflow 44 Forking workflow 44 Semantic versioning 45 Setting up a development environment for microservices 45 Autodiscovery service 47 Microservice base core – NGINX and PHP-FPM 48 Frameworks for microservices 56 PHP-FIG 56 PSR-7 57 Messages 58 Headers 58 Host header 59 Streams 59 Request targets and URIs 59 Server-side requests 60 Uploaded files 60 Middleware 61 Available frameworks 65 Phalcon 65
[ iii ] Slim framework 66 Lumen 66 Zend Expressive 67 Silex (based on Symfony) 67 Summary 68 Chapter 3: Application Design 69 Microservices structure 69 Microservice patterns 71 API gateway 71 Service discovery and registry 72 Shared database or database per service 73 Database per service 73 Shared database 74 RESTful conventions 74 Security 75 Standards 75 Consumer amenities 76 Caching strategy 77 General caching strategy 77 HTTP caching 80 Static files caching 82 Domain-driven design 83 How domain-driven design works 83 Using domain-driver design in microservices 86 Event-driven architecture 87 Event-driven architecture in microservices 88 Continuous integration, continuous delivery, and tools 91 Continuous integration – CI 91 What is continous integration? 91 Benefits of CI 92 Tools for continuous integration 92 Continuous delivery 93 Benefits of continuous delivery 94 Tools for a continuous delivery pipeline 95 Summary 95 Chapter 4: Testing and Quality Control 96 The importance of using tests in your application 96 Testing in microservices 97 Test-driven development 98 How to do TDD? 99
[ iv ] Why should I use TDD? 100 TDD algorithm 102 Red – writing the unit tests 102 Green – make the code work 103 Refactor – eliminate redundancy 103 Behavior-driven development 104 What is BDD? 104 How does it work? 104 Cucumber as DSL for BDD 104 Acceptance test-driven development 106 User stories 107 ATDD algorithm 108 Discuss 108 Distill 109 Develop 109 Demo 109 Tools 110 Composer 110 PHPUnit 110 Unit testing 112 Running the tests 113 Assertions 114 assertArrayHasKey 114 assertClassHasAttribute 115 assertArraySubset 116 assertClassHasStaticAttribute 116 assertContains() 117 assertDirectory() and assertFile() 117 assertString() 118 assertRegExp() 119 assertJson() 119 Boolean assertions 120 Type assertions 120 Other assertions 120 Unit testing from scratch 121 Behat 128 Installation 128 Test execution 128 Behat example from scratch 129 Selenium 132 Selenium WebDriver 133 Selenium IDE 133 Summary 133 Chapter 5: Microservices Development 134
[ v ] Dependency management 134 Routing 136 Postman 139 Middleware 141 Implementing a microservice call 142 Request life cycle 145 Communication between microservices with Guzzle 146 Database operations 149 Error handling 158 Validation 158 Manage exceptions 161 Async and queue 164 Caching 167 Summary 171 Chapter 6: Monitoring 172 Debugging and profiling 172 What is debugging? 172 What is profiling? 173 Debugging and profiling in PHP with Xdebug 173 Debugging installation 173 Debugging setup 175 Debugging the output 176 Profiling installation 177 Profiling setup 178 Analyzing the output file 178 Error handling 179 What is error handling? 179 Why is error handling important? 179 Challenges when managing error handling with microservices 180 Basic die() function 180 Custom error handling 181 Report method 182 Render method 182 Error handling with Sentry 183 Application logs 188 Challenges in microservices 189 Logs in Lumen 190 Application monitoring 191 Monitoring by levels 192 Application level 192 Datadog 193
[ vi ] Infrastructure level 194 Prometheus 195 Weave Scope 198 Hardware/hypervisor monitoring 200 Summary 200 Chapter 7: Security 201 Encryption in microservices 201 Database encryption 202 Encryption in MariaDB 203 InnoDB encryption 205 Performance overhead 206 TSL/SSL protocols 206 How the TSL/SSL protocol works 207 TSL/SSL termination 208 TSL/SSL with NGINX 209 Authentication 211 OAuth 2 215 How to use OAuth 2 on Lumen 215 OAuth 2 installation 215 Setup 216 Let's try OAuth2 217 JSON Web Token 219 How to use JWT on Lumen 219 Setting up JWT 220 Let's try JWT 224 Access Control List 225 What is ACL? 225 How to use ACL 226 Security of the source code 228 Environment variables 228 External services 229 Tracking and monitoring 229 Best practices 230 File permissions and ownership 230 PHP execution locations 231 Never trust users 231 SQL injection 231 Cross-site scripting XSS 231 Session hijacking 231 Remote files 232 Password storage 232
[ vii ] Password policies 232 Source code revelation 233 Directory traversal 233 Summary 233 Chapter 8: Deployment 234 Dependency management 234 Composer require-dev 234 The .gitignore file 235 Vendor folder 235 Deployment workflows 235 Vendor folder on repository 235 Composer in production 236 Frontend dependencies 236 Grunt 236 Gulp 238 SASS 238 Bower 239 Deploy automation 240 Simple PHP script 240 Ansible and Ansistrano 241 Ansible requirements 242 Ansible installation 242 What is Ansistrano? 243 How does Ansistrano work? 243 Deploying with Ansistrano 245 Other deployment tools 247 Advanced deployment techniques 248 Continuous integration with Jenkins 248 Blue/Green deployment 249 Canary releases 250 Immutable infrastructure 251 Backup strategies 252 What is backup? 252 Why is it important? 252 What and where we need to back up 253 Backup types 253 Full backup 253 Incremental and differential backups 254 Backup tools 254 Bacula 254 Percona xtrabackup 255 Custom scripts 256
[ viii ] Validating backups 256 Be ready for the apocalypse 256 Summary 257 Chapter 9: From Monolithic to Microservices 258 Refactor strategies 258 Stop diving 258 Divide frontend and backend 260 Extraction services 262 How to extract a module 262 Tutorial: From monolithic to microservices 263 Stop diving 264 Divide frontend and backend 270 Extraction services 275 Summary 284 Chapter 10: Strategies for Scalability 285 Capacity planning 285 Knowing the limits of your application 286 Availability math 289 Load testing 292 Apache JMeter 292 Installing Apache JMeter 293 Executing load tests with Apache JMeter 294 Load testing with Artillery 302 Installing Artillery 302 Executing loading tests with Artillery 303 Creating Artillery scripts 305 Advanced scripting 306 Load testing with siege 307 Installing siege on RHEL, CentOS, and similar operating systems 307 Installing siege on Debian or Ubuntu 308 Installing siege on other OS 308 Quick siege example 308 Scalability plan 309 Step #0 311 Step #1 311 Step #2 312 Step #3 312 Step #4 313 Step #5 313 Summary 313
[ ix ] Chapter 11: Best Practices and Conventions 314 Code versioning best practices 314 Caching best practices 315 Performance impact 315 Handle cache misses 315 Group requests 316 Size of elements to be stored in cache 316 Monitor your cache 316 Choose your cache algorithm carefully 316 Performance best practices 317 Minimize HTTP requests 317 Minimize HTML, CSS, and JavaScript 318 Image optimization 319 Use sprites 319 Use lossless image compression 319 Scale your images 320 Use data URIs 320 Cache, cache, and more cache 321 Avoid bad requests 322 Use Content Delivery Networks (CDNs) 322 Dependency management 322 Semantic versioning 323 How semantic versioning works 324 Semantic versioning in action 324 We have been told to add a new feature to the project 324 We have been told that there is a bug in our project 324 We have been asked for a big change 325 Error handling 325 Client request successful 325 Request redirected 326 Client request incomplete 326 Server errors 327 Coding practices 327 Dealing with strings 327 Single quotes versus double quotes 328 Spaces versus tabs 328 Regular expressions 328 Connection and queries to a database 329 Using the === operator 329 Working with release branches 329 Quick example 330
[ x ] Summary 331 Chapter 12: Cloud and DevOps 332 What is Cloud? 332 Autoscalable and elastic 333 Lower management efforts 334 Cheaper 334 Grow faster 334 Time to market 335 Select your Cloud provider 335 Amazon Web Services (AWS) 335 Microsoft Azure 336 Rackspace 337 DigitalOcean 337 Joyent 338 Google Compute Engine 338 Deploying your application to the Cloud 339 Docker Swarm 339 Installing Docker Swarm 340 Adding services to our Swarm 343 Scaling your services in Swarm 346 Apache Mesos and DC/OS 347 Kubernetes 347 Deploying to Joyent Triton 349 What is DevOps? 350 Summary 352 Index 353
Preface Microservices are the new kids on the block. They are a way of solving problems, but it does not mean that they will always be the best solution. Microservices are a full and complex architecture that you need to understand deeply in order to be able to apply them successfully in the real world. We know that understanding and having everything in place for microservices can take time and can be very complex, so for this reason we wrote this book--to give you a small guide to microservices, from A to Z. This book will only be a glimpse of what you can do with microservices, but we hope that it will be at least a helpful start into this world. We will start our journey in this book by explaining the foundations of this architecture, the basic knowledge you will need to have. As you advance through the book, we will be increasing the level of difficulty. However, we will guide you through every step of the process, so at the end of the book you will be able to know whether a microservice architecture is the best solution for your problem and how can you apply this architecture. Enjoy your microservices! What this book covers Chapter 1, What are Microservices?, will teach you all the basics of microservices. Chapter 2, Development Environment, will take you through setting up your development machine to build microservices successfully. Chapter 3, Application Design, will help you start designing your application, creating the foundation of your project. Chapter 4, Testing and Quality Control, looks at how important testing your application is and how can you add tests to your application. Chapter 5, Microservices Development, will cover building a microservices application, explaining each step involved. Chapter 6, Monitoring, covers how to monitor your application so that you can always know how your application is behaving.
Preface [ 2 ] Chapter 7, Security, focuses on how you can add an extra layer of complexity to your application to make it more secure. Chapter 8, Deployment, explains how you can deploy your application successfully. Chapter 9, From Monolithic to Microservices, discusses an example of how a monolithic application can be transformed into a microservice. Chapter 10, Strategies for Scalability, outlines how you can create a scalable application. Chapter 11, Best Practices and Conventions, will refresh your knowledge about the best practices and conventions you should use in an application. Chapter 12, Cloud and DevOps, looks at the different cloud providers and the DevOps world. What you need for this book To follow along with this book, you’ll need a computer with an Internet connection to download the required software and Docker images. At some point, you will need at least a text editor or an IDE, and we highly recommend PHPStorm for this job. Who this book is for This book is for experienced PHP developers who want to go a step ahead in their careers and start building successful scalable and maintainable applications, all based on microservices. After reading the book, they will be able to know whether microservices are the best solutions to their problems and if it is the case, create a successful application. Conventions In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The next lines of code read the link and assign it to the to the BeautifulSoup function."
Comments 0
Loading comments...
Reply to Comment
Edit Comment