Building Microservices with Spring

Book Description

Learn and use the design patterns and best practices in Spring to solve common design problems and build user-friendly microservices

Key Features

  • Study the benefits of using the right design pattern in your toolkit
  • Manage your code easily with Spring's dependency injection pattern
  • Explore the features of Docker and Mesos to build successful microservices

Book Description

Getting Started with Spring Microservices begins with an overview of the Spring Framework 5.0, its design patterns, and its guidelines that enable you to implement responsive microservices at scale. You will learn how to use GoF patterns in application design. You will understand the dependency injection pattern, which is the main principle behind the decoupling process of the Spring Framework and makes it easier to manage your code. Then, you will learn how to use proxy patterns in aspect-oriented programming and remoting. Moving on, you will understand the JDBC template patterns and their use in abstracting database access.

After understanding the basics, you will move on to more advanced topics, such as reactive streams and concurrency. Written to the latest specifications of Spring that focuses on Reactive Programming, the Learning Path teaches you how to build modern, internet-scale Java applications in no time.

Next, you will understand how Spring Boot is used to deploying serverless autonomous services by removing the need to have a heavyweight application server. You'll also explore ways to deploy your microservices to Docker and managing them with Mesos.

By the end of this Learning Path, you will have the clarity and confidence for implementing microservices using Spring Framework.

This Learning Path includes content from the following Packt products:

  • Spring 5 Microservices by Rajesh R V
  • Spring 5 Design Patterns by Dinesh Rajput

What you will learn

  • Develop applications using dependency injection patterns
  • Build web applications using traditional Spring MVC patterns
  • Utilize the reactive programming pattern to build reactive web apps
  • Learn concurrency and handle multiple connections inside a web server
  • Use Spring Boot and Spring Cloud to develop microservices
  • Leverage reactive programming to build cloud-native applications

Who this book is for

Getting Started with Spring Microservices is ideal for Spring developers who want to use design patterns to solve common design problems and build cloud-ready, Internet-scale applications, and simple RESTful services.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright
    1. Building Microservices with Spring
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Getting Started with Spring Framework 5.0 and Design Patterns
    1. Introducing Spring Framework
    2. Simplifying application development using Spring and its pattern
      1. Using the power of the POJO pattern
      2. Injecting dependencies between POJOs
        1. How DI works and makes things easy for development and testing
        2. Using factory helper pattern for dependent components
      3. Using DI pattern for dependent components
      4. Applying aspects for cross cutting concerns
        1. How Spring AOP works
      5. Applying the template pattern to eliminate boilerplate code
        1. The Template Design pattern in Spring
    3. Using a Spring container to manage beans with the Factory pattern
      1. Bean factory
      2. Application contexts
      3. Creating a container with an application context
    4. Life of a bean in the container
    5. Spring modules
      1. Core Spring container
      2. Spring's AOP module
      3. Spring DAO - data access and integration
      4. Spring's ORM
      5. Spring web MVC
    6. New features in Spring Framework 5.0
    7. Summary
  7. Overview of GOF Design Patterns - Core Design Patterns
    1. Introducing the power of design patterns
    2. Common GoF Design Pattern overview
    3. Creational design patterns
      1. Factory design pattern
        1. Implementing the Factory design pattern in Spring Framework
        2. Sample implementation of the Factory design pattern
      2. Abstract factory design pattern
        1. Common problems where you should apply the Abstract factory design pattern
        2. Implementing the Abstract factory design pattern in the Spring Framework
        3. Sample implementation of the Abstract Factory design pattern
      3. Singleton design pattern
        1. Common problems where you should apply Singleton pattern
        2. Singleton design pattern implementation in the Spring Framework
        3. Sample implementation of the Singleton design pattern
      4. Prototype design pattern
        1. Benefits of the Prototype design pattern
        2. UML class structure
        3. Sample implementation of the Prototype design pattern
      5. Builder design pattern
        1. Benefits of the Builder pattern:
        2. UML class structure
        3. Implementing the Builder pattern in the Spring Framework
        4. Common problems where you should apply Builder pattern
        5. Sample implementation of the Builder design pattern
    4. Summary
  8. Wiring Beans using the Dependency Injection Pattern
    1. The dependency injection pattern
      1. Solving problems using the dependencies injection pattern
        1. Without dependency injection
        2. With dependency injection pattern
    2. Types of dependency injection patterns
      1. Constructor-based dependency injection pattern
      2. Setter-based dependency injection
    3. Configuring the dependency injection pattern with Spring
    4. Dependency injection pattern with Java-based configuration
      1. Creating a Java configuration class - AppConfig.java
        1. Declaring Spring beans into configuration class
        2. Injecting Spring beans
        3. Best approach to configure the dependency injection pattern with Java
    5. Dependency injection pattern with XML-based configuration
      1. Creating an XML configuration file
        1. Declaring Spring beans in an XML file
        2. Injecting Spring beans
          1. Using constructor injection
          2. Using setter injection
    6. Dependency injection pattern with Annotation-based configuration
      1. What are Stereotype annotations?
        1. Creating auto searchable beans using Stereotype annotations
        2. Searching beans using component scanning
        3. Annotating beans for autowiring
          1. Using @Autowired with setter method
          2. Using @Autowired with the fields
      2. The Autowiring DI pattern and disambiguation
        1. Resolving disambiguation in Autowiring DI pattern
        2. Implementing the Abstract Factory Pattern in Spring (FactoryBean interface)
          1. Implementation of FactoryBean interface in Spring
          2. Sample implementation of FactoryBean interface
    7. Best practices for configuring the DI pattern
    8. Summary
  9. Spring Aspect Oriented Programming with Proxy and Decorator pattern
    1. Proxy pattern in Spring
      1. Proxying classes using Decorator pattern in Spring
    2. What are cross-cutting concerns?
    3. What is Aspect-Oriented Programming?
      1. Problems resolved by AOP
        1. Code tangling
        2. Code scattering
      2. How AOP Works to solve problems
    4. Core AOP terminology and concepts
      1. Advice
      2. Join Point
      3. Pointcut
      4. Aspect
      5. Weaving
    5. Defining pointcuts
      1. Writing pointcuts
    6. Creating aspects
      1. Define aspects using Annotation
    7. Implementing Advice
      1. Advice type - Before
        1. Before Advice example
      2. Advice Types: After Returning
        1. After Returning Advice example
      3. Advice Types: After Throwing
        1. After Throwing Advice example
      4. Advice Types: After
        1. After Advice example
      5. Advice Types - Around
        1. Around Advice example
    8. Define aspects using XML configuration
    9. Understanding AOP proxies
    10. Summary
  10. Accessing a Database with Spring and JDBC Template Patterns
    1. The best approach to designing your data-access
      1. The resource management problem
      2. Implementing the template design pattern
        1. Problems with the traditional JDBC
        2. Solving problems with Spring's JdbcTemplate
    2. Configuring the data source and object pool pattern
      1. Configuring a data source using a JDBC driver
      2. Configuring the data source using pool connections
    3. Implementing the Builder pattern to create an embedded data source
      1. Abstracting database access using the DAO pattern
    4. The DAO pattern with the Spring Framework
      1. Working with JdbcTemplate
      2. When to use JdbcTemplate
        1. Creating a JdbcTemplate in an application
        2. Implementing a JDBC-based repository
        3. Jdbc callback interfaces
          1. Creating a RowMapper class
          2. Implementing RowCallbackHandler
          3. Implementing ResultSetExtractor
    5. Best practices for Jdbc and configuring JdbcTemplate
    6. Summary
  11. Improving Application Performance Using Caching Patterns
    1. What is cache?
      1. Where do we use caching?
    2. Understanding cache abstraction
    3. Enabling caching via the Proxy pattern
      1. Enabling the caching proxy using Annotation
      2. Enabling the Caching Proxy using the XML namespace
    4. Declarative Annotation-based caching
      1. The @Cacheable annotation
      2. The @CachePut annotation
        1. Customizing the cache key
        2. Conditional caching
      3. The @CacheEvict annotation
      4. The @Caching annotation
      5. The @CacheConfig annotation
    5. Declarative XML-based caching
    6. Configuring the cache storage
      1. Setting up the CacheManager
    7. Third-party cache implementations
      1. Ehcache-based cache
        1. XML-based configuration
    8. Creating custom caching annotations
    9. Top caching best practices to be used in a web application
    10. Summary
  12. Implementing Reactive Design Patterns
    1. Understanding application requirement over the years
    2. Understanding the reactive pattern
      1. The reactive pattern traits
        1. Responsiveness
        2. Resilience
        3. Scalable
        4. Message-driven architecture
    3. Blocking calls
    4. Non-blocking calls
    5. Back-pressure
    6. Implementing reactive with the Spring 5 Framework
      1. Reactive Streams
    7. Spring Web reactive module
      1. Implementing a reactive web application at the server side
      2. The Annotation-based programming model
      3. The functional programming model
      4. Implementing a Reactive Client-Side application
    8. Request and response body conversion
    9. Summary
  13. Implementing Concurrency Patterns
    1. Active object pattern
    2. Monitor object pattern
    3. Half-Sync/Half-Async patterns
    4. Leader/follower pattern
    5. Reactor pattern
    6. Thread-specific storage pattern
      1. Best practices for concurrency module
    7. Summary
  14. Demystifying Microservices
    1. Evolution of microservices
      1. Business demand as a catalyst for microservices evolution
      2. Technology as a catalyst for 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 service in a microservice
      2. Microservices are lightweight
      3. Microservices with polyglot architecture
      4. Automation in 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. An example of a travel agent portal
    7. Microservices benefits
      1. Supports polyglot architecture
      2. Enables experimentation and innovation
      3. Elastically and selectively scalable
      4. Allows substitution
      5. Enables to build organic systems
      6. Helps managing technology debt
      7. Allowing co-existence of different versions
      8. Supporting building self-organizing systems
      9. Supporting event-driven architecture
      10. Enables DevOps
    8. Summary
  15. Related Architecture Styles and Use Cases
    1. Service-Oriented Architecture (SOA)
      1. Service-oriented integration
      2. Legacy modernization
      3. Service-oriented application
      4. Monolithic migration using SOA
    2. Twelve-Factor Apps
      1. Single code base
      2. Bundle dependencies
      3. Externalizing configurations
      4. Backing services are addressable
      5. Isolation between build, release, and run
      6. Stateless, shared nothing processes
      7. Expose services through port bindings
      8. Concurrency for scale out
      9. Disposability, with minimal overhead
      10. Development, production parity
      11. Externalizing logs
      12. Package admin processes
    3. Serverless computing
    4. Lambda architecture
    5. DevOps, Cloud, and Containers
      1. DevOps as the practice and process for microservices
      2. Cloud and Containers as the self-service infrastructure for microservices
    6. Reactive microservices
      1. A reactive microservice-based order management system
    7. Microservice use cases
    8. Microservices early adopters - Is there a common theme?
      1. Monolithic migration as the common use case
    9. Microservice frameworks
    10. Summary
  16. Building Microservices with Spring Boot
    1. Setting up a development environment
    2. Spring Boot for building RESTful microservices
    3. Getting started with Spring Boot
    4. Developing a Spring Boot microservice
    5. Developing our first Spring Boot microservice
      1. Testing Spring Boot microservice
    6. HATEOAS-enabled Spring Boot microservice
    7. Reactive Spring Boot microservices
      1. Reactive microservices using Spring WebFlux
        1. Understanding Reactive Streams
          1. Publisher
          2. Subscriber
          3. Subscription
          4. Processor
      2. Reactive microservices using Spring Boot and RabbitMQ
    8. Implementing security
      1. Securing a microservice with basic security
      2. Securing microservice with OAuth2
    9. Enabling cross origin for microservices interactions
    10. Spring Boot actuators for microservices instrumentation
      1. Monitoring using JConsole
      2. Monitoring using ssh
      3. Adding a custom health module
      4. Building custom metrics
    11. Documenting microservices
    12. Putting it all together - Developing a customer registration microservice example
    13. Summary
  17. Scale Microservices with Spring Cloud Components
    1. What is Spring Cloud?
    2. Spring Cloud releases
    3. Setting up the environment for the BrownField PSS
    4. Spring Cloud Config
      1. Building microservices with Config Server
      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 Config Server health
      8. Config Server for configuration files
      9. Completing changes to use Config Server
    5. 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
    6. 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 client is not a Eureka Client
        3. Completing Zuul for all other services
    7. Streams for reactive microservices
    8. Protecting microservices with Spring Cloud Security
    9. Summarising the BrownField PSS architecture
    10. Summary
  18. Logging and Monitoring Microservices
    1. Understanding log management challenges
    2. Centralized logging solution
    3. Selection of logging solutions
      1. Cloud services
      2. Off-the-shelf solutions
      3. Best of the breed integration
        1. Log shippers
        2. Log stream processors
        3. Log storage
        4. Dashboards
      4. Custom logging implementation
      5. Distributed tracing with Spring Cloud Sleuth
    4. Monitoring microservices
      1. Monitoring challenges
      2. Monitoring tools
      3. Monitoring microservice dependency
      4. Spring Cloud Hystrix for fault-tolerant microservices
      5. Aggregate Hystrix streams with Turbine
    5. Data analysis using Data Lake
    6. Summary
  19. Containerizing Microservices with Docker
    1. Understanding gaps in the BrownField PSS microservices
    2. What are containers?
    3. Difference between VM and containers
    4. Benefits of containers
    5. Microservices and containers
    6. Introduction to Docker
      1. Key components of Docker
        1. The Docker daemon
        2. The Docker client
        3. The Docker image
        4. The Docker container
        5. The Docker registry
        6. Dockerfile
    7. Deploying microservices into Docker
    8. Running RabbitMQ on Docker
    9. Using the Docker registry
      1. Setting up the Docker Hub
      2. Publish microservices to the Docker Hub
    10. Microservices on Cloud
      1. Installing Docker on AWS EC2
    11. Running BrownField services on EC2
    12. Future of containerization
    13. Summary
  20. Scaling Dockerized Microservices with Mesos and Marathon
    1. Scaling microservices
      1. Understanding autoscaling
      2. The missing pieces
    2. Container orchestration
      1. Why is container orchestration is important
      2. What does container orchestration do?
      3. Relationship with microservices
      4. Relationship with virtualization
      5. Container orchestration solutions
        1. Docker Swarm
        2. Kubernetes
        3. Apache Mesos
        4. HashiCorp Nomad
        5. CoreOS Fleet
    3. Container orchestration with Mesos and Marathon
      1. Mesos in details
        1. Mesos architecture
        2. Marathon
    4. Implementing Mesos and Marathon with DCOS
    5. Implementing Mesos and Marathon for BrownField microservices
      1. Installing Mesos, Marathon, and related components
      2. Running Mesos and Marathon
    6. Preparing BrownField PSS services
      1. Deploying BrownField PSS services
    7. Summary
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Building Microservices with Spring
  • Author(s): Dinesh Rajput, Rajesh R V
  • Release date: December 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789955644