Using Spring Boot with the Netfix OSS toolset is easy thanks to some excellent integration libraries. In a modern microservice based application it has become even more important to handle downstream failures properly and Netflix have provided a particularly neat solution for that.
First let's cover what each component does in this application. In this tutorial I'm going to leave Eureka out, it would often be used for application discovery but its not always easy to add to an existing application.
Hystrix - Latency and fault tolerance library that will provide a circuit breaker to help with external failures.
Lets imagine we need to build an application that has to interact with a list of products from another application. The list of products is managed by another team (or maybe another company) so don't know anything about how it works and can't change it. Here's a simplified json body on what to expect.
In real life this would API would have a lot more data and some search criteria but it really doesn't matter for our purposes.
So in our pom we first set our up dependency management with the parent project as the Spring Boot starter parent and add the Edgware release as a dependency set. Our list of dependencies is small and only includes the web package, feign, hystrix and ribbon.
The main class is fairly simple, just a standard Spring Boot main method but with the addition of the @EnableFeignClients annotation. This will allow Spring Boot to scan for Feign clients.
The client is an interface that will be implemented by Spring thanks to the @FeignClient annotation. That will cause the client to be created and name becomes the configuration key so you can set the servers. The Feign client will not be able to be autowired into any service that needs it.
The real benefit of using the Hystrix is the option to use a fallback. When a fallback is configured the application will be able to proceed, with a more basic response even if the other service is down.
In the fallback we simply return an empty list of products but in a real scenario this could be pulled from a cache or simply include the most popular products.
Since we're not using Eureka in this example the url of the servers we want to send requests to need to be put into configuration. This can be a useful technique when the service you need to interact with is outside of your control.
The configuration has firstly disabled Eureka, this should be option but if Eureka is on the classpath it will interfere with ribbon configuration so its best to be explicit.
The ribbon configuration uses the name of the Feign client, in this case products so ribbon knows which client to apply it to.
There you have it, an application that includes a client side load balancer and a circuit breaker to provide fault tolerance. Spring Boot does most of the hard work for us so we just need to configure it.