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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PinCheckDependency extends HystrixCommand<Boolean> { | |
private HttpClient httpClient; | |
public PinCheckDependency(HttpClient httpClient) { | |
super(HystrixCommandGroupKey.Factory.asKey("PinCheckService")); | |
this.httpClient = httpClient; | |
} | |
@Override | |
protected Boolean run() throws Exception { | |
HttpGet pinCheck = new HttpGet("http://localhost:9090/pincheck"); | |
HttpResponse pinCheckResponse = httpClient.execute(pinCheck); | |
int statusCode = pinCheckResponse.getStatusLine().getStatusCode(); | |
if (statusCode != 200) { | |
throw new RuntimeException("Oh dear no pin check , status code " + statusCode); | |
} | |
String pinCheckInfo = EntityUtils.toString(pinCheckResponse.getEntity()); | |
return Boolean.valueOf(pinCheckInfo); | |
} | |
@Override | |
public Boolean getFallback() { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defaultHystrixConfig: | |
hystrix.command.DeviceServiceDependency.execution.isolation.thread.timeoutInMilliseconds: 2000 | |
hystrix.threadpool.PinCheckService.maxQueueSize: 50 | |
hystrix.threadpool.PinCheckService.coreSize: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NotNull | |
@JsonProperty | |
private Map<String, Object> defaultHystrixConfig; | |
public Map<String, Object> getDefaultHystrixConfig() { | |
return defaultHystrixConfig; | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void run(AppConfig appConfig, Environment environment) throws Exception { | |
ConfigurationManager.install(new MapConfiguration(appConfig.getDefaultHystrixConfig())); | |
... | |
} |
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.
3 comments:
Great post.How do you mix in archaius.properties or config.properties with DynamicPropertyFactory .getInstance().getXXX() to get dynamically changed values along with -Darchaius.configurationSource.additionalUrls=file:///%CATALINA_HOME%/../archaius.properties ? The last comment about using Zookeeper for dynamic values i snot clear to me.
Hi Vijay
Hystrix uses archaius so you can use any supported archaius mechanism.
The docs for Zookeeper support are here:
https://github.com/Netflix/archaius/wiki/ZooKeeper-Dynamic-Configuration
Or take a look at this full example from one of my colleagues: https://github.com/stuartgunter/distributed-config
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.
Post a Comment