具有MassTransit,RabbitMQ和SignalR的分布式体系结构 [英] Distributed architecture with MassTransit, RabbitMQ and SignalR

查看:303
本文介绍了具有MassTransit,RabbitMQ和SignalR的分布式体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 MassTransit 我必须提供在网页上生成报告的功能,而无需通过单击按钮来重新加载页面,我还应该调用Windows服务进行数据准备(该服务处理每个请求的时间为30秒-1分钟).

I have to provide ability to generate report on a web page without page reloading by click on a button, also I should call a windows service for data preparation (The service handles each request for 30sek - 1min).

基于此示例的首次尝试: https://github.com/MassTransit/Sample-RequestResponse

My first try based on this sample: https://github.com/MassTransit/Sample-RequestResponse

    [HttpPost]
    public async Task<HttpStatusCodeResult> GenerateReport(string someJsonData)
    {    
        var serviceAddress = new Uri(ConfigurationManager.AppSettings["BaseLineRecordService"]);
        var client = this.Bus.CreateRequestClient<ICreateReportRequest, ICreateReportResponse>(serviceAddress, TimeSpan.FromHours(1));
        ICreateReportResponse response = await client.Request(new CreateReportRequest());
        reportHub.ShowRepordData(response); // Update data by SingleR
        return new HttpStatusCodeResult(200);
    }

但是据我了解,这不是一种更好的方法,因为我在所有数据准备过程中都保持连接.

But as I understand it' not a better approach, because I'm keeping connection during all data preparation.

我已经阅读了许多文章,并且发现了三种方法.首选哪种方式?

I've read many articles and I have found three ways. Which way is preferred?

1)喜欢这篇文章 http://www .maldworth.com/2015/07/19/signalrchat-with-masstransit-v3/

2)首先,但是使用Rest API调用,而不是从IIS端使用消费者

2) As first but with Rest API calling instead of Consumers from IIS side

3)本文的想法

推荐答案

我使用SignalR的集线器进行此操作,并使用常规MassTransit使用者观察服务器上的事件.当观察到事件时,我触发事件处理程序,该事件处理程序使用集线器调度到连接的客户端.这样一来,事件便立即被推送到浏览器,而不会在控制器中的服务器上留下未决的异步调用.

I do this using hubs from SignalR, and observe events at the server using regular MassTransit consumers. When events are observed, I trigger the event handler, which dispatches using the Hub to connected clients. That way, the events are pushed down to the browser instantly without leaving an async call pending at the server in a controller.

您可以在 Fooidity 中看到它,它执行类似的操作:

You can see this in Fooidity which does something similar:

https://github.com/phatboyg/Fooidity/blob/develop/src/Fooidity.Management.Web/Hubs/ApplicationHubEventHandler.cs#L18

使用GlobalHost解析集线器,然后在集线器上提出方法.可以通过使用组来区分事件上下文,这些组是每个节点处理的SignalR功能.因此,只要每个节点都在观察事件,客户端就可以连接到任何集线器并得到通知.这对于负载均衡非常有效,而不必为SignalR使用沉重的群集背板-因为RabbitMQ具有超轻量级的事件分发功能.

Using the GlobalHost to resolve the Hub, then raising the method on the hub. The event context can be discriminated by using groups, which are a SignalR feature that is handled per node. So as long as every node is observing the event, clients can be connected to any hub and get notified. This works nicely for load balancing, without having to use a heavy cluster backplane for SignalR -- since RabbitMQ is super lightweight for event distribution.

您也可以对非持久队列执行此操作,这使其速度更快-因为服务器重置/连接丢失的可能性大于代理崩溃的可能性.

You can do it with non-durable queues as well, which makes it even faster -- since a server reset/connection drop is more likely than a broker crash.

身份验证在ApplicationHub内部进行处理,如相邻的源文件中所示: https://github.com/phatboyg /Fooidity/blob/develop/src/Fooidity.Management.Web/Hubs/ApplicationHub.cs

Authentication is handled inside the ApplicationHub, as shown in the adjacent source file: https://github.com/phatboyg/Fooidity/blob/develop/src/Fooidity.Management.Web/Hubs/ApplicationHub.cs

检查一下,希望对您有所帮助.

Check it out, hopefully it helps.

这篇关于具有MassTransit,RabbitMQ和SignalR的分布式体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆