Masstransit 使用 RabbitMQ 性能很慢?

时间:2023-04-26
本文介绍了Masstransit 使用 RabbitMQ 性能很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在没有 Masstransit 的情况下使用 RabbitMQ,每秒发送 10,000 条消息和 100 秒内发送 100 万条消息.

I used RabbitMQ without Masstransit and send 10,000 message/per sec and One million message in 100 second.

但是在将 Masstransit 与 RabbitMQ 一起使用后,我的机器上的性能非常低.

But after using Masstransit with RabbitMQ the performance is very low in my machine.

当发布/消费消息时,硬盘非常活跃(99% 的使用率),并且此进程的 CPU 活动几乎为 0%.

The hard disk is very active (99% usage) when publish/consume message and CPU activity for this process is almost 0%.

当使用此代码运行发布者/订阅者控制台应用程序时:

When the run Publisher/Subscriber console application with this code :

var bus = ServiceBusFactory.New(x =>
{
    x.UseRabbitMq();
    x.ReceiveFrom("rabbitmq://localhost/Example_Hello");
});
var message = new MyMessage() { Text = "hello", When = DateTime.Now };
for (int i = 0; i < 100; i++)
{
    bus.Publish<MyMessage>(message, x => { });
}

在 6 秒内发布了 100 条消息,我不知道为什么很慢.

Published 100 message in 6 second and I don't know why is very slow.

我的机器配置和软件版本是:

My machine's configuration and software version is:

Windows 8.1 64 位

Windows 8.1 64bit

英特尔酷睿 i3 3.30GHz

Intel Core i3 3.30GHz

内存 8GB

Visual Studio 2013 C#.Net 4.5.1

Visual Studio 2013 C#.Net 4.5.1

Erlang 6.3

RabbitMQ 3.4.4

RabbitMQ 3.4.4

捷运 2.9.9

RabbitMQ.Client 3.4.0

RabbitMQ.Client 3.4.0

推荐答案

这是因为在幕后,MassTransit 正在等待 RabbitMQ 对消息进行 Ack 消息,确保消息被代理成功接受在返回给调用者之前.如果没有这种等待,如果代理无法接收写入,则消息可能会丢失.

This is because under the covers, MassTransit is waiting for RabbitMQ to Ack the message, ensuring that it was successfully accepted by the broker before returning to the caller. Without this wait, if the broker fails to receive the write, the message could be lost.

使用 MassTransit v3(目前处于预发布状态),Publish 方法(以及所有 Send 方法)现在是异步的,返回一个 Task 可以等待(或者不等待,如果你不关心结果的话).

With MassTransit v3 (currently in pre-release), the Publish method (and all Send methods) are now async, returning a Task that can be awaited (or not, if you don't care about the outcome).

我可以为 .NET 4.0 开发人员添加一个 PublishAsync 方法到版本 2.9.9,事实上,这就是我最终可能会做的临时解决方法,用于仍然使用当前稳定版本的应用程序.将 NoWait 选项添加到 SendContext 也很有用,允许应用程序选择退出 Ack 等待行为.

I could add a PublishAsync method for .NET 4.0 developers to version 2.9.9, in fact, that's what I may end up doing as a temporary workaround for applications still using the current stable version. It may also be useful to add a NoWait option to the SendContext, allowing the application to opt-out of the Ack wait behavior.

老实说,在我的用例中,我只是更喜欢持久性而不是性能.

I just favor durability over performance in my use case, honestly.

这篇关于Masstransit 使用 RabbitMQ 性能很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 C# 应用程序中导入 JsonConvert? 下一篇:阻止任务在某个线程上运行

相关文章