I am trying to configure hystrixJS into one of my nodejs app. I want to wrap up couple of external dependencies which my app is making. https://www.npmjs.com/package/hystrixjs
I read the readme but I still couldn't get how can i wrap my dependency call with this hystrix and how to configure a dashboard for this. If anyone already tried this before please give me some directions.
Thanks.
2 Answers
Answers 1
Here's official tutorial for doing that.
This library is inspired by the by the the Netflix Hystrix module for Java applications, which "is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable".
Answers 2
A very very simple example to understand how wrap your services.
First we need a service which returns a Q promise. I.e. a XMLHttpRequest:
function xhrService(options) { var deferred = Q.defer(), req = new XMLHttpRequest(); req.open(options.method || 'GET', options.url, true); // Set request headers if provided. Object.keys(options.headers || {}).forEach(function (key) { req.setRequestHeader(key, options.headers[key]); }); req.onreadystatechange = function(e) { if(req.readyState !== 4) { return; } if([200,304].indexOf(req.status) === -1) { deferred.reject(new Error('Server responded with a status of ' + req.status)); } else { deferred.resolve(e.target.result); } }; req.send(options.data || void 0); return deferred.promise; }
Now you have to create or get a hystrixJS command for the service using CommandsFactory that will run your service.
var serviceCommand = CommandsFactory.getOrCreate("ComandKey") .circuitBreakerErrorThresholdPercentage(service.errorThreshold) .timeout(service.timeout) .run(xhrService) // your service here .circuitBreakerRequestVolumeThreshold(service.concurrency) .circuitBreakerSleepWindowInMilliseconds(service.timeout) .statisticalWindowLength(10000) .statisticalWindowNumberOfBuckets(10) .errorHandler(isErrorHandler) .build();
Next step is launching the command using execute with the arguments xhrService
should get.
var promise = serviceCommand.execute(arguments);
Now it is a matter of dealing with promises in your code (There are tons of info in the net about working with promises) and read a lot of Hystrix documentation to get a idea about how it works and what can you do with it.
0 comments:
Post a Comment