Saturday, August 23, 2014

Using Hystrix with Dropwizard

I've previously blogged about Hystrix/Tenacity and Breakerbox. The full code and outline of the example is located here.

Subsequently I've been using Dropwizard and Hystrix without Tenacity/Breakerbox and found it far simpler. I don't see a great deal of value in adding Tenacity and Breakerbox as Hystrix uses Netflix's configuration library Archaius which already comes with dynamic configuration via files, databases and Zookeeper.

So lets see what is involved in integrating Hystrix and Dropwizard.

The example is the same from this article. Briefly, it is a single service that calls out to three other services:
  • A user service
  • A pin check service
  • A device service

To allow the application to reliably handle dependency failures we are going to call out to each of the three services using Hystrix commands. Here is an example of calling out using the Apache Http Client to a pin check service:

To execute this command you simply instantiate an instance and call execute(), Hystrix handles creating a work queue and thread pool. Each command that is executed with the same group will use the same work queue and thread pool. You tell Hystrix the group by passing it to super() when extending a Hystrix command. To configure Hystrix in a Dropwizard like way we can add a map to our Dropwizard YAML:

This will translate to a Map in your Dropwizard configuration class:

The advantage of using a simple map rather than a class with the property names matching Hystrix property names is this allows you to be completely decoupled from Hystrix and its property naming conventions. It also allows users to copy property names directly from Hystrix documentation into the YAML.

To enable Hystrix to pick these properties up it requires a single line in your Dropwizard application class. This simplicity is due to the fact that Hystrix uses Archaius for property management.

Now you can add as any of Hystrix's many properties to your YAML. Then later extend the Configuration you install to include a dynamic configuration source such as ZooKeeper.

I hope this shows just how simple it is to use Hystrix with Dropwizard without bothering with Tenacity. A full working example is on github


Vijay Balakrishnan said...

Great post.How do you mix in or with DynamicPropertyFactory .getInstance().getXXX() to get dynamically changed values along with -Darchaius.configurationSource.additionalUrls=file:///%CATALINA_HOME%/../ ? The last comment about using Zookeeper for dynamic values i snot clear to me.

chbatey said...

Hi Vijay

Hystrix uses archaius so you can use any supported archaius mechanism.

The docs for Zookeeper support are here:

Or take a look at this full example from one of my colleagues:

Dim4ksan said...

I'd want to introduce myself to everyone. During my time in school, this site and its "pay someone to take my test" service have been invaluable. You will be replaced by a group of individuals who will do your task. When I worked on it for several months, there were no issues. As far as feasible, every work was done on schedule and under budget The idea is one that I strongly recommend to all of my friends and to you as well.