NServiceBus提供了8種傳輸管道組件,分別是Learning、MSMQ、Azure Service Bus、Azure Service Bus (Legacy)、Azure Storage Queues、SQL Server、RabbitMQ、Amazon SQS。前兩篇咱們主要用的是Learnning,這篇使用RabbitMQ,也能夠直接用於生產。async
RabbitMQ不在NServiceBus下,是一個單獨的組件,須要單獨安裝。
ui
var transport = config.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=192.168.80.129;username=admin;password=admin"); transport.UseDirectRoutingTopology();
class Program { static ILog log = LogManager.GetLogger<Program>(); static void Main(string[] args) { MainAsync().GetAwaiter().GetResult(); } static async Task MainAsync() { Console.Title = "Sample.ClientUI"; var config = new EndpointConfiguration("Sample.ClientUI"); config.UseSerialization<NewtonsoftSerializer>(); config.UsePersistence<InMemoryPersistence>(); config.EnableInstallers(); var transport = config.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=192.168.80.129;username=admin;password=admin"); transport.UseDirectRoutingTopology(); var endpointInstance = await Endpoint.Start(config).ConfigureAwait(false); await RunAsync(endpointInstance).ConfigureAwait(false); await endpointInstance.Stop().ConfigureAwait(false); } static async Task RunAsync(IEndpointInstance endpointInstance) { log.Info("Press 'P' to send an PlaceOrder Command"); while (true) { var key = Console.ReadKey(); Console.WriteLine(); switch (key.Key) { case ConsoleKey.P: { var command = new PlaceOrder { OrderId = Guid.NewGuid().ToString() }; log.Info($"Sending PlaceOrder with OrderId:{command.OrderId}"); await endpointInstance.Send("Sample.Server",command).ConfigureAwait(false); break; } case ConsoleKey.Q: return; default: log.Info("Please try again"); break; } } } }
先啓動ClientUI,啓動後先發送幾條數據,發送數據觀察程序控制臺數據是否發送,而後在RabbitMQ控制檯裏觀察Queue的狀況,沒建立Queue時,這裏會自動建立Queue,隊列名稱和端點名稱相同。這裏發送消息數據,數據會在Sample.Server隊列裏。
3d
這裏能看到在隊列Sample.Server裏有三條未消費的數據,由於我尚未啓動Sample.Server控制檯程序。code
public class Program { static ILog log = LogManager.GetLogger<Program>(); static void Main(string[] args) { MainAsync().GetAwaiter().GetResult(); } static async Task MainAsync() { Console.Title = "Sample.Server"; var config = new EndpointConfiguration("Sample.Server"); config.UseSerialization<NewtonsoftSerializer>(); config.UsePersistence<InMemoryPersistence>(); config.EnableInstallers(); var transport = config.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=192.168.80.129;username=admin;password=admin"); transport.UseDirectRoutingTopology(); var endpointInstance = await Endpoint.Start(config).ConfigureAwait(false); log.Info("Press any key to quit"); Console.ReadKey(); await endpointInstance.Stop().ConfigureAwait(false); } }
public class PlaceOrderHandler : IHandleMessages<PlaceOrder> { static ILog log = LogManager.GetLogger<PlaceOrderHandler>(); public Task Handle(PlaceOrder message, IMessageHandlerContext context) { log.Info($"Received PlaceOrder with OrderId:{message.OrderId}"); return Task.CompletedTask; } }
啓動Sample.Server控制檯後,就會消費Queue裏的數據,消費完成後觀察程序控制臺的提示和RabbitMQ控制檯的提示。
server
這裏能看到剛纔堆積的三條數據已經被消費了。blog
寫完這個demo我就在想在程序裏經過使用NServiceBus的RabbitMQ組件和直接在程序裏使用RabbitMQ直接調用的區別,到底有沒有必要經過NServiceBus調用,還須要進一步考量。rabbitmq