List of Listings

Chapter 1. Welcome to the cloud, Spring

Listing 1.1. Hello World with Spring Boot: a simple Spring microservice

Listing 1.2. Hello World Service using Spring Cloud

Chapter 2. Building microservices with Spring Boot

Listing 2.1. Maven pom file for the licensing service

Listing 2.2. Introducing the @SpringBootApplication annotation

Listing 2.3. Marking the LicenseServiceController as a Spring RestController

Listing 2.4. Exposing an individual GET HTTP endpoint

Chapter 3. Controlling your configuration with Spring Cloud configuration server

Listing 3.1. Setting up the pom.xml for the Spring Cloud configuration server

Listing 3.2. The bootstrap class for your Spring Cloud Config server

Listing 3.3. Spring Cloud configuration’s application.yml file

Listing 3.4. Additional Maven dependencies needed by the licensing service

Listing 3.5. Configuring the licensing services bootstrap.yml

Listing 3.6. The JPA model code for a single license record

Listing 3.7. LicenseRepository interface defines the query methods

Listing 3.8. LicenseService class used to execute database commands

Listing 3.9. ServiceConfig used to centralize application properties

Listing 3.10. Spring Cloud config bootstrap.yml

Listing 3.11. The @RefreshScope annotation

Chapter 4. On service discovery

Listing 4.1. Adding dependencies to your pom.xml

Listing 4.2. Setting up your Eureka configuration in the application.yml file

Listing 4.3. Annotating the bootstrap class to enable the Eureka server

Listing 4.4. Modifying your organization service’s application.yml to talk to Eureka

Listing 4.5. Calling the licensing service with different REST Clients

Listing 4.6. getLicense() function will use multiple methods to perform a REST call

Listing 4.7. Setting up the bootstrap class to use the Eureka Discovery Client

Listing 4.8. Using the DiscoveryClient to look up information

Listing 4.9. Annotating and defining a RestTemplate construction method

Listing 4.10. Using a Ribbon-backed RestTemplate to call a service

Listing 4.11. Enabling the Spring Cloud/Netflix Feign client in the licensing service

Listing 4.12. Defining a Feign interface for calling the organization service

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

Listing 5.1. The @EnableCircuitBreaker annotation used to activate Hystrix in a service

Listing 5.2. Wrappering a remote resource call with a circuit breaker

Listing 5.3. Randomly timing out a call to the licensing service database

Listing 5.4. Customizing the time out on a circuit breaker call

Listing 5.5. Implementing a fallback in Hystrix

Listing 5.6. Creating a bulkhead around the getLicensesByOrg() method

Listing 5.7. Configuring the behavior of a circuit breaker

Listing 5.8. The UserContextFilter parsing the HTTP header and retrieving data

Listing 5.9. All UserContext data is managed by UserContextHolder

Listing 5.10. Defining your own Hystrix concurrency strategy

Listing 5.11. Propagating the UserContext with DelegatingUserContextCallable.java

Listing 5.12. Hooking custom HystrixConcurrencyStrategy class into Spring Cloud

Chapter 6. Service routing with Spring Cloud and Zuul

Listing 6.1. Setting up the Zuul Server bootstrap class

Listing 6.2. Configuring the Zuul server to talk to Eureka

Listing 6.3. Setting up custom routes with a prefix

Listing 6.4. Mapping the licensing service to a static route

Listing 6.5. Mapping licensing service statically to multiple routes

Listing 6.6. Zuul pre-filter for generating correlation IDs

Listing 6.7. Retrieving the tmx-correlation-id from the HTTP headers

Listing 6.8. Mapping the correlation ID to the UserContext class

Listing 6.9. Storing the HTTP header values inside the UserContext class

Listing 6.10. The UserContextHolder stores the UserContext in a ThreadLocal

Listing 6.11. All outgoing microservice calls have the correlation ID injected into them

Listing 6.12. Adding the UserContextInterceptor to the RestTemplate class

Listing 6.13. Injecting the correlation ID into the HTTP response

Listing 6.14. The skeleton of your route filter

Listing 6.15. The run() method for the SpecialRoutesFilter is where the work begins

Listing 6.16. Invoking the SpecialRouteservice to see if a routing record exists

Listing 6.17. Determining whether to use the alternative service route

Listing 6.18. The forwardToSpecialRoute invokes the alternative service

Chapter 7. Securing your microservices

Listing 7.1. The authentication-service bootstrap class

Listing 7.2. OAuth2Config service defines what applications can use your service

Listing 7.3. Defining the User ID, password and roles for your application

Listing 7.4. Configuring the bootstrap class to be a protected resource

Listing 7.5. Restricting access to only authenticated users

Listing 7.6. Restricting deletes to the ADMIN role only

Listing 7.7. Using the OAuth2RestTemplate to propagate the OAuth2 access token

Listing 7.8. Setting up the JWT token store

Listing 7.9. Hooking JWT into your authentication service via the JWTOAuth2Config class

Listing 7.10. Creating a custom RestTemplate class to inject the JWT token

Listing 7.11. The UserContextInterceptor will inject the JWT token into your REST calls

Listing 7.12. Using a JWT token enhancer class to add a custom field

Listing 7.13. Hooking in your TokenEnhancer

Listing 7.14. Parsing the organizationId out of your JWT Token

Chapter 8. Event-driven architecture with Spring Cloud Stream

Listing 8.1. The annotated Application.java class

Listing 8.2. Publishing a message to the message broker

Listing 8.3. The Spring Cloud Stream configuration for publishing a message

Listing 8.4. Publishing a message in your organization service

Listing 8.5. Consuming a message using Spring Cloud Stream

Listing 8.6. Mapping the licensing service to a message topic in Kafka

Listing 8.7. Adding the Spring Redis Dependencies

Listing 8.8. Establishing how your licensing service will communicate with Redis

Listing 8.9. OrganizationRedisRepository defines methods used to call Redis

Listing 8.10. The OrganizationRedisRepositoryImpl implementation

Listing 8.11. OrganizationRestTemplateClient class will implement cache logic

Listing 8.12. Defining a custom input channel for the licensing service

Listing 8.13. Modifying the licensing service to use your custom input channel

Listing 8.14. Using the new custom channel in the OrganizationChangeHandler

Listing 8.15. Processing an organization change in the licensing service

Chapter 9. Distributed tracing with Spring Cloud Sleuth and Zipkin

Listing 9.1. Adding Spring Cloud Sleuth to Zuul

Listing 9.2. Adding the Spring Cloud Sleuth trace ID via a Zuul POST filter

Listing 9.3. Client-side Spring Cloud Sleuth and Zipkin dependences

Listing 9.4. JAR dependencies needed for Zipkin service

Listing 9.5. Building your Zipkin servers bootstrap class

Listing 9.6. Instrumenting the call to read licensing data from Redis

Listing 9.7. The instrumented getOrg() method

Chapter 10. Deploying your microservices

Listing 10.1. Anatomy of the .travis.yml build

Listing 10.2. Configuring the core run-time for your build

Listing 10.3. Pre-build installation steps

Listing 10.4. Executing the build

Listing 10.5. Tagging the chapter 10 code repository with the GitHub release API

Listing 10.6. Pushing created Docker images to Docker Hub

Listing 10.7. Deploying Docker Images to EC2

Listing 10.8. Kicking off the platform tests using Travis CI REST API

Appendix A. Running a cloud on your desktop

Listing A.1. Spotify Docker Maven plugin used to create Dockerimage

Listing A.2. Dockerfile prepares Docker image

Listing A.3. run.sh script used to launch the licensing service

Listing A.4. The docker-compose.yml file defines the services that are to be launched