Setting up a Development Environment for Microservices
Total Page:16
File Type:pdf, Size:1020Kb
www.allitebooks.com 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 www.allitebooks.com 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 www.allitebooks.com Credits Authors Copy Editor Carlos Pérez Sánchez Shaila Kusanale Pablo Solar Vilariño Reviewers Project Coordinator Gabor Zelei Vaidehi Sawant Commissioning Editor Proofreader Aaron Lazar Safis Editing Acquisition Editor Indexer Divya Poojari Mariammal Chettiyar Content Development Editor Graphics Anurag Ghogre Jason Monteiro Technical Editor Production Coordinator Jijo Maliyekal Melwyn Dsa Subhalaxmi Nadar www.allitebooks.com 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. www.allitebooks.com 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 [email protected] 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 [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 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 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 [ ii ] 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 [ iii ] 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