-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add an example on how to use the Hystrix Metrics
- Loading branch information
1 parent
2d44df6
commit fd8246a
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Hystrix Example | ||
|
||
This example shows how to access the Hystrix Metrics and create an SSE stream that can be plugged into the Hystrix Dashboard. | ||
|
||
Similiar to the jQuery example, a simple service is exposed at the route `http://localhost:3000`. As the service receives requests, it gets slower and slower. Once it takes more than 1 second to respond, the service just returns a `423 (Locked)` error. There is also a SSE stream available at `http://localhost:3000/hystrix.stream` | ||
|
||
To see the Hystrix Metrics in action, you will need to run the Hystrix Dashboard. There are instructions on how to do that here: https://github.com/Netflix/Hystrix/tree/master/hystrix-dashboard | ||
|
||
Once the dashboard is running, navigate to `http://localhost:7979/hystrix-dashboard` (this assumes you are running it from the git repo), and add our servers hystrix stream. | ||
|
||
Now make a few requests to `http://localhost:3000` and you should see movement on the dashboard | ||
|
||
|
||
Install the dependecies | ||
|
||
```sh | ||
$ npm install | ||
``` | ||
|
||
Start the server. | ||
|
||
```sh | ||
$ npm start | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
|
||
const app = express(); | ||
|
||
const circuitBreaker = require('../../'); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
const baseline = process.env.CB_BASELINE || 20; | ||
let delay = baseline; | ||
|
||
function flakeFunction () { | ||
return new Promise((resolve, reject) => { | ||
if (delay > 1000) { | ||
return reject('Flakey Service is Flakey'); | ||
} | ||
|
||
setTimeout(() => { | ||
console.log('replying with flakey response after delay of ', delay); | ||
resolve(`Sending flakey service. Current Delay at ${delay}`); | ||
delay = delay * 2; | ||
}, delay); | ||
}); | ||
} | ||
|
||
setInterval(() => { | ||
if (delay !== baseline) { | ||
delay = baseline; | ||
console.log('resetting flakey service delay', delay); | ||
} | ||
}, 20000); | ||
|
||
// circuit breaker | ||
const circuitBreakerOptions = { | ||
maxFailures: 5, | ||
timeout: 5000, | ||
resetTimeout: 10000, | ||
name: 'customName', | ||
group: 'customGroupName' | ||
}; | ||
|
||
function fallback () { | ||
return 'Service Fallback'; | ||
} | ||
|
||
const circuit = circuitBreaker(flakeFunction, circuitBreakerOptions); | ||
circuit.fallback(fallback); | ||
|
||
const hystrixStats = circuit.hystrixStats; | ||
|
||
// setup our SSE endpoint | ||
app.use('/hystrix.stream', (request, response) => { | ||
response.writeHead(200, {'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive'}); | ||
response.write('retry: 10000\n'); | ||
response.write('event: connecttime\n'); | ||
|
||
hystrixStats.getHystrixStream().pipe(response); | ||
}); | ||
|
||
app.use('/', (request, response) => { | ||
circuit.fire().then((result) => { | ||
response.send(result); | ||
}).catch((err) => { | ||
response.send(err); | ||
}); | ||
}); | ||
|
||
module.exports = app; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('hystrix:server'); | ||
var http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === 'string' | ||
? 'Pipe ' + port | ||
: 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "hystrix-example", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./bin/www" | ||
}, | ||
"dependencies": { | ||
"body-parser": "~1.16.0", | ||
"debug": "~2.6.0", | ||
"express": "~4.14.1" | ||
} | ||
} |