PHP Microservices

Book description

Transit from monolithic architectures to highly available, scalable, and fault-tolerant microservices

About This Book

  • Build your own applications based on event-driven microservices and set them up on a production server.

  • Successfully transform any monolithic application into a microservice.

  • Monitor the health of your application, prevent downtime, and reduce costs.

  • Who This Book Is For

    PHP developers who want to build scalable, highly available, and secure applications will find this book useful. No knowledge of microservices is assumed.

    What You Will Learn

  • Set up a development environment using the right strategies and tools.

  • Learn about application design and structure to start implementing your application.

  • Transform a monolithic application into microservices.

  • Explore the best way to start implementing your application using testing.

  • Understand how to monitor your microservices, handle errors, and debug the application.

  • Deploy your finished application into a production environment and learn how to solve common problems.

  • Know how to scale your application based on microservices once it is up–and-running.

  • In Detail

    The world is moving away from bulky, unreliable, and high-maintenance PHP applications, to small, easy-to-maintain and highly available microservices and the pressing need is for PHP developers to understand the criticalities in building effective microservices that scale at large. This book will be a reliable resource, and one that will help you to develop your skills and teach you techniques for building reliable microservices in PHP.

    The book begins with an introduction to the world of microservices, and quickly shows you how to set up a development environment and build a basic platform using Docker and Vagrant. You will then get into the different design aspects to be considered while building microservices in your favorite framework and you will explore topics such as testing, securing, and deploying microservices. You will also understand how to migrate a monolithic application to the microservice architecture while keeping scalability and best practices in mind. Furthermore you will get into a few important DevOps techniques that will help you progress on to more complex domains such as native cloud development, as well as some interesting design patterns.

    By the end of this book you will be able to develop applications based on microservices in an organized and efficient way. You will also gain the knowledge to transform any monolithic applications into microservices.

    Style and approach

    Filled with code that you can start typing straightaway, this book will take you through building, testing, securing, and deploying microservices in the most practical way possible. The focus of the book is more inclined towards showing you how it’s done, rather than with what to do, although you will get a good idea of those tools most widely used to build microservices.

    Table of contents

    1. PHP Microservices
      1. PHP Microservices
      2. Credits
      3. About the Authors
      4. About the Reviewer
      5. www.PacktPub.com
        1. Why subscribe?
      6. Customer Feedback
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. What are Microservices?
        1. Monolithic versus microservices
        2. Service Oriented Architectures versus microservices
        3. Microservices characteristics
          1. Successful cases
          2. Disadvantages of microservices
        4. How to focus your development on microservices
          1. Always create small logical black boxes
          2. Network latency is your hidden enemy
          3. Always think about scalability
          4. Use a lightweight communication protocol
          5. Use queues to reduce a service load or make async executions
          6. Be ready for the worst case scenario
          7. Each service is different, so keep different repositories and build environments
        5. Advantages of using PHP on microservices
          1. A short history of PHP
          2. PHP milestones
            1. Version 4.x
            2. Version 5.x
            3. Version 6.x
            4. Version 7.x
          3. Advantages
          4. Disadvantages
        6. Summary
      9. 2. Development Environment
        1. Design and architecture to build the basic platform for microservices
        2. Requirements to start working on microservices
          1. Docker installation
            1. Installing Docker on macOS
              1. Docker for Mac (alias, native implementation) versus Docker toolbox
              2. Minimum requirements
              3. Docker for Mac installation process
            2. Installing Docker on Linux
              1. CentOS/RHEL
              2. Minimum requirements
              3. Installing Docker using yum
            3. Post-install setup - creating a Docker group
            4. Installing Docker on Ubuntu
              1. Minimum requirements
              2. Installing Docker using apt
              3. Common issues on Ubuntu
              4. UFW forwarding
              5. DNS server
            5. Post-install setup – creating a Docker group
              1. Starting Docker on boot
            6. Installing Docker on Windows
              1. Minimum requirements
              2. Installing the Docker tools
          2. How to check your Docker engine, compose, and machine versions
            1. Quick example to check your Docker installation
            2. Common management tasks
        3. Version control – Git versus SVN
          1. Git
          2. Hosting
            1. GitHub
            2. BitBucket
          3. Version control strategies
            1. Centralized work
            2. Feature branch workflow
            3. Gitflow workflow
            4. Forking workflow
            5. Semantic versioning
        4. Setting up a development environment for microservices
          1. Autodiscovery service
          2. Microservice base core - NGINX and PHP-FPM
        5. Frameworks for microservices
          1. PHP-FIG
          2. PSR-7
            1. Messages
            2. Headers
              1. Host header
              2. Streams
              3. Request targets and URIs
              4. Server-side requests
              5. Uploaded files
          3. Middleware
          4. Available frameworks
            1. Phalcon
            2. Slim framework
            3. Lumen
            4. Zend Expressive
            5. Silex (based on Symfony)
        6. Summary
      10. 3. Application Design
        1. Microservices structure
          1. Microservice patterns
          2. API gateway
          3. Service discovery and registry
          4. Shared database or database per service
            1. Database per service
            2. Shared database
        2. RESTful conventions
          1. Security
          2. Standards
          3. Consumer amenities
        3. Caching strategy
          1. General caching strategy
          2. HTTP caching
          3. Static files caching
        4. Domain-driven design
          1. How domain-driven design works
          2. Using domain-driver design in microservices
        5. Event-driven architecture
          1. Event-driven architecture in microservices
        6. Continuous integration, continuous delivery, and tools
          1. Continuous integration - CI
            1. What is continous integration?
            2. Benefits of CI
            3. Tools for continuous integration
          2. Continuous delivery 
            1. Benefits of continuous delivery
            2. Tools for a continuous delivery pipeline
        7. Summary
      11. 4. Testing and Quality Control
        1. The importance of using tests in your application
          1. Testing in microservices
        2. Test-driven development
          1. How to do TDD?
          2. Why should I use TDD?
          3. TDD algorithm
            1. Red - writing the unit tests
            2. Green - make the code work
            3. Refactor - eliminate redundancy
        3. Behavior-driven development
          1. What is BDD?
          2. How does it work?
            1. Cucumber as DSL for BDD
        4. Acceptance test-driven development
          1. User stories
          2. ATDD algorithm
            1. Discuss
            2. Distill
            3. Develop
            4. Demo
        5. Tools
          1. Composer
          2. PHPUnit
            1. Unit testing
            2. Running the tests
            3. Assertions
            4. assertArrayHasKey
            5. assertClassHasAttribute
            6. assertArraySubset
            7. assertClassHasStaticAttribute
            8. assertContains()
            9. assertDirectory() and assertFile()
            10. assertString()
            11. assertRegExp()
            12. assertJson()
            13. Boolean assertions
            14. Type assertions
            15. Other assertions
            16. Unit testing from scratch
          3. Behat
            1. Installation
            2. Test execution
            3. Behat example from scratch
          4. Selenium
            1. Selenium WebDriver
            2. Selenium IDE
        6. Summary
      12. 5. Microservices Development
        1. Dependency management
        2. Routing
          1. Postman
          2. Middleware
        3. Implementing a microservice call
          1. Request life cycle
          2. Communication between microservices with Guzzle
        4. Database operations
        5. Error handling
          1. Validation
          2. Manage exceptions
        6. Async and queue
        7. Caching
        8. Summary
      13. 6. Monitoring
        1. Debugging and profiling
          1. What is debugging?
          2. What is profiling?
          3. Debugging and profiling in PHP with Xdebug
            1. Debugging installation
            2. Debugging setup
            3. Debugging the output
            4. Profiling installation
            5. Profiling setup
            6. Analyzing the output file
        2. Error handling
          1. What is error handling?
          2. Why is error handling important?
          3. Challenges when managing error handling with microservices
            1. Basic die() function
            2. Custom error handling
              1. Report method
              2. Render method
            3. Error handling with Sentry
        3. Application logs
          1. Challenges in microservices
          2. Logs in Lumen
        4. Application monitoring
          1. Monitoring by levels
            1. Application level
              1. Datadog
            2. Infrastructure level
              1. Prometheus
              2. Weave Scope
            3. Hardware/hypervisor monitoring
        5. Summary
      14. 7. Security
        1. Encryption in microservices
          1. Database encryption
            1. Encryption in MariaDB
            2. InnoDB encryption
            3. Performance overhead
          2. TSL/SSL protocols
            1. How the TSL/SSL protocol works
          3. TSL/SSL termination
          4. TSL/SSL with NGINX
        2. Authentication
          1. OAuth 2
            1. How to use OAuth 2 on Lumen
              1. OAuth 2 installation
              2. Setup
              3. Let's try OAuth2
          2. JSON Web Token
            1. How to use JWT on Lumen
              1. Setting up JWT
              2. Let's try JWT
        3. Access Control List
          1. What is ACL?
          2. How to use ACL
        4. Security of the source code
          1. Environment variables
          2. External services
        5. Tracking and monitoring
        6. Best practices
          1. File permissions and ownership
          2. PHP execution locations
          3. Never trust users
          4. SQL injection
          5. Cross-site scripting XSS
          6. Session hijacking
          7. Remote files
          8. Password storage
          9. Password policies
          10. Source code revelation
          11. Directory traversal
        7. Summary
      15. 8. Deployment
        1. Dependency management
          1. Composer require-dev
          2. The .gitignore file
          3. Vendor folder
          4. Deployment workflows
            1. Vendor folder on repository
            2. Composer in production
          5. Frontend dependencies
            1. Grunt
            2. Gulp
            3. SASS
            4. Bower
        2. Deploy automation
          1. Simple PHP script
          2. Ansible and Ansistrano
            1. Ansible requirements
            2. Ansible installation
            3. What is Ansistrano?
            4. How does Ansistrano work?
            5. Deploying with Ansistrano
          3. Other deployment tools
        3. Advanced deployment techniques
          1. Continuous integration with Jenkins
          2. Blue/Green deployment
          3. Canary releases
          4. Immutable infrastructure
        4. Backup strategies
          1. What is backup?
          2. Why is it important?
          3. What and where we need to back up
          4. Backup types
            1. Full backup
            2. Incremental and differential backups
          5. Backup tools
            1. Bacula
            2. Percona xtrabackup
            3. Custom scripts
          6. Validating backups
          7. Be ready for the apocalypse
        5. Summary
      16. 9. From Monolithic to Microservices
        1. Refactor strategies
          1. Stop diving
          2. Divide frontend and backend
          3. Extraction services
            1. How to extract a module
        2. Tutorial: From monolithic to microservices
          1. Stop diving
          2. Divide frontend and backend
          3. Extraction services
        3. Summary
      17. 10. Strategies for Scalability
        1. Capacity planning
          1. Knowing the limits of your application
          2. Availability math
        2. Load testing
          1. Apache JMeter
            1. Installing Apache JMeter
            2. Executing load tests with Apache JMeter
          2. Load testing with Artillery
            1. Installing Artillery
            2. Executing loading tests with Artillery
            3. Creating Artillery scripts
            4. Advanced scripting
          3. Load testing with siege
            1. Installing siege on RHEL, CentOS, and similar operating systems
            2. Installing siege on Debian or Ubuntu
            3. Installing siege on other OS
            4. Quick siege example
        3. Scalability plan
          1. Step #0
          2. Step #1
          3. Step #2
          4. Step #3
          5. Step #4
          6. Step #5
        4. Summary
      18. 11. Best Practices and Conventions
        1. Code versioning best practices
        2. Caching best practices
          1. Performance impact
          2. Handle cache misses
          3. Group requests
          4. Size of elements to be stored in cache
          5. Monitor your cache
          6. Choose your cache algorithm carefully
        3. Performance best practices
          1. Minimize HTTP requests
          2. Minimize HTML, CSS, and JavaScript
          3. Image optimization
            1. Use sprites
            2. Use lossless image compression
            3. Scale your images
            4. Use data URIs
            5. Cache, cache, and more cache
            6. Avoid bad requests
            7. Use Content Delivery Networks (CDNs)
        4. Dependency management
        5. Semantic versioning
          1. How semantic versioning works
          2. Semantic versioning in action
            1. We have been told to add a new feature to the project
            2. We have been told that there is a bug in our project
            3. We have been asked for a big change
        6. Error handling
          1. Client request successful
          2. Request redirected
          3. Client request incomplete
          4. Server errors
        7. Coding practices
          1. Dealing with strings
          2. Single quotes versus double quotes
          3. Spaces versus tabs
          4. Regular expressions
          5. Connection and queries to a database
          6. Using the === operator
        8. Working with release branches
          1. Quick example
        9. Summary
      19. 12. Cloud and DevOps
        1. What is Cloud?
          1. Autoscalable and elastic
          2. Lower management efforts
          3. Cheaper
          4. Grow faster
          5. Time to market
        2. Select your Cloud provider
          1. Amazon Web Services (AWS)
          2. Microsoft Azure
          3. Rackspace
          4. DigitalOcean
          5. Joyent
          6. Google Compute Engine
        3. Deploying your application to the Cloud
          1. Docker Swarm
            1. Installing Docker Swarm
            2. Adding services to our Swarm
            3. Scaling your services in Swarm
          2. Apache Mesos and DC/OS
          3. Kubernetes
          4. Deploying to Joyent Triton
        4. What is DevOps?
        5. Summary

    Product information

    • Title: PHP Microservices
    • Author(s): Carlos Pérez Sánchez, Pablo Solar Vilariño
    • Release date: March 2017
    • Publisher(s): Packt Publishing
    • ISBN: 9781787125377