Spring Microservices in Action
John Carnell
  • June 2017
  • ISBN 9781617293986
  • 384 pages
  • printed in black & white

Spring is fast becoming the framework for microservices-this book shows you why and how.

John Guthrie, Dell/EMC

Spring Microservices in Action teaches you how to build microservice-based applications using Java and the Spring platform.

Table of Contents detailed table of contents

1. Welcome to the cloud, Spring

1.1. What is a microservice?

1.2. What is Spring and why is it relevant to microservices?

1.3. What you will learn in this book

1.4. Why is this book relevant to you?

1.5. Building a microservice with Spring Boot

1.6. Why change the way we build applications?

1.7. What exactly is the cloud?

1.8. Why the cloud and microservices?

1.9. Microservices are more than just writing the code

1.9.1. Core microservice development pattern

1.9.2. Microservice routing patterns

1.9.3. Microservice client resiliency patterns

1.9.4. Microservice security patterns

1.9.5. Microservice logging and tracing patterns

1.9.6. Microservice build/deployment patterns

1.10. Leveraging Spring Cloud in building your microservices

1.10.1. Spring Boot

1.10.2. Spring Cloud Config

1.10.3. Spring Cloud Service Discovery

1.10.4. Spring Cloud/Netflix Hystrix and Ribbon

1.10.5. Spring Cloud/Netflix Zuul

1.10.6. Spring Cloud Stream

1.10.7. Spring Cloud Sleuth

1.10.8. Spring Cloud Security

1.10.9. What about Provisioning?

1.11. Spring Cloud by example

1.12. Making sure our examples are relevant

1.13. Summary

2. Building microservices with Spring Boot

2.1. The architect’s story: designing the microservice architecture

2.1.1. Decomposing the business problem

2.1.2. Establishing Service Granularity

2.1.3. Talking to one another: service interfaces

2.2. When not to use microservices

2.2.1. Complexity of building distributed systems

2.2.2. Server sprawl

2.2.3. Type of application

2.2.4. Data Transformations and consistency

2.3. The developer’s tale: building a microservice with Spring Boot and Java

2.3.1. Getting started with the skeleton project

2.3.2. Booting our Spring Boot application: writing the Bootstrap class

2.3.3. Building the doorway into the microservice: The Spring Boot controller

2.4. The DevOps story: building for the rigors of runtime

2.4.1. Service assembly: packaging and deploying your microservices

2.4.2. Service bootstrapping: managing configuration of your microservices

2.4.3. Service registration and discovery: how clients communicate to your microservices

2.4.4. Communicating a microservice’s health

2.5. Pulling the perspectives together

2.6. Summary

3. Controlling your configuration with Spring Cloud Configuration Server

3.1. On managing configuration (and complexity)

3.1.1. Our configuration management architecture

3.1.2. Implementation choices

3.2. Building our Spring Cloud Configuration Server

3.2.1. Setting up the Spring Cloud Config Bootstrap class

3.2.2. Using Spring Cloud Configuration Server with the filesystem

3.3. Integrating Spring Cloud Config with a Spring Boot Client

3.3.1. Setting up the Licensing Service Spring Cloud Config Server Dependencies

3.3.2. Configuring the Licensing Service to use Spring Cloud Config

3.3.3. Wiring in a data source using Spring Cloud Configuration Server

3.3.4. Directly Reading Properties using the @Value Annotation

3.3.5. Using Spring Cloud Configuration Server with Git

3.3.6. Refreshing your properties using Spring Cloud Configuration Server

3.4. Protecting sensitive configuration information

3.4.1. Download and Install Oracle JCE Jars needed for encryption

3.4.2. Setting up an encryption key

3.4.3. Encrypting and decrypting a property

3.4.4. Configure our microservices to use encryption on the client side

3.5. Closing Thoughts

3.6. Summary

4. On service discovery

4.1. Where's my Service?

4.2. On Service Discovery in the Cloud

4.2.1. The Architecture of Service Discovery

4.2.2. Service Discovery in Action Using Spring and Netflix Eureka

4.3. Building our Spring Eureka Service

4.4. Registering Services with Spring Eureka

4.5. Using Service Discovery to Look up a Service

4.5.1. Looking up Service Instances with Spring DiscoveryClient

4.5.2. Invoking Services with Ribbon-aware Spring RestTemplate

4.5.3. Invoking Services with Netflix Feign Client

4.6. Summary

5. When bad things happen: client resiliency patterns with Spring Cloud and Netflix Hystrix

5.1. What are client-side resiliency patterns

5.1.1. Client-side load balancing

5.1.2. Circuit breaker

5.1.3. Fallback processing

5.1.4. Bulkheads

5.2. Why client resiliency matters

5.3. Enter Hystrix

5.4. Setting up the licensing server to use Spring Cloud and Hystrix

5.5. Implementing a circuit breaker using Hystrix.

5.5.1. Timing out a call to the organization microservice

5.5.2. Customizing the timeout on a circuit breaker

5.6. Fallback processing

5.7. Implementing the Bulkhead Pattern

5.8. Getting beyond the basics, fine tuning Hystrix

5.8.1. Hystrix Configuration Revisited

5.9. Thread Context and Hystrix

5.9.1. ThreadLocal and Hystrix

5.9.2. The HystrixConcurrencyStrategy in action

5.10. Summary

6. Service routing with Spring Cloud and Zuul

6.1. What is a services gateway?

6.2. Introducing Spring Cloud and Netflix Zuul

6.2.1. Setting up Zuul Spring Boot Project

6.2.2. Using Spring Cloud Annotation for the Zuul Service

6.2.3. Configuring Zuul to communicate with Eureka

6.3. Configuring Routes in Zuul

6.3.1. Automated mapping routes via service discovery

6.3.2. Mapping routes manually using service discovery

6.3.3. Manual mapping of routes using static URLs

6.3.4. Dynamically reload route configuration

6.3.5. Zuul and Service timeouts

6.4. The Real Power of Zuul: Filters

6.5. Building our first Zuul pre-filter Generating Correlation Ids

6.5.1. Using the Correlation ID in your service calls

6.6. Building a Post Filter Receiving Correlation IDs

6.7. Building a Dynamic Route Filter

6.7.1. Building the skeleton of the routing filter

6.7.2. Implementing the run() method

6.7.3. Forwarding the route

6.7.4. Pulling it all together

6.8. Summary

7. Securing your microservices

7.1. Introduction to OAuth2

7.2. Starting small, using Spring and OAuth2 to protect a single endpoint

7.2.1. Setting up the EagleEye OAuth2 authentication service

7.2.2. Registering our client applications with the OAuth2 service

7.2.3. Configuring some EagleEye users

7.2.4. Authenticating the user

7.3. Protecting the Organization Service using OAuth2

7.3.1. Adding the Spring Security and OAuth2 jars to the individual services

7.3.2. Configuring the service to point to our OAuth2 authentication service

7.3.3. Defining who and what can access the service

7.3.4. Propagating the OAuth2 access token

7.4. JSON Web Tokens and OAuth2

7.4.1. Modifying the authentication service to issue JSON web tokens

7.4.2. Consuming JSON web tokens in your microservices

7.4.3. Extending the JWT Token

7.4.4. Parsing a custom field out of a JSON Token

7.5. Some closing thoughts on microservice security

7.6. Summary

8. Event Driven Architecture with Spring Cloud Stream

8.1. The case for messaging, EDA and microservices

8.1.1. Using synchronous request-response approach to communicate state change

8.1.2. Using messaging to communicate state changes between services

8.1.3. Downsides of a messaging architecture

8.2. Introducing Spring Cloud Stream

8.2.1. The Spring Cloud Stream architecture

8.3. Writing a simple message producer and consumer

8.3.1. Writing the message producer in the organization service

8.3.2. Writing the message consumer in the licensing service

8.3.3. Seeing the message service in action

8.4. A Spring Cloud Stream use case: distributed caching

8.4.1. Using Redis to cache lookups

8.4.2. Defining custom channels

8.4.3. Bringing it all together: Clearing the cache when a message is received

8.5. Summary

9. Distributed Tracing with Spring Cloud Sleuth and Zipkin

9.1. Spring Cloud Sleuth and the correlation ID

9.1.1. Adding Spring Cloud sleuth to licensing and organization

9.1.2. Anatomy of a Spring Cloud Sleuth Trace

9.2. Log aggregation and Spring Cloud Sleuth

9.2.1. A Spring Cloud Sleuth/PaperTrail implementation in action

9.2.2. Create a Papertrail account and configure a syslog connector

9.2.3. Redirecting Docker output to Papertrail

9.2.4. Searching for Spring Cloud Sleuth trace IDs in Papertrail

9.2.5. Adding the correlation id to the HTTP response with Zuul

9.3. Distributed Tracing with Open Zipkin

9.3.1. Setting up the Spring Cloud Sleuth and Zipkin Dependencies

9.3.2. Configuring the services to point to Zipkin

9.3.3. Installing and configuring a Zipkin Server

9.3.4. Setting tracing levels

9.3.5. Using Zipkin to trace transactions

9.3.6. Visualizing a more complex transaction

9.3.7. Capturing messaging traces

9.3.8. Adding custom spans

9.4. Summary

10. Deploying your microservices

10.1. EagleEye: Setting up our core infrastructure in the cloud

10.1.1. Creating the PostgreSQL database using Amazon RDS

10.1.2. Creating the Redis cluster in Amazon

10.1.3. Creating an ECS cluster

10.2. Beyond the Infrastructure: Deploying EagleEye

10.2.1. Deploying the EagleEye services to ECS manually

10.3. The architecture of a build/deployment pipeline

10.4. Our build and deployment pipeline in action

10.5. Beginning our build deploy/pipeline: GitHub and Travis CI

10.6. Enabling your service to build in Travis CI

10.6.1. Core build run-time configuration

10.6.2. Pre-build tool installations

10.6.3. Executing the build

10.6.4. Tagging the source control code

10.6.5. Building the microservices and creating the Docker images

10.6.6. Pushing the Images to Docker Hub

10.6.7. Starting the services in Amazon ECS

10.6.8. Kicking off the platform tests

10.7. Closing thoughts on the build/deployment pipeline

10.8. Summary

Appendixes

Appendix A: Running a cloud on your desktop

A.1. Required software

A.2. Downloading the projects from GitHub

A.3. Anatomy of each chapter

A.4. Build and compiling the projects

A.5. Building the Docker Image

A.6. Launching the services with Docker compose

A.7. Summary

Appendix B: OAuth2 grant types

B.1. Password grants

B.2. Client credential grants

B.3. Authorization code grants

B.4. Implicit grant

B.5. How tokens are refreshed

B.6. Summary

About the Technology

Microservices break up your code into small, distributed, and independent services that require careful forethought and design. Fortunately, Spring Boot and Spring Cloud simplify your microservice applications, just as the Spring Framework simplifies enterprise Java development. Spring Boot removes the boilerplate code involved with writing a REST-based service. Spring Cloud provides a suite of tools for the discovery, routing, and deployment of microservices to the enterprise and the cloud.

About the book

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.

What's inside

  • Core microservice design principles
  • Managing configuration with Spring Cloud Config
  • Client-side resiliency with Spring, Hystrix, and Ribbon
  • Intelligent routing using Netflix Zuul
  • Deploying Spring Cloud applications

About the reader

This book is written for developers with Java and Spring experience.

About the author

John Carnell is a senior cloud engineer with twenty years of experience in Java.


Buy
combo $49.99 pBook + eBook + liveBook
eBook $39.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks

A complete real-world bible for any microservices project in Spring.

Mirko Bernardoni, Ixxus

Thorough and practical...with all the special capabilities of Spring thrown in.

Vipul Gupta, SAP

Learn how to tame complex and distributed system design. Highly recommended.

Ashwin Raj, Innocepts