Microservice Communication

In this section wqe are going to look at how microservices can communicate with each other using Spring Cloud, there are a number of ways they can do this

In the first example the microservice will use a REST template going via the Eureka Discovery Service to communicate with one or more instances of a service that it requires, this can be seen in the diagram below


For this tutorial a album service has been created with simply returns a list of albums as per the below secreenshots.

Then in the microsservice that needs to use the Albums service we can use the ResTemplate to send a GET request to Albums Service via th Eureka Discovery service.


We are going to use a client called Feign which is a declarative HTTP client, we can use the @FeignClient annotation that specifies the service that we want to use, also feign also supports client load balancing which works wit the Eureka Discovery Service. First we need to pull in a dependency


To enable Feign Client we add the @EnableFeignClients annotation to the applications main program file as per below


To create a new feign client we first create an interface which has the @GetMapping annotation and is used to call the Albums microservice to get a list of the users albums. To handle feign exceptions you could use the tried and trusted try-catch block capturing any FeignExceptions, however we can use the Feign error decoder, which is a bit more complex than try-catch block, you can access the HTTP response code and the payload to create a better error message to the user of logger. The FeignErrorDecoder class basically decodes the response header to get things like the status code and body, this allows use to send a custom error message based on the error, notice thats its a component which means it will be available for dependency injection. I will discuss the @EnableCircuitBreaker in a moment.

Using Hystrix circuit breaker we can create a fallback method to use if a microservice is not responding, I have an example of this in the above left-hand screenshot, Hystrix will monitoring the services and if not available will redirect to the fallback method, once the service is available it will then redirect to the service all the code to do this is already written and available if you pull in the Hystrix dependency (left-hand screenshot). To enable the Hystrix curcuit we use the @EnableCircuitBreaker annotation which I showed earlier, and add a property to the property file as per below (right-hand screenshot). The we can use the fallback property to the @FeignClient annotation which will point to a fallback class, notice that inside the fallback method the same method name is used in that of the interface getAlbums. It will be this method that will be called if the microservice is not available, in this case we return an empty list. We use the FallBackfactory which gives use access to throwable and thus we can now handle specific exceptions and for example update the logger but still contain to send back an empty album list.


Just with the interface coded (I will talk about the fallback in a moment) we can inject this into the Users service and use it to get a list of Albums as per below


You can enable logging of the HTTP request regarding the feign client, this is help for debugging. First we set a property value, this valuie needs the full package and interface name as per below, we can then set the level of debugging.


Then we need to add the logger to the main application file as per below, notice that we return a feign Logger object, there are differen level that you can set for example FULL, BASIC, etc.