Spring Cloud Microservices(Quick guide)

Purpose of this post is to give a reader a quick info about different components of Spring Cloud Microservices and how they work together.

Spring Boot
Let’s start with a low-level building block of all Spring Cloud components – Spring Boot. Spring Boot easily to create the application from the ground up in minutes(https://start.spring.io/), which gives you core functionality. It has a lot of out-of-the-box features, which are necessary for creating microservices. Like, mapping HTTP verbs(GET, POST, PUT, PATCH and DELETE) to URLs and the serialization and deserialization of JSON/XML protocols to POJO. Although you should keep in mind that Spring Boot is an opinionated view of how the application should be like(configurations, dependencies).

Spring Cloud Config
Spring Cloud Config handles the management of application configuration data(application.yml or application.properties) through a centralized service. Spring Cloud Config as of now, supports Git, File System and Vault backend. Once your application starts up it will request from Spring Cloud Config it’s configuration files.
For example look at example of PiggyMetrics app, all your configurations files located in git repo as such:

Note pls that each .yml file corresponds to application name, as such {applicationName}.yml. One more thing you can configure here is your profile – {application}-{profile}.{yml/properties}.

High-level diagram:

Spring Cloud Service Discovery
Spring Cloud Service Discovery allows you to abstract away the physical location (IP and/or server name) of where your servers are deployed from the clients consuming these services. It also registers, deregisters and checks the health of services. Clients of your services will send a request via logical service name(http://piggy-metrics.com/account-service/getUser), rather than physical location(http://10.4.3.20:6724/getUser). Spring Cloud Service Discovery can be implemented using Consul and Eureka as its service discovery engine.

High-level diagram:

Spring Cloud/Netflix Hystrix and Ribbon
Generally, Spring Cloud heavily integrates with Netflix OSS.
Netflix has created a library called Hystrix that implements the circuit breaker and bulkhead pattern. In other words if service_A calls service_B and last one is not responding, Hystrix allows you to excecute “plan B”, e.g. call service_C.
Another Netflix project called Ribbon, which is a client-side load balancer that gives you a lot of control over the behavior of HTTP and TCP clients. As an example, service_A for some of its data processing needs to call service_B, if service_A uses Ribbon for calls it will automatically load balance all requests between all instances of service_B.

High-level diagram:

Spring Cloud/Netflix Zuul
Spring Cloud uses Netflix Zuul project to provide dynamic routing, authentication and security, multiregion resiliency, stress testing, insights and monitoring capabilities for your microservice application. This list is not full, for more info pls look here.

High-level diagram:

Spring Cloud Sleuth
Spring Cloud Sleuth allows you to integrate unique tracking identifiers into the HTTP calls and message channels(Apache Kafka, RabbitMQ) being used within your application. These tracking numbers(correlation ID) allow you to track a transaction across multiple services in your application. Spring Cloud Sleuth automatically adds these IDs to logging statements.

High-level diagram:

Log will look like this:

account-microservice.log:2016-02-26 11:15:47.561  INFO [account-microservice,2485ec27856c56f4,2485ec27856c56f4,true] 68058 --- [nio-8081-exec-1] i.s.c.sleuth.docs.account-microservice.Application   : Calling statistics-microservice.
statistics-microservice.log:2016-02-26 11:15:47.710  INFO [statistics-microservice,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.statistics-microservice.Application   : Request received from statistics-microservice.
account-microservice.log:2016-02-26 11:15:47.924  INFO [account-microservice,2485ec27856c56f4,9aa10ee6fbde75fa,true] 68059 --- [nio-8082-exec-1] i.s.c.sleuth.docs.statistics-microservice.Application   : Got response from statistics-microservice.

Where 2485ec27856c56f4 is your correlation ID.

Resources used to create this post and recommended for more detailed reading:
Spring Microservices in Action
– Spring Framework documentation
– Netflix documentation
Building Microservices
draw.io

Leave a Reply

Your email address will not be published.