由于时间和精力,以及文档资料的可见性,我在我的POC和这个系列博文的准备中,只会使用到MassTransit和CAP这两个开源项目。
三、MassTransit Quick Start这里以MassTransit + RabbitMQ为例子,首先请确保安装了RabbitMQ,如果没有安装,可以阅读我的《基于EasyNetQ使用RabbitMQ消息队列》去把RabbitMQ先安装到你的电脑上。另外,RabbitMQ的背景知识也有一堆,有机会也还是要了解下Exchange,Channel、Queue等内容。
3.1 最简单的发送/接收实例(1)准备两个控制台程序,一个为Sender(发送者),一个为Receiver(接收者),并分别通过NuGet安装MassTransit以及MassTransit.RabbitMQ
NuGet>Install-Package MassTransit
NuGet>Install-Package MassTransit.RabbitMQ
(2)编写Sender
public class Program { public static void Main(string[] args) { Console.Title = "MassTransit Client"; var bus = Bus.Factory.CreateUsingRabbitMq(cfg => { var host = cfg.Host(new Uri("rabbitmq://192.168.80.71/EDCVHOST"), hst => { hst.Username("admin"); hst.Password("edison"); }); }); var uri = new Uri("rabbitmq://192.168.80.71/EDCVHOST/Qka.MassTransitTest"); var message = Console.ReadLine(); while (message != null) { Task.Run(() => SendCommand(bus, uri, message)).Wait(); message = Console.ReadLine(); } Console.ReadKey(); } private static async void SendCommand(IBusControl bus, Uri sendToUri, string message) { var endPoint = await bus.GetSendEndpoint(sendToUri); var command = new Client() { Id = 100001, Name = "Edison Zhou", Birthdate = DateTime.Now.AddYears(-18), Message = message }; await endPoint.Send(command); Console.WriteLine($"You Sended : Id = {command.Id}, Name = {command.Name}, Message = {command.Message}"); } }