You are previewing Spring Microservices.
O'Reilly logo
Spring Microservices

Book Description

Build scalable microservices with Spring, Docker, and Mesos

About This Book

  • Learn how to efficiently build and implement microservices in Spring, and how to use Docker and Mesos to push the boundaries of what you thought possible

  • Examine a number of real-world use cases and hands-on code examples.

  • Distribute your microservices in a completely new way

  • Who This Book Is For

    If you are a Spring developers and want to build cloud-ready, internet-scale applications to meet modern business demands, then this book is for you Developers will understand how to build simple Restful services and organically grow them to truly enterprise grade microservices ecosystems.

    What You Will Learn

  • Get to know the microservices development lifecycle process

  • See how to implement microservices governance

  • Familiarize yourself with the microservices architecture and its benefits

  • Use Spring Boot to develop microservices

  • Find out how to avoid common pitfalls when developing microservices

  • Be introduced to end-to-end microservices written in Spring Framework and Spring Boot

  • In Detail

    The Spring Framework is an application framework and inversion of the control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions to build web applications on top of the Java EE platform. This book will help you implement the microservice architecture in Spring Framework, Spring Boot, and Spring Cloud. Written to the latest specifications of Spring, you'll be able to build modern, Internet-scale Java applications in no time.

    We would start off with the guidelines to implement responsive microservices at scale. We will then deep dive into Spring Boot, Spring Cloud, Docker, Mesos, and Marathon. Next you will understand how Spring Boot is used to deploy autonomous services, server-less by removing the need to have a heavy-weight application server. Later you will learn how to go further by deploying your microservices to Docker and manage it with Mesos. By the end of the book, you'll will gain more clarity on how to implement microservices using Spring Framework and use them in Internet-scale deployments through real-world examples.

    Style and approach

    The book follows a step by step approach on how to develop microservices using Spring Framework, Spring Boot, and a set of Spring Cloud components that will help you scale your applications.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Spring Microservices
      1. Table of Contents
      2. Spring Microservices
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      8. 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
      9. 1. Demystifying Microservices
        1. The evolution of microservices
          1. Business demand as a catalyst for microservices evolution
          2. Technology as a catalyst for the microservices evolution
          3. Imperative architecture evolution
        2. What are microservices?
        3. Microservices – the honeycomb analogy
        4. Principles of microservices
          1. Single responsibility per service
          2. Microservices are autonomous
        5. Characteristics of microservices
          1. Services are first-class citizens
            1. Characteristics of services in a microservice
          2. Microservices are lightweight
          3. Microservices with polyglot architecture
          4. Automation in a microservices environment
          5. Microservices with a supporting ecosystem
          6. Microservices are distributed and dynamic
          7. Antifragility, fail fast, and self-healing
        6. Microservices examples
          1. An example of a holiday portal
          2. A microservice-based order management system
          3. An example of a travel agent portal
        7. Microservices benefits
          1. Supports polyglot architecture
          2. Enabling experimentation and innovation
          3. Elastically and selectively scalable
          4. Allowing substitution
          5. Enabling to build organic systems
          6. Helping reducing technology debt
          7. Allowing the coexistence of different versions
          8. Supporting the building of self-organizing systems
          9. Supporting event-driven architecture
          10. Enabling DevOps
        8. Relationship with other architecture styles
          1. Relations with SOA
            1. Service-oriented integration
            2. Legacy modernization
            3. Service-oriented application
            4. Monolithic migration using SOA
          2. Relations with Twelve-Factor apps
            1. A single code base
            2. Bundling dependencies
            3. Externalizing configurations
            4. Backing services are addressable
            5. Isolation between build, release, and run
            6. Stateless, shared nothing processes
            7. Exposing services through port bindings
            8. Concurrency to scale out
            9. Disposability with minimal overhead
            10. Development and production parity
            11. Externalizing logs
            12. Package admin processes
        9. Microservice use cases
          1. Microservices early adopters
          2. The common theme is monolithic migrations
        10. Summary
      10. 2. Building Microservices with Spring Boot
        1. Setting up a development environment
        2. Developing a RESTful service – the legacy approach
        3. Moving from traditional web applications to microservices
        4. Using Spring Boot to build RESTful microservices
        5. Getting started with Spring Boot
        6. Developing the Spring Boot microservice using the CLI
        7. Developing the Spring Boot Java microservice using STS
          1. Examining the POM file
          2. Examining
          3. Examining
          4. Examining
          5. Testing the Spring Boot microservice
        8. Developing the Spring Boot microservice using Spring Initializr – the HATEOAS example
        9. What's next?
        10. The Spring Boot configuration
          1. Understanding the Spring Boot autoconfiguration
          2. Overriding default configuration values
          3. Changing the location of the configuration file
          4. Reading custom properties
          5. Using a .yaml file for configuration
          6. Using multiple configuration profiles
          7. Other options to read properties
        11. Changing the default embedded web server
        12. Implementing Spring Boot security
          1. Securing microservices with basic security
          2. Securing a microservice with OAuth2
        13. Enabling cross-origin access for microservices
        14. Implementing Spring Boot messaging
        15. Developing a comprehensive microservice example
        16. Spring Boot actuators
          1. Monitoring using JConsole
          2. Monitoring using SSH
        17. Configuring application information
        18. Adding a custom health module
          1. Building custom metrics
        19. Documenting microservices
        20. Summary
      11. 3. Applying Microservices Concepts
        1. Patterns and common design decisions
          1. Establishing appropriate microservice boundaries
            1. Autonomous functions
            2. Size of a deployable unit
            3. Most appropriate function or subdomain
            4. Polyglot architecture
            5. Selective scaling
            6. Small, agile teams
            7. Single responsibility
            8. Replicability or changeability
            9. Coupling and cohesion
            10. Think microservice as a product
          2. Designing communication styles
            1. Synchronous style communication
            2. Asynchronous style communication
            3. How to decide which style to choose?
          3. Orchestration of microservices
          4. How many endpoints in a microservice?
          5. One microservice per VM or multiple?
          6. Rules engine – shared or embedded?
          7. Role of BPM and workflows
          8. Can microservices share data stores?
          9. Setting up transaction boundaries
            1. Altering use cases to simplify transactional requirements
            2. Distributed transaction scenarios
          10. Service endpoint design consideration
            1. Contract design
            2. Protocol selection
              1. Message-oriented services
              2. HTTP and REST endpoints
              3. Optimized communication protocols
              4. API documentations
          11. Handling shared libraries
          12. User interfaces in microservices
          13. Use of API gateways in microservices
          14. Use of ESB and iPaaS with microservices
          15. Service versioning considerations
          16. Design for cross origin
          17. Handling shared reference data
          18. Microservices and bulk operations
        2. Microservices challenges
          1. Data islands
          2. Logging and monitoring
          3. Dependency management
          4. Organization culture
          5. Governance challenges
          6. Operation overheads
          7. Testing microservices
          8. Infrastructure provisioning
        3. The microservices capability model
          1. Core capabilities
          2. Infrastructure capabilities
          3. Supporting capabilities
          4. Process and governance capabilities
        4. Summary
      12. 4. Microservices Evolution – A Case Study
        1. Reviewing the microservices capability model
        2. Understanding the PSS application
          1. Business process view
          2. Functional view
          3. Architectural view
          4. Design view
          5. Implementation view
          6. Deployment view
        3. Death of the monolith
          1. Pain points
          2. Stop gap fix
          3. Retrospection
            1. Shared data
            2. Single database
              1. Native queries
              2. Stored procedures
            3. Domain boundaries
        4. Microservices to the rescue
        5. The business case
        6. Plan the evolution
          1. Evolutionary approach
          2. Identification of microservices boundaries
            1. Analyze dependencies
              1. Events as opposed to query
              2. Events as opposed to synchronous updates
              3. Challenge requirements
              4. Challenge service boundaries
              5. Final dependency graph
          3. Prioritizing microservices for migration
          4. Data synchronization during migration
          5. Managing reference data
          6. User interfaces and web applications
            1. Session handling and security
          7. Test strategy
          8. Building ecosystem capabilities
        7. Migrate modules only if required
        8. Target architecture
          1. Internal layering of microservices
          2. Orchestrating microservices
          3. Integration with other systems
          4. Managing shared libraries
          5. Handling exceptions
        9. Target implementation view
          1. Implementation projects
          2. Running and testing the project
        10. Summary
      13. 5. Scaling Microservices with Spring Cloud
        1. Reviewing microservices capabilities
        2. Reviewing BrownField's PSS implementation
        3. What is Spring Cloud?
          1. Spring Cloud releases
          2. Components of Spring Cloud
            1. Spring Cloud and Netflix OSS
        4. Setting up the environment for BrownField PSS
        5. Spring Cloud Config
          1. What's next?
          2. Setting up the Config server
          3. Understanding the Config server URL
            1. Accessing the Config Server from clients
          4. Handling configuration changes
          5. Spring Cloud Bus for propagating configuration changes
          6. Setting up high availability for the Config server
          7. Monitoring the Config server health
          8. Config server for configuration files
          9. Completing changes to use the Config server
        6. Feign as a declarative REST client
        7. Ribbon for load balancing
        8. Eureka for registration and discovery
          1. Understanding dynamic service registration and discovery
          2. Understanding Eureka
          3. Setting up the Eureka server
          4. High availability for Eureka
        9. Zuul proxy as the API gateway
          1. Setting up Zuul
          2. High availability of Zuul
            1. High availability of Zuul when the client is also a Eureka client
            2. High availability when the client is not a Eureka client
          3. Completing Zuul for all other services
        10. Streams for reactive microservices
        11. Summarizing the BrownField PSS architecture
        12. Summary
      14. 6. Autoscaling Microservices
        1. Reviewing the microservice capability model
        2. Scaling microservices with Spring Cloud
        3. Understanding the concept of autoscaling
          1. The benefits of autoscaling
          2. Different autoscaling models
            1. Autoscaling an application
            2. Autoscaling the infrastructure
          3. Autoscaling in the cloud
        4. Autoscaling approaches
          1. Scaling with resource constraints
          2. Scaling during specific time periods
          3. Scaling based on the message queue length
          4. Scaling based on business parameters
          5. Predictive autoscaling
        5. Autoscaling BrownField PSS microservices
          1. The capabilities required for an autoscaling system
          2. Implementing a custom life cycle manager using Spring Boot
          3. Understanding the deployment topology
          4. Understanding the execution flow
          5. A walkthrough of the life cycle manager code
          6. Running the life cycle manager
        6. Summary
      15. 7. Logging and Monitoring Microservices
        1. Reviewing the microservice capability model
        2. Understanding log management challenges
        3. A centralized logging solution
        4. The selection of logging solutions
          1. Cloud services
          2. Off-the-shelf solutions
          3. Best-of-breed integration
            1. Log shippers
            2. Log stream processors
            3. Log storage
            4. Dashboards
          4. A custom logging implementation
          5. Distributed tracing with Spring Cloud Sleuth
        5. Monitoring microservices
          1. Monitoring challenges
          2. Monitoring tools
          3. Monitoring microservice dependencies
          4. Spring Cloud Hystrix for fault-tolerant microservices
          5. Aggregating Hystrix streams with Turbine
        6. Data analysis using data lakes
        7. Summary
      16. 8. Containerizing Microservices with Docker
        1. Reviewing the microservice capability model
        2. Understanding the gaps in BrownField PSS microservices
        3. What are containers?
        4. The difference between VMs and containers
        5. The benefits of containers
        6. Microservices and containers
        7. Introduction to Docker
          1. The key components of Docker
            1. The Docker daemon
            2. The Docker client
          2. Docker concepts
            1. Docker images
            2. Docker containers
            3. The Docker registry
            4. Dockerfile
        8. Deploying microservices in Docker
        9. Running RabbitMQ on Docker
        10. Using the Docker registry
          1. Setting up the Docker Hub
          2. Publishing microservices to the Docker Hub
        11. Microservices on the cloud
          1. Installing Docker on AWS EC2
        12. Running BrownField services on EC2
        13. Updating the life cycle manager
        14. The future of containerization – unikernels and hardened security
        15. Summary
      17. 9. Managing Dockerized Microservices with Mesos and Marathon
        1. Reviewing the microservice capability model
        2. The missing pieces
        3. Why cluster management is important
        4. What does cluster management do?
        5. Relationship with microservices
        6. Relationship with virtualization
        7. Cluster management solutions
          1. Docker Swarm
          2. Kubernetes
          3. Apache Mesos
          4. Nomad
          5. Fleet
        8. Cluster management with Mesos and Marathon
          1. Diving deep into Mesos
            1. The Mesos architecture
            2. Marathon
        9. Implementing Mesos and Marathon for BrownField microservices
          1. Setting up AWS
          2. Installing ZooKeeper, Mesos, and Marathon
            1. Configuring ZooKeeper
            2. Configuring Mesos
            3. Running Mesos, Marathon, and ZooKeeper as services
              1. Running the Mesos slave in the command line
            4. Preparing BrownField PSS services
            5. Deploying BrownField PSS services
            6. Reviewing the deployment
        10. A place for the life cycle manager
          1. Rewriting the life cycle manager with Mesos and Marathon
        11. The technology metamodel
        12. Summary
      18. 10. The Microservices Development Life Cycle
        1. Reviewing the microservice capability model
        2. The new mantra of lean IT – DevOps
          1. Reducing wastage
          2. Automating every possible step
          3. Value-driven delivery
          4. Bridging development and operations
        3. Meeting the trio – microservices, DevOps, and cloud
          1. Cloud as the self-service infrastructure for Microservices
          2. DevOps as the practice and process for microservices
        4. Practice points for microservices development
          1. Understanding business motivation and value
          2. Changing the mindset from project to product development
          3. Choosing a development philosophy
            1. Design thinking
            2. The start-up model
            3. The Agile practice
          4. Using the concept of Minimum Viable Product
          5. Overcoming the legacy hotspot
          6. Addressing challenges around databases
          7. Establishing self-organizing teams
          8. Building a self-service cloud
          9. Building a microservices ecosystem
          10. Defining a DevOps-style microservice life cycle process
            1. Value-driven planning
            2. Agile development
            3. Continuous integration
            4. Continuous testing
            5. Continuous release
            6. Continuous monitoring and feedback
          11. Automating the continuous delivery pipeline
            1. Development
            2. Continuous integration
            3. Automated testing
              1. Different candidate tests for automation
                1. Automated sanity tests
                2. Regression testing
                3. Automated functional testing
                4. Automated acceptance testing
                5. Performance testing
                6. Real user flow simulation or journey testing
                7. Automated security testing
                8. Exploratory testing
                9. A/B testing, canary testing, and blue-green deployments
                10. Other nonfunctional tests
                11. Testing in production
                12. Antifragility testing
              2. Target test environments
            4. Continuous deployment
            5. Monitoring and feedback
          12. Automated configuration management
        5. Microservices development governance, reference architectures, and libraries
        6. Summary
      19. Index