How to configure Hystrixjs in a node app?

家住魔仙堡 提交于 2019-12-10 03:54:24

问题


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.


回答1:


You can find a sample in the example app in the repo. But also feel free to submit a question on bitbucket and I will try to provide more examples.

In general you can wrap any function that returns a promise, it does not have to be a http request, although it is the most common use case.

The dashboard is not part of hystrix itself. The way it works, you can run a dashboard locally, see for instructions here and then add an endpoint to your application to expose the metrics. The example app shows how to do it:

function hystrixStreamResponse(request, response) {
    response.append('Content-Type', 'text/event-stream;charset=UTF-8');
    response.append('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate');
    response.append('Pragma', 'no-cache');
    return hystrixStream.toObservable().subscribe(
        function onNext(sseData) {
            response.write('data: ' + sseData + '\n\n');
        },
        function onError(error) {console.log(error);
        },
        function onComplete() {
            return response.end();
        }
    );
};

app.get('/api/hystrix.stream', hystrixStreamResponse);

You can then paste the url into the dashboard and it will display your commands.

Let me know if it helps




回答2:


An abstraction that might help:

import {commandFactory} from "hystrixjs";

export const CommandsBuilder = class CommandsBuilder {

    static createMyCommand({runFn, fallbackFn}){
        return commandFactory.getOrCreate("my-command-name")
            .run(runFn)
            .fallbackTo(fallbackFn)
            .build();
    }

};
  • runFn: The function that calls the external service
  • fallbackFn: The function called if the service is down or slow

More configuration options under Creating a command in Hystrixjs readme

I also did a post on this.




回答3:


In case, you use hapi server, you can create sse data:

use strict'
const hystrixSSEStream = require('hystrixjs').hystrixSSEStream;
module.exports = [
    {
        method: 'GET',
        path: '/hystrix-sse-stream',
        handler: (request, reply) => {
            request.raw.res.writeHead(200, { 'content-type': 'text/event-stream; charset=utf-8',
                'Pragma': 'no-cache',
                'cache-control': 'no-cache, no-store, max-age=0, must-revalidate' })
            hystrixSSEStream.toObservable().subscribe(
                function onNext(sseData) {
                    request.raw.res.write('data: ' + sseData + '\n\n')
                },
                function onError(error) {
                    reply(error)
                },
                function onComplete() {
                    reply.continue()
                }
            )
        }
    }
]


来源:https://stackoverflow.com/questions/35836541/how-to-configure-hystrixjs-in-a-node-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!