1.消息的优先级
假如现在有个需求,我们需要让一些优先级最高的通知推送到客户端,我们可以使用redis的sortedset,也可以使用我们今天要说的rabbit的消息优先级属性
Producer代码
using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; namespace RabbitMQConsole { class Program { static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory(); factory.HostName = "39.**.**.**"; factory.Port = 5672; factory.VirtualHost = "/"; factory.UserName = "root"; factory.Password = "root"; var exchange = "change4"; var route = "route2"; var queue9 = "queue9"; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange, type: "fanout", durable: true, autoDelete: false); //x-max-priority属性必须设置,否则消息优先级不生效 channel.QueueDeclare(queue9, durable: true, exclusive: false, autoDelete: false,arguments: new Dictionary<string, object> { { "x-max-priority", 50 } }); channel.QueueBind(queue9, exchange, queue9); while(true) { var messagestr = Console.ReadLine(); var messagepri = Console.ReadLine(); var props = channel.CreateBasicProperties(); props.Persistent = true; props.Priority = (byte)int.Parse(messagepri);//设置消息优先级 channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes(messagestr)); } } } } } }