Spring Microservices in Action [2 ed.] 1617296953, 9781617296956

Spring Microservices in Action, Second Edition teaches you to build microservice-based applications using Java and Sprin

12,435 3,306 29MB

English Pages 448 [447] Year 2021

Report DMCA / Copyright


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

Spring Microservices in Action [2 ed.]
 1617296953, 9781617296956

  • Commentary
  • Vector PDF
  • 1 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up