O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Spring Microservices in Action

Book Description

Spring Microservices in Action teaches you how to build microservice-based applications using Java and the Spring platform. You'll learn to do microservice design as you build and deploy your first Spring Cloud application. Throughout the book, carefully selected real-life examples expose microservice-based patterns for configuring, routing, scaling, and deploying your services. You'll see how Spring's intuitive tooling can help augment and refactor existing applications with microservices.

Table of Contents

  1. Spring Microservices in Action
    1. John Carnell
  2. Copyright
    1. Dedication
  3. Brief Table of Contents
  4. Table of Contents
  5. Preface
  6. Acknowledgments
  7. About this Book
    1. You should read this book if
    2. How this book is organized
    3. About the code
    4. Author Online
  8. About the Author
  9. About the Cover Illustration
  10. Chapter 1. Welcome to the cloud, Spring
    1. 1.1. What’s a microservice?
    2. 1.2. What is Spring and why is it relevant to microservices?
    3. 1.3. What you’ll learn in this book
    4. 1.4. Why is this book relevant to you?
    5. 1.5. Building a microservice with Spring Boot
    6. 1.6. Why change the way we build applications?
    7. 1.7. What exactly is the cloud?
    8. 1.8. Why the cloud and microservices?
    9. 1.9. Microservices are more than writing the code
      1. 1.9.1. Core microservice development pattern
      2. 1.9.2. Microservice routing patterns
      3. 1.9.3. Microservice client resiliency patterns
      4. 1.9.4. Microservice security patterns
      5. 1.9.5. Microservice logging and tracing patterns
      6. 1.9.6. Microservice build/deployment patterns
    10. 1.10. Using Spring Cloud in building your microservices
      1. 1.10.1. Spring Boot
      2. 1.10.2. Spring Cloud Config
      3. 1.10.3. Spring Cloud service discovery
      4. 1.10.4. Spring Cloud/Netflix Hystrix and Ribbon
      5. 1.10.5. Spring Cloud/Netflix Zuul
      6. 1.10.6. Spring Cloud Stream
      7. 1.10.7. Spring Cloud Sleuth
      8. 1.10.8. Spring Cloud Security
      9. 1.10.9. What about provisioning?
    11. 1.11. Spring Cloud by example
    12. 1.12. Making sure our examples are relevant
    13. 1.13. Summary
  11. Chapter 2. Building microservices with Spring Boot
    1. 2.1. The architect’s story: designing the microservice architecture
      1. 2.1.1. Decomposing the business problem
      2. 2.1.2. Establishing service granularity
      3. 2.1.3. Talking to one another: service interfaces
    2. 2.2. When not to use microservices
      1. 2.2.1. Complexity of building distributed systems
      2. 2.2.2. Server sprawl
      3. 2.2.3. Type of application
      4. 2.2.4. Data transformations and consistency
    3. 2.3. The developer’s tale: building a microservice with Spring Boot and Java
      1. 2.3.1. Getting started with the skeleton project
      2. 2.3.2. Booting your Spring Boot application: writing the Bootstrap class
      3. 2.3.3. Building the doorway into the microservice: the Spring Boot controller
    4. 2.4. The DevOps story: building for the rigors of runtime
      1. 2.4.1. Service assembly: packaging and deploying your microservices
      2. 2.4.2. Service bootstrapping: managing configuration of your microservices
      3. 2.4.3. Service registration and discovery: how clients communicate with - your microservices
      4. 2.4.4. Communicating a microservice’s health
    5. 2.5. Pulling the perspectives together
    6. 2.6. Summary
  12. Chapter 3. Controlling your configuration with Spring Cloud configuration server
    1. 3.1. On managing configuration (and complexity)
      1. 3.1.1. Your configuration management architecture
      2. 3.1.2. Implementation choices
    2. 3.2. Building our Spring Cloud configuration server
      1. 3.2.1. Setting up the Spring Cloud Config Bootstrap class
      2. 3.2.2. Using Spring Cloud configuration server with the filesystem
    3. 3.3. Integrating Spring Cloud Config with a Spring Boot client
      1. 3.3.1. Setting up the licensing service Spring Cloud Config server dependencies
      2. 3.3.2. Configuring the licensing service to use Spring Cloud Config
      3. 3.3.3. Wiring in a data source using Spring Cloud configuration server
      4. 3.3.4. Directly Reading Properties using the @Value Annotation
      5. 3.3.5. Using Spring Cloud configuration server with Git
      6. 3.3.6. Refreshing your properties using Spring Cloud configuration server
    4. 3.4. Protecting sensitive configuration information
      1. 3.4.1. Download and install Oracle JCE jars needed for encryption
      2. 3.4.2. Setting up an encryption key
      3. 3.4.3. Encrypting and decrypting a property
      4. 3.4.4. Configure microservices to use encryption on the client side
    5. 3.5. Closing thoughts
    6. 3.6. Summary
  13. Chapter 4. On service discovery
    1. 4.1. Where’s my service?
    2. 4.2. On service discovery in the cloud
      1. 4.2.1. The architecture of service discovery
      2. 4.2.2. Service discovery in action using Spring and Netflix Eureka
    3. 4.3. Building your Spring Eureka Service
    4. 4.4. Registering services with Spring Eureka
    5. 4.5. Using service discovery to look up a service
      1. 4.5.1. Looking up service instances with Spring DiscoveryClient
      2. 4.5.2. Invoking services with Ribbon-aware Spring RestTemplate
      3. 4.5.3. Invoking services with Netflix Feign client
    6. 4.6. Summary
  14. Chapter 5. When bad things happen: client resiliency patterns with Spring Cloud and Netflix Hystrix
    1. 5.1. What are client-side resiliency patterns?
      1. 5.1.1. Client-side load balancing
      2. 5.1.2. Circuit breaker
      3. 5.1.3. Fallback processing
      4. 5.1.4. Bulkheads
    2. 5.2. Why client resiliency matters
    3. 5.3. Enter Hystrix
    4. 5.4. Setting up the licensing server to use Spring Cloud and Hystrix
    5. 5.5. Implementing a circuit breaker using Hystrix
      1. 5.5.1. Timing out a call to the organization microservice
      2. 5.5.2. Customizing the timeout on a circuit breaker
    6. 5.6. Fallback processing
    7. 5.7. Implementing the bulkhead pattern
    8. 5.8. Getting beyond the basics; fine-tuning Hystrix
      1. 5.8.1. Hystrix configuration revisited
    9. 5.9. Thread context and Hystrix
      1. 5.9.1. ThreadLocal and Hystrix
      2. 5.9.2. The HystrixConcurrencyStrategy in action
    10. 5.10. Summary
  15. Chapter 6. Service routing with Spring Cloud and Zuul
    1. 6.1. What is a services gateway?
    2. 6.2. Introducing Spring Cloud and Netflix Zuul
      1. 6.2.1. Setting up the Zuul Spring Boot project
      2. 6.2.2. Using Spring Cloud annotation for the Zuul service
      3. 6.2.3. Configuring Zuul to communicate with Eureka
    3. 6.3. Configuring routes in Zuul
      1. 6.3.1. Automated mapping routes via service discovery
      2. 6.3.2. Mapping routes manually using service discovery
      3. 6.3.3. Manual mapping of routes using static URLs
      4. 6.3.4. Dynamically reload route configuration
      5. 6.3.5. Zuul and service timeouts
    4. 6.4. The real power of Zuul: filters
    5. 6.5. Building your first Zuul pre-filter generating correlation IDs
      1. 6.5.1. Using the correlation ID in your service calls
    6. 6.6. Building a post filter receiving correlation IDs
    7. 6.7. Building a dynamic route filter
      1. 6.7.1. Building the skeleton of the routing filter
      2. 6.7.2. Implementing the run() method
      3. 6.7.3. Forwarding the route
      4. 6.7.4. Pulling it all together
    8. 6.8. Summary
  16. Chapter 7. Securing your microservices
    1. 7.1. Introduction to OAuth2
    2. 7.2. Starting small: using Spring and OAuth2 to protect a single endpoint
      1. 7.2.1. Setting up the EagleEye OAuth2 authentication service
      2. 7.2.2. Registering client applications with the OAuth2 service
      3. 7.2.3. Configuring EagleEye users
      4. 7.2.4. Authenticating the user
    3. 7.3. Protecting the organization service using OAuth2
      1. 7.3.1. Adding the Spring Security and OAuth2 jars to the individual services
      2. 7.3.2. Configuring the service to point to your OAuth2 authentication service
      3. 7.3.3. Defining who and what can access the service
      4. 7.3.4. Propagating the OAuth2 access token
    4. 7.4. JavaScript Web Tokens and OAuth2
      1. 7.4.1. Modifying the authentication service to issue JavaScript Web Tokens
      2. 7.4.2. Consuming JavaScript Web Tokens in your microservices
      3. 7.4.3. Extending the JWT Token
      4. 7.4.4. Parsing a custom field out of a JavaScript token
    5. 7.5. Some closing thoughts on microservice security
      1. Use HTTPS/Secure Sockets Layer (SSL) for all service communication
      2. Use a services gateway to access your microservices
      3. Zone your services into a public API and private API
      4. Limit the attack surface of your microservices by locking down un- nneeded network ports
    6. 7.6. Summary
  17. Chapter 8. Event-driven architecture with Spring Cloud Stream
    1. 8.1. The case for messaging, EDA, and microservices
      1. 8.1.1. Using synchronous request-response approach to communicate state change
      2. 8.1.2. Using messaging to communicate state changes between services
      3. 8.1.3. Downsides of a messaging architecture
    2. 8.2. Introducing Spring Cloud Stream
      1. 8.2.1. The Spring Cloud Stream architecture
    3. 8.3. Writing a simple message producer and consumer
      1. 8.3.1. Writing the message producer in the organization service
      2. 8.3.2. Writing the message consumer in the licensing service
      3. 8.3.3. Seeing the message service in action
    4. 8.4. A Spring Cloud Stream use case: distributed caching
      1. 8.4.1. Using Redis to cache lookups
      2. 8.4.2. Defining custom channels
      3. 8.4.3. Bringing it all together: clearing the cache when a message is received
    5. 8.5. Summary
  18. Chapter 9. Distributed tracing with Spring Cloud Sleuth and Zipkin
    1. 9.1. Spring Cloud Sleuth and the correlation ID
      1. 9.1.1. Adding Spring Cloud sleuth to licensing and organization
      2. 9.1.2. Anatomy of a Spring Cloud Sleuth trace
    2. 9.2. Log aggregation and Spring Cloud Sleuth
      1. 9.2.1. A Spring Cloud Sleuth/Papertrail implementation in action
      2. 9.2.2. Create a Papertrail account and configure a syslog connector
      3. 9.2.3. Redirecting Docker output to Papertrail
      4. 9.2.4. Searching for Spring Cloud Sleuth trace IDs in Papertrail
      5. 9.2.5. Adding the correlation ID to the HTTP response with Zuul
    3. 9.3. Distributed tracing with Open Zipkin
      1. 9.3.1. Setting up the Spring Cloud Sleuth and Zipkin dependencies
      2. 9.3.2. Configuring the services to point to Zipkin
      3. 9.3.3. Installing and configuring a Zipkin server
      4. 9.3.4. Setting tracing levels
      5. 9.3.5. Using Zipkin to trace transactions
      6. 9.3.6. Visualizing a more complex transaction
      7. 9.3.7. Capturing messaging traces
      8. 9.3.8. Adding custom spans
    4. 9.4. Summary
  19. Chapter 10. Deploying your microservices
    1. 10.1. EagleEye: setting up your core infrastructure in the cloud
      1. 10.1.1. Creating the PostgreSQL database using Amazon RDS
      2. 10.1.2. Creating the Redis cluster in Amazon
      3. 10.1.3. Creating an ECS cluster
    2. 10.2. Beyond the infrastructure: deploying EagleEye
      1. 10.2.1. Deploying the EagleEye services to ECS manually
    3. 10.3. The architecture of a build/deployment pipeline
    4. 10.4. Your build and deployment pipeline in action
    5. 10.5. Beginning your build deploy/pipeline: GitHub and Travis CI
    6. 10.6. Enabling your service to build in Travis CI
      1. 10.6.1. Core build run-time configuration
      2. 10.6.2. Pre-build tool installations
      3. 10.6.3. Executing the build
      4. 10.6.4. Tagging the source control code
      5. 10.6.5. Building the microservices and creating the Docker images
      6. 10.6.6. Pushing the images to Docker Hub
      7. 10.6.7. Starting the services in Amazon ECS
      8. 10.6.8. Kicking off the platform tests
    7. 10.7. Closing thoughts on the build/deployment pipeline
    8. 10.8. Summary
  20. Appendix A. Running a cloud on your desktop
    1. A.1. Required software
    2. A.2. Downloading the projects from GitHub
    3. A.3. Anatomy of each chapter
    4. A.4. Building and compiling the projects
    5. A.5. Building the Docker image
    6. A.6. Launching the services with Docker Compose
  21. Appendix B. OAuth2 grant types
    1. B.1. Password grants
    2. B.2. Client credential grants
    3. B.3. Authorization code grants
    4. B.4. Implicit grant
    5. B.5. How tokens are refreshed
  22. Index
  23. List of Figures
  24. List of Tables
  25. List of Listings