繁体   English   中英

如何在节点应用程序中配置Hystrixjs?

[英]How to configure Hystrixjs in a node app?

我正在尝试将hystrixJS配置到我的一个nodejs应用程序中。 我想结束我的应用程序正在制作的几个外部依赖项。 https://www.npmjs.com/package/hystrixjs

我读了自述文件,但我仍然无法得到如何用这个hystrix包装我的依赖调用以及如何为此配置仪表板。 如果有人已经尝试过这个,请给我一些指示。

谢谢。

您可以在repo中的示例应用程序中找到示例。 但也可以随意提交关于bitbucket的问题,我将尝试提供更多示例。

通常,您可以包装任何返回promise的函数,它不必是http请求,尽管它是最常见的用例。

仪表板不是hystrix本身的一部分。 它的工作方式,您可以在本地运行仪表板,查看此处的说明,然后将端点添加到您的应用程序以公开指标。 示例应用程序显示了如何执行此操作:

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);

然后,您可以将URL粘贴到仪表板中,它将显示您的命令。

如果有帮助,请告诉我

一个可能有用的抽象:

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 :调用外部服务的函数
  • fallbackFn :如果服务停止或缓慢,则调用该函数

Hystrixjs自述文件中 创建命令下的更多配置选项

我也做了一个帖子

如果你使用hapi服务器,你可以创建sse数据:

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()
                }
            )
        }
    }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM