RabbitMq 路由模式 讲解
前面讲到的 将交换机设置成fanout,进行消息广播的模式,对消息没有任何的控制,是一种简单无脑的模式,没法对消息进行过滤和控制
步骤
1 声明交换机时设置 交换机类型为direct
channel.ExchangeDeclare(exchange: “direct_logs”, type: “direct”);
2 发布消息到交换机时设置 routingKey
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: “direct_logs”,
routingKey: severity,
basicProperties: null,
body: body);
that ‘severity’ can be one of ‘info’, ‘warning’, ‘error’
3 订阅者的也同样的设置相应的routingKey
var queueName = channel.QueueDeclare().QueueName;
foreach(var severity in args)
{
channel.QueueBind(queue: queueName,
exchange: “direct_logs”,
routingKey: severity);
}
发布者代码
using System;using System.Linq;using RabbitMQ.Client;using System.Text;
class EmitLogDirect
{
public static void Main(string[] args)
{
var factory = new Connect