地铁可以自动缩放吗?

时间:2019-05-05 09:44:09

标签: c# .net-core rabbitmq masstransit

我计划在工作中的一个项目中使用RabbitMQ。

我正在评估各种类型的客户,并想知道MassTransit是否可以满足我们有关可伸缩性的需求之一。

我在下面写了一个简单的代码:

public class MyMessage
{ 
    public string SomeText { get; set; }
}

public static class Program
{
    public static async Task Main(params string[] args)
    {
        var counter = 0;
        var bus = Bus.Factory.CreateUsingRabbitMq(busFactoryConfigurator =>
        {
            var host = busFactoryConfigurator.Host(new Uri("rabbitmq://localhost"), hostConfigurator =>
            {
                hostConfigurator.Username("guest");
                hostConfigurator.Password("guest");
            });

            busFactoryConfigurator.ReceiveEndpoint(host, "test_queue", endpointConfigurator =>
            {
                endpointConfigurator.Handler<MyMessage>(async context =>
                {
                    var countDoku = counter;
                    counter++;
                    await Console.Out.WriteLineAsync(countDoku.ToString() + ": started " + Thread.CurrentThread.ManagedThreadId);
                    await Task.Delay(500);
                    await Console.Out.WriteLineAsync(countDoku.ToString() + ": done " + Thread.CurrentThread.ManagedThreadId);
                    await Console.Out.WriteLineAsync($"Received: {context.Message.SomeText}");
                });
            });
        });

        await bus.StartAsync();

        Parallel.ForEach(Enumerable.Repeat(42, 5000), async _ => await bus.Publish(new MyMessage {SomeText = "Hi"}));

        Console.ReadKey();

        await bus.StopAsync();
    }
}

它远非完美的基准(例如ConsoleParallel.ForEach仅用于尽可能多地并行执行async个操作),但是我注意到了一些有点尴尬:

0: started 14
1: started 4
2: started 15
3: started 18
4: started 4
5: started 14
6: started 6
7: started 15
12: started 15
13: started 6
10: started 4
11: started 5
8: started 18
9: started 14
14: started 15
15: started 6
0: done 6
Received: Hi
5: done 14
6: done 5
Received: Hi
3: done 13
Received: Hi
12: done 8
Received: Hi
Received: Hi
7: done 18
Received: Hi
2: done 4
Received: Hi
4: done 15
Received: Hi
1: done 18
Received: Hi
11: done 18
Received: Hi
8: done 5
Received: Hi
9: done 14
Received: Hi
10: done 6
Received: Hi
13: done 4
Received: Hi
14: done 8
Received: Hi
15: done 15
Received: Hi
16: started 15
17: started 15
18: started 8
19: started 4
20: started 8
21: started 15
22: started 4
23: started 5
24: started 18
25: started 5
26: started 6
31: started 14
28: started 18
29: started 5
30: started 4
27: started 13
18: done 14
Received: Hi
17: done 13
Received: Hi
16: done 5

处理部分不能同时处理超过15个项目...

我想知道这是我的基准代码问题还是MassTransit配置的限制?我是否应该使用actor框架更好地调度从队列接收的项目负载,以便同时处理更多项目?

0 个答案:

没有答案