Spring Microservices in Action, Second Edition

Book description

By dividing large applications into separate self-contained units, Microservices are a great step toward reducing complexity and increasing flexibility. Spring Microservices in Action, Second Edition teaches you how to build microservice-based applications using Java and the Spring platform. This second edition is fully updated for the latest version of Spring, with expanded coverage of API routing with Spring Cloud Gateway, logging with the ELK stack, metrics with Prometheus and Grafana, security with the Hashicorp Vault, and modern deployment practices with Kubernetes and Istio.

About the Technology
Building and deploying microservices can be easy in Spring! Libraries like Spring Boot, Spring Cloud, and Spring Cloud Gateway reduce the boilerplate code in REST-based services. They provide an effective toolbox to get your microservices up and running on both public and private clouds.

About the Book
Spring Microservices in Action, Second Edition teaches you to build microservice-based applications using Java and Spring. You’ll start by creating basic services, then move to efficient logging and monitoring. Learn to refactor Java applications with Spring’s intuitive tooling, and master API management with Spring Cloud Gateway. You’ll even deploy Spring Cloud applications with AWS and Kubernetes.

What's Inside
  • Microservice design principles and best practices
  • Configuration with Spring Cloud Config and Hashicorp Vault
  • Client-side resiliency with Resilience4j, and Spring Cloud Load Balancer
  • Metrics monitoring with Prometheus and Grafana
  • Distributed tracing with Spring Cloud Sleuth, Zipkin, and ELK Stack


About the Reader
For experienced Java and Spring developers.

About the Authors
John Carnell is a senior cloud engineer with 20 years of Java experience. Illary Huaylupo Sánchez is a software engineer with over 13 years of experience.

Quotes
A must-have for any professional Spring microservice developer’s bookshelf!
- Iain Campbell, Tango Telecom

A holistic and practical approach to building cloud native microservices using Spring. Loads of practice, advice, and fun.
- Satej Sahu, Honeywell

A perfect reference, whether you are a beginner or an experienced Java developer.
- Philippe Vialatte, Symphony

A great guide on modern secure deployment practices for the cloud.
- Todd Cook, Appen

Publisher resources

View/Submit Errata

Table of contents

  1. inside front cover
  2. Spring Microservices in Action
  3. Copyright
  4. dedication
  5. brief contents
  6. contents
  7. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    4. about the authors
    5. About the cover illustration
  8. 1 Welcome to the cloud, Spring
    1. 1.1 The evolution towards a microservices architecture
      1. 1.1.1 N-tier architecture
      2. 1.1.2 What’s a monolithic architecture?
      3. 1.1.3 What’s a microservice?
      4. 1.1.4 Why change the way we build applications?
    2. 1.2 Microservices with Spring
    3. 1.3 What are we building?
    4. 1.4 What is this book about?
      1. 1.4.1 What you’ll learn in this book
      2. 1.4.2 Why is this book relevant to you?
    5. 1.5 Cloud and microservice-based applications
      1. 1.5.1 Building a microservice with Spring Boot
      2. 1.5.2 What exactly is cloud computing?
      3. 1.5.3 Why the cloud and microservices?
    6. 1.6 Microservices are more than writing the code
    7. 1.7 Core microservice development pattern
    8. 1.8 Microservice routing patterns
    9. 1.9 Microservice client resiliency
    10. 1.10 Microservice security patterns
    11. 1.11 Microservice logging and tracing patterns
    12. 1.12 Application metrics pattern
    13. 1.13 Microservice build/deployment patterns
    14. Summary
  9. 2 Exploring the microservices world with Spring Cloud
    1. 2.1 What is Spring Cloud?
      1. 2.1.1 Spring Cloud Config
      2. 2.1.2 Spring Cloud Service Discovery
      3. 2.1.3 Spring Cloud LoadBalancer and Resilience4j
      4. 2.1.4 Spring Cloud API Gateway
      5. 2.1.5 Spring Cloud Stream
      6. 2.1.6 Spring Cloud Sleuth
      7. 2.1.7 Spring Cloud Security
    2. 2.2 Spring Cloud by example
    3. 2.3 How to build a cloud-native microservice
      1. 2.3.1 Codebase
      2. 2.3.2 Dependencies
      3. 2.3.3 Config
      4. 2.3.4 Backing services
      5. 2.3.5 Build, release, run
      6. 2.3.6 Processes
      7. 2.3.7 Port binding
      8. 2.3.8 Concurrency
      9. 2.3.9 Disposability
      10. 2.3.10 Dev/prod parity
      11. 2.3.11 Logs
      12. 2.3.12 Admin processes
    4. 2.4 Making sure our examples are relevant
    5. 2.5 Building a microservice with Spring Boot and Java
      1. 2.5.1 Setting up the environment
      2. 2.5.2 Getting started with the skeleton project
      3. 2.5.3 Booting your Spring Boot application: Writing the bootstrap class
    6. Summary
  10. 3 Building microservices with Spring Boot
    1. 3.1 The architect’s story: Designing the microservice architecture
      1. 3.1.1 Decomposing the business problem
      2. 3.1.2 Establishing service granularity
      3. 3.1.3 Defining the service interfaces
    2. 3.2 When not to use microservices
      1. 3.2.1 Complexity when building distributed systems
      2. 3.2.2 Server or container sprawl
      3. 3.2.3 Application type
      4. 3.2.4 Data transactions and consistency
    3. 3.3 The developer’s tale: Building a microservice with Spring Boot and Java
      1. 3.3.1 Building the doorway into the microservice: The Spring Boot controller
      2. 3.3.2 Adding internationalization into the licensing service
      3. 3.3.3 Implementing Spring HATEOAS to display related links
    4. 3.4 The DevOps story: Building for the rigors of runtime
      1. 3.4.1 Service assembly: Packaging and deploying your microservices
      2. 3.4.2 Service bootstrapping: Managing configuration of your microservices
      3. 3.4.3 Service registration and discovery: How clients communicate with your microservices
      4. 3.4.4 Communicating a microservice’s health
    5. 3.5 Pulling the perspectives together
    6. Summary
  11. 4 Welcome to Docker
    1. 4.1 Containers or virtual machines?
    2. 4.2 What is Docker?
    3. 4.3 Dockerfiles
    4. 4.4 Docker Compose
    5. 4.5 Integrating Docker with our microservices
      1. 4.5.1 Building the Docker Image
      2. 4.5.2 Creating Docker images with Spring Boot
      3. 4.5.3 Launching the services with Docker Compose
      4. Summary
  12. 5 Controlling your configuration with the Spring Cloud Configuration Server
    1. 5.1 On managing configuration (and complexity)
      1. 5.1.1 Your configuration management architecture
      2. 5.1.2 Implementation choices
    2. 5.2 Building our Spring Cloud Configuration Server
      1. 5.2.1 Setting up the Spring Cloud Config bootstrap class
      2. 5.2.2 Using the Spring Cloud Config Server with a filesystem
      3. 5.2.3 Setting up the configuration files for a service
    3. 5.3 Integrating Spring Cloud Config with a Spring Boot client
      1. 5.3.1 Setting up the licensing service Spring Cloud Config Service dependencies
      2. 5.3.2 Configuring the licensing service to use Spring Cloud Config
      3. 5.3.3 Wiring in a data source using Spring Cloud Config Server
      4. 5.3.4 Directly reading properties using @ConfigurationProperties
      5. 5.3.5 Refreshing your properties using Spring Cloud Config Server
      6. 5.3.6 Using Spring Cloud Configuration Server with Git
      7. 5.3.7 Integrating Vault with the Spring Cloud Config service
      8. 5.3.8 Vault UI
    4. 5.4 Protecting sensitive configuration information
      1. 5.4.1 Setting up a symmetric encryption key
      2. 5.4.2 Encrypting and decrypting a property
    5. 5.5 Closing thoughts
    6. Summary
  13. 6 On service discovery
    1. 6.1 Where’s my service?
    2. 6.2 Service discovery in the cloud
      1. 6.2.1 The architecture of service discovery
      2. 6.2.2 Service discovery in action using Spring and Netflix Eureka
    3. 6.3 Building our Spring Eureka service
    4. 6.4 Registering services with Spring Eureka
      1. 6.4.1 Eureka’s REST API
      2. 6.4.2 Eureka dashboard
    5. 6.5 Using service discovery to look up a service
      1. 6.5.1 Looking up service instances with Spring Discovery Client
      2. 6.5.2 Invoking services with a Load Balancer–aware Spring REST template
      3. 6.5.3 Invoking services with Netflix Feign client
      4. Summary
  14. 7 When bad things happen: Resiliency patterns with Spring Cloud and Resilience4j
    1. 7.1 What are client-side resiliency patterns?
      1. 7.1.1 Client-side load balancing
      2. 7.1.2 Circuit breaker
      3. 7.1.3 Fallback processing
      4. 7.1.4 Bulkheads
    2. 7.2 Why client resiliency matters
    3. 7.3 Implementing Resilience4j
    4. 7.4 Setting up the licensing service to use Spring Cloud and Resilience4j
    5. 7.5 Implementing a circuit breaker
      1. 7.5.1 Adding the circuit breaker to the organization service
      2. 7.5.2 Customizing the circuit breaker
    6. 7.6 Fallback processing
    7. 7.7 Implementing the bulkhead pattern
    8. 7.8 Implementing the retry pattern
    9. 7.9 Implementing the rate limiter pattern
    10. 7.10 ThreadLocal and Resilience4j
    11. Summary
  15. 8 Service routing with Spring Cloud Gateway
    1. 8.1 What is a service gateway?
    2. 8.2 Introducing Spring Cloud Gateway
      1. 8.2.1 Setting up the Spring Boot gateway project
      2. 8.2.2 Configuring the Spring Cloud Gateway to communicate with Eureka
    3. 8.3 Configuring routes in Spring Cloud Gateway
      1. 8.3.1 Automated mapping of routes via service discovery
      2. 8.3.2 Manually mapping routes using service discovery
      3. 8.3.3 Dynamically reloading route configuration
    4. 8.4 The real power of Spring Cloud Gateway: Predicate and Filter Factories
      1. 8.4.1 Built-in Predicate Factories
      2. 8.4.2 Built-in Filter Factories
      3. 8.4.3 Custom filters
    5. 8.5 Building the pre-filter
    6. 8.6 Using the correlation ID in the services
      1. 8.6.1 UserContextFilter: Intercepting the incoming HTTP request
      2. 8.6.2 UserContext: Making the HTTP headers easily accessible to the service
      3. 8.6.3 Custom RestTemplate and UserContextInterceptor: Ensuring that the correlation ID gets propagated
    7. 8.7 Building a post-filter receiving correlation ID
    8. Summary
  16. 9 Securing your microservices
    1. 9.1 What is OAuth2?
    2. 9.2 Introduction to Keycloak
    3. 9.3 Starting small: Using Spring and Keycloak to protect a single endpoint
      1. 9.3.1 Adding Keycloak to Docker
      2. 9.3.2 Setting up Keycloak
      3. 9.3.3 Registering a client application
      4. 9.3.4 Configuring O-stock users
      5. 9.3.5 Authenticating our O-stock users
    4. 9.4 Protecting the organization service using Keycloak
      1. 9.4.1 Adding the Spring Security and Keycloak JARs to the individual services
      2. 9.4.2 Configuring the service to point to our Keycloak server
      3. 9.4.3 Defining who and what can access the service
      4. 9.4.4 Propagating the access token
      5. 9.4.5 Parsing a custom field in a JWT
    5. 9.5 Some closing thoughts on microservice security
      1. 9.5.1 Use HTTPS secure sockets layer (SSL) for all service communication
      2. 9.5.2 Use a service gateway to access your microservices
      3. 9.5.3 Zone your services into a public API and private API
      4. 9.5.4 Limit the attack surface of your microservices by locking down unneeded network ports
      5. Summary
  17. 10 Event-driven architecture with Spring Cloud Stream
    1. 10.1 The case for messaging, EDA, and microservices
      1. 10.1.1 Using a synchronous request-response approach to communicate state change
      2. 10.1.2 Using messaging to communicate state changes between services
      3. 10.1.3 Downsides of a messaging architecture
    2. 10.2 Introducing Spring Cloud Stream
    3. 10.3 Writing a simple message producer and consumer
      1. 10.3.1 Configuring Apache Kafka and Redis in Docker
      2. 10.3.2 Writing the message producer in the organization service
      3. 10.3.3 Writing the message consumer in the licensing service
      4. 10.3.4 Seeing the message service in action
    4. 10.4 A Spring Cloud Stream use case: Distributed caching
      1. 10.4.1 Using Redis to cache lookups
      2. 10.4.2 Defining custom channels
    5. Summary
  18. 11 Distributed tracing with Spring Cloud Sleuth and Zipkin
    1. 11.1 Spring Cloud Sleuth and the correlation ID
      1. 11.1.1 Adding Spring Cloud Sleuth to licensing and organization
      2. 11.1.2 Anatomy of a Spring Cloud Sleuth trace
    2. 11.2 Log aggregation and Spring Cloud Sleuth
      1. 11.2.1 A Spring Cloud Sleuth/ELK Stack implementation in action
      2. 11.2.2 Configuring Logback in our services
      3. 11.2.3 Defining and running ELK Stack applications in Docker
      4. 11.2.4 Configuring Kibana
      5. 11.2.5 Searching for Spring Cloud Sleuth trace IDs in Kibana
      6. 11.2.6 Adding the correlation ID to the HTTP response with Spring Cloud Gateway
    3. 11.3 Distributed tracing with Zipkin
      1. 11.3.1 Setting up the Spring Cloud Sleuth and Zipkin dependencies
      2. 11.3.2 Configuring the services to point to Zipkin
      3. 11.3.3 Configuring a Zipkin server
      4. 11.3.4 Setting tracing levels
      5. 11.3.5 Using Zipkin to trace transactions
      6. 11.3.6 Visualizing a more complex transaction
      7. 11.3.7 Capturing messaging traces
      8. 11.3.8 Adding custom spans
    4. Summary
  19. 12 Deploying your microservices
    1. 12.1 The architecture of a build/deployment pipeline
    2. 12.2 Setting up O-stock’s core infrastructure in the cloud
      1. 12.2.1 Creating the PostgreSQL database using Amazon RDS
      2. 12.2.2 Creating the Redis cluster in Amazon
    3. 12.3 Beyond the infrastructure: Deploying O-stock and ELK
      1. 12.3.1 Creating an EC2 with ELK
      2. 12.3.2 Deploying the ELK Stack in the EC2 instance
      3. 12.3.3 Creating an EKS cluster
    4. 12.4 Your build/deployment pipeline in action
    5. 12.5 Creating our build/deploy pipeline
      1. 12.5.1 Setting up GitHub
      2. 12.5.2 Enabling our services to build in Jenkins
      3. 12.5.3 Understanding and generating the pipeline script
      4. 12.5.4 Creating the Kubernetes pipeline scripts
    6. 12.6 Closing thoughts on the build/deployment pipeline
    7. Summary
  20. appendix A. Microservices architecture best practices
    1. A.1 Richardson Maturity Model
    2. A.2 Spring HATEOAS
    3. A.3 Externalized configuration
    4. A.4 Continuous integration (CI) and continuous delivery (CD)
    5. A.5 Monitoring
    6. A.6 Logging
    7. A.7 API gateways
  21. appendix B. OAuth2 grant types
    1. B.1 Password grant types
    2. B.2 Client credential grant type
    3. B.3 Authorization grant type
    4. B.4 Implicit grant type
    5. B.5 How tokens are refreshed
  22. appendix C. Monitoring your microservices
    1. C.1 Introducing monitoring with Spring Boot Actuator
      1. C.1.1 Adding Spring Boot Actuator
      2. C.1.2 Enabling Actuator endpoints
    2. C.2 Setting up Micrometer and Prometheus
      1. C.2.1 Understanding Micrometer and Prometheus
      2. C.2.2 Implementing Micrometer and Prometheus
    3. C.3 Configuring Grafana
    4. C.4 Summarizing our discussion
  23. index
  24. inside back cover

Product information

  • Title: Spring Microservices in Action, Second Edition
  • Author(s): Illary Huaylupo Sánchez, John Carnell
  • Release date: June 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617296956