1.RabbitMQ.Client
2.EasyNetQ
3.定义一个MyMessage自定义实体类
public class MyMessage
{
public string Text { get; set; }
}
发送到所对应的全部队列,好处是处理非常快,原理是它无需设置路由Key
using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhouYu.RabbitMQDemo01
{
class Program
{
static void Main(string[] args)
{
//创建MyMessage类对象
MyMessage message = new MyMessage();
//调用生产者方法
ProducerMessage(message);
}
///
/// 生产者
///
///
private static void ProducerMessage(MyMessage msg)
{
//连接MQ,返回一个实例
var advancedBus = CreateAdvancedBus();
//判断mq是否连接
if (advancedBus.IsConnected)
{
//定义一个交换机和交换机模式是广播
var exchange = advancedBus.ExchangeDeclare("net02Fanout", ExchangeType.Fanout);
//发送消息
advancedBus.Publish(exchange, "", false, new Message(msg));
}
else
{
Console.WriteLine("Can't connect");
}
}
///
/// 建立MQ连接
///
///
public static IAdvancedBus CreateAdvancedBus()
{
string connString = "host=127.0.0.1:5672;virtualHost=/;username=zhangweizhong;password=weizhong1988";
if (connString == null || connString == string.Empty)
throw new Exception("messageserver connection string is missing or empty");
return RabbitHutch.CreateBus(connString).Advanced;
}
}
}
using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhouYu.RabbitMQDemo02
{
class Program
{
static void Main(string[] args)
{
//调用消费者方法
ConsumeMessage();
}
///
/// 消费者
///
private static void ConsumeMessage()
{
//连接MQ,返回一个实例
var advancedBus = CreateAdvancedBus();
//定义一个交换机和交换机模式是广播
var exchange = advancedBus.ExchangeDeclare("net02Fanout", ExchangeType.Fanout);
//创建三个队列
var queue1 = advancedBus.QueueDeclare("queue1.notice.wangwu");
var queue2 = advancedBus.QueueDeclare("queue2.notice.wangwu");
var queue3 = advancedBus.QueueDeclare("queue3.notice.wangwu");
//绑定队列--第一个参数是交换机,第二个是队列名称
advancedBus.Bind(exchange, queue1, "net02Fanout.queue1.wangwu");
advancedBus.Bind(exchange, queue2, "net02Fanout.queue2.wangwu");
advancedBus.Bind(exchange, queue3, "net02Fanout.queue3.wangwu");
//advancedBus.Consume(queue1, (body, properties, info) => Task.Factory.StartNew(() =>
//{
// var message = Encoding.UTF8.GetString(body);
// Console.WriteLine("Got message: '{0}'", message);
//}));
}
///
/// 建立MQ连接
///
///
public static IAdvancedBus CreateAdvancedBus()
{
string connString = "host=127.0.0.1:5672;virtualHost=/;username=zhangweizhong;password=weizhong1988";
if (connString == null || connString == string.Empty)
throw new Exception("messageserver connection string is missing or empty");
return RabbitHutch.CreateBus(connString).Advanced;
}
}
}
发送到某个特定的队列,单点无需定义交换机,使用的是默认交换机,直接定义队列
using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhouYu.RabbitMQDemo01
{
class Program
{
static void Main(string[] args)
{
//创建MyMessage类对象
MyMessage message = new MyMessage();
//调用生产者方法
ProducerMessage(message);
}
///
/// 生产者
///
///
private static void ProducerMessage(MyMessage msg)
{
//连接MQ,返回一个实例
var advancedBus = CreateAdvancedBus();
//判断mq是否连接
if (advancedBus.IsConnected)
{
//定义一个队列
var queue = advancedBus.QueueDeclare("net02direct.notice.zhangsan");
//发送信息
advancedBus.Publish(Exchange.GetDefault(), queue.Name, false, new Message(msg));
}
else
{
Console.WriteLine("Can't connect");
}
}
///
/// 建立MQ连接
///
///
public static IAdvancedBus CreateAdvancedBus()
{
string connString = "host=127.0.0.1:5672;virtualHost=/;username=zhangweizhong;password=weizhong1988";
if (connString == null || connString == string.Empty)
throw new Exception("messageserver connection string is missing or empty");
return RabbitHutch.CreateBus(connString).Advanced;
}
}
}
using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhouYu.RabbitMQDemo02
{
class Program
{
static void Main(string[] args)
{
//调用消费者方法
ConsumeMessage();
}
///
/// 消费者
///
private static void ConsumeMessage()
{
//连接MQ
var advancedBus = CreateAdvancedBus();
//定义交换机
var exchange = advancedBus.ExchangeDeclare("net02direct", ExchangeType.Direct);
//定义队列
var queue = advancedBus.QueueDeclare("net02direct.notice.zhangsan");
//绑定队列
advancedBus.Bind(exchange, queue, "net02direct.notice.zhangsan");
advancedBus.Consume(queue, (body, properties, info) => Task.Factory.StartNew(() =>
{
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Got message: '{0}'", message);
}));
}
///
/// 建立MQ连接
///
///
public static IAdvancedBus CreateAdvancedBus()
{
string connString = "host=127.0.0.1:5672;virtualHost=/;username=zhangweizhong;password=weizhong1988";
if (connString == null || connString == string.Empty)
throw new Exception("messageserver connection string is missing or empty");
return RabbitHutch.CreateBus(connString).Advanced;
}
}
}
模糊区域在三种中最为灵活,可根据某种规则去匹配接收对应的消息
using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhouYu.RabbitMQDemo01
{
class Program
{
static void Main(string[] args)
{
//调用生产者方法
ProducerMessage();
}
//定义一个路由Key
private static String[] routingKeys = { "adtec.laoliu", "ali.xiaowang", "tencent.laozhang", "adtec.xiaohu" };
///
/// 生产者--模糊区域
///
///
private static void ProducerMessage()
{
//连接MQ
var advancedBus = CreateAdvancedBus();
//判断是否连接
if (advancedBus.IsConnected)
{
//定义一个交换
var exchange = advancedBus.ExchangeDeclare("net02topic", ExchangeType.Topic);
//遍历
foreach (var routingKey in routingKeys)
{
//创建MyMessage类对象
MyMessage myMessage = new MyMessage();
myMessage.Text = "HelloWorld" + "' from " + routingKey;
//发送信息
advancedBus.Publish(exchange, routingKey, false, new Message(myMessage));
Console.WriteLine("send message: '{0}'", myMessage.Text);
}
}
else
{
Console.WriteLine("Can't connect");
}
}
///
/// 建立MQ连接
///
///
public static IAdvancedBus CreateAdvancedBus()
{
string connString = "host=127.0.0.1:5672;virtualHost=/;username=zhangweizhong;password=weizhong1988";
if (connString == null || connString == string.Empty)
throw new Exception("messageserver connection string is missing or empty");
return RabbitHutch.CreateBus(connString).Advanced;
}
}
}
using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZhouYu.RabbitMQDemo02
{
class Program
{
static void Main(string[] args)
{
//调用消费者方法
ConsumeMessage();
}
///
/// 消费者--模糊区域
///
private static void ConsumeMessage()
{
//连接MQ
var advancedBus = CreateAdvancedBus();
//定义交换机
var exchange = advancedBus.ExchangeDeclare("net02topic", ExchangeType.Topic);
//定义队列
var queue = advancedBus.QueueDeclare("net02topic.notice.zhangsan");
//绑定队列
advancedBus.Bind(exchange, queue, "#.adtec.#");
advancedBus.Consume(queue, (body, properties, info) => Task.Factory.StartNew(() =>
{
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Got message: '{0}'", message);
}));
}
///
/// 建立MQ连接
///
///
public static IAdvancedBus CreateAdvancedBus()
{
string connString = "host=127.0.0.1:5672;virtualHost=/;username=zhangweizhong;password=weizhong1988";
if (connString == null || connString == string.Empty)
throw new Exception("messageserver connection string is missing or empty");
return RabbitHutch.CreateBus(connString).Advanced;
}
}
}