Microservices with NetflixOSS: Ribbon part 4

This is the next post on the services of bulding a microservice with the netflix stack . If you did not read the previous posts i recomed you do:

* NetflixOSS The DevOps Stack for MicroServices - http://diego-pacheco.blogspot.com/2015/09/netflixoss-devops-stack-for.html
* Microservices with NetflixOSS: Karyon, Ribbon and Eureka part 1 - http://diego-pacheco.blogspot.com/2015/09/microservices-with-netflixoss-karyon.html
* Microservices with NetflixOSS: Building and Running Eureka part 2 - http://diego-pacheco.blogspot.com/2015/09/microservices-with-netflixoss-building.html
* Microservices with NetflixOSS: Karyon and Services part 3 - http://diego-pacheco.blogspot.com/2015/09/this-is-next-post-on-services-of.html


Now is the time to introduce Ribbon. So we gonna create a very simple client that will call our REST Service we did on the previous post. We will need eureka client in order to tell ribbon how to get the IP of the service - first of all we need configure the client with this config here: eureka-client.properties



This code could be used for several reasons, for instance this could be another microservice calling our microservice, this also could be a website. We just need 3 classe todo the job :-)

The Classes

ApiFallbackHandler -> This is fall back implementation is the code will be run when service call fails.
ApiResponseValidator -> This validates the response to check if everything is ok.
RibbonClientUsingEureka -> This is the ribbon client using eureka.

ApiResponseValidator

You can use this class to throw errors in case the anwser is not proper - for this case im just logging but keep in my this is not just for logging.



ApiFallbackHandler

This is important for the sense of Anti-fragility because here we can do other thing if the service is down or even return something is wrong or incosistent. The code is kinda of confusing because we need use RxJava to have non-blocking IO.



RibbonClientUsingEureka

Here is were the magic happens. I`m using the Ribbon Code Template to call our service. You can see we are using Eureka Client API to retrieve the service HOST and PORT. When you use Ribbon you are using Hystrix for free so this code is protected - you will have retrys, load balancers, time outs, caching and much more :-)

Lalter i`m just using the Google Gson library to process the JSON response. That`s it you have a microservice calling another microservice using Ribbon, Hystrix, Eureka and Karyon. You also can get the full code on my github https://github.com/diegopacheco/netflixoss-pocs/tree/master/ribbon-eureka-client

Cheers,
Diego Pacheco

Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java