一、简介

我用过RabbirMQ的发布订阅模式,以及一对一的延迟队列。

1、RabbitMQ的有消息确认机制,消费一条则队列中少一条,也有对应的消费到消息及认为是消费成功这样的模式,一般使用前者。

发布订阅我是在处理大量数据的更新及与其他系统有数据来往时使用的。在本地程序处理一条则发送一条到队列,保证本地处理成功并发送到其他系统。

延迟队列这种模式也是在与其他系统有交互并且在我这边系统接到成功后必须不马上发给其他的系统,如果在多少时间内本地没有接到说不发的指令才有延迟队列转发到其他系统。

安装部署简介及可视化页面:

https://www.cnblogs.com/Leo_wl/p/5402125.html

2、在c#中的使用

NuGet引用:RabbitMQ.Client

2.1 发布消息

2.1 .1发布订阅

public static void PublishMsgString(IConnection conn, string message, PublishMsgModel model, IDictionary<string, object> headerDict)

{

try

{

using (var channel = conn.CreateModel())

{

channel.ExchangeDeclare(exchange: model.ExchangeName, type: model.ExchangeType, durable: model.Durable);

channel.QueueDeclare(queue: model.QueueName, durable: model.Durable, exclusive: false, autoDelete: false, arguments: null);

channel.QueueBind(queue: model.QueueName, exchange: model.ExchangeName, routingKey: model.RouteKey);

var properties = channel.CreateBasicProperties();

properties.DeliveryMode = 2;

if (headerDict != null)

{

properties.Headers = headerDict;

properties.ContentType = "text/plain";

properties.ContentEncoding = "UTF-8";

}

//string messageString = JsonConvert.SerializeObject(message);

byte[] body = Encoding.UTF8.GetBytes(message);

channel.ConfirmSelect();

channel.BasicPublish(model.ExchangeName, model.RouteKey, properties, body);

if (channel.WaitForConfirms())

{

Common.WriteLog($"【Success】【发布消息】[MsgType]{model.MsgType}[messsage]{message}成功", "publish", model.QueueName);

}

else

{

Common.WriteLog($"【Error】【发布消息】[MsgType]{model.MsgType}[messsage]{message}失败", "publish", model.QueueName);

}

}

}

catch (Exception ex)

{

Common.WriteLog($"【Error_Ex】【发布消息】[MsgType]{model.MsgType}发布消息异常:{ex.Message}[message]{message}", "publish", model.QueueName);

}

}

2.1.2、延迟队列

public static void PublishExpirationMsgString(string message, PublishMsgModel model, PublishMsgModel modelExpiration, Dictionary<string, object> headerDict, bool isTest)

{

try

{

ConnectionFactory connFactory = new ConnectionFactory()

{

HostName = isTest ? ConfigurationManager.AppSettings["MQ.HostName.Test"] : ConfigurationManager.AppSettings["MQ.HostName"],

UserName = isTest ? ConfigurationManager.AppSettings["MQ.UserName.Test"] : ConfigurationManager.AppSettings["MQ.UserName"],

Password = isTest ? ConfigurationManager.AppSettings["MQ.Password.Test"] : ConfigurationManager.AppSettings["MQ.Password"],

VirtualHost = isTest ? ConfigurationManager.AppSettings["MQ.VirtualHostLog.Test"] : ConfigurationManager.AppSettings["MQ.VirtualHostLog"],

Port = int.Parse(isTest ? ConfigurationManager.AppSettings["MQ.Port.Test"] : ConfigurationManager.AppSettings["MQ.Port"]),

AutomaticRecoveryEnabled = true,

RequestedHeartbeat = 30

};

using (IConnection conn = connFactory.CreateConnection())

{

using (var channel = conn.CreateModel())

{

Dictionary<string, object> dic = new Dictionary<string, object>();

dic.Add("x-expires", 4 * 1000 * 60);

dic.Add("x-message-ttl", 3 * 1000 * 60);//队列上消息过期时间,应小于队列过期时间

dic.Add("x-dead-letter-exchange", model.ExchangeName);//过期消息转向路由

dic.Add("x-dead-letter-routing-key", model.RouteKey);//过期消息转向路由相匹配routingkey

channel.QueueDeclare(queue: modelExpiration.QueueName, durable: modelExpiration.Durable, exclusive: false, autoDelete: false, arguments: dic);

channel.ExchangeDeclare(exchange: modelExpiration.ExchangeName, type: modelExpiration.ExchangeType, durable: modelExpiration.Durable);

channel.QueueBind(queue: modelExpiration.QueueName, exchange: modelExpiration.ExchangeName, routingKey: modelExpiration.RouteKey);

var properties = channel.CreateBasicProperties();

properties.DeliveryMode = 2;

if (headerDict != null)

{

properties.Headers = headerDict;

properties.ContentType = "text/plain";

properties.ContentEncoding = "UTF-8";

}

//string messageString = JsonConvert.SerializeObject(message);

byte[] body = Encoding.UTF8.GetBytes(message);

channel.ConfirmSelect();

// properties.Expiration = (2 * 1000 * 60).ToString();

channel.BasicPublish(modelExpiration.ExchangeName, modelExpiration.RouteKey, properties, body);

if (channel.WaitForConfirms())

{

Common.WriteLog($"【Success】【发布消息】[MsgType]{modelExpiration.MsgType}[messsage]{message}", "publish", modelExpiration.QueueName);

}

else

{

Common.WriteLog($"【Error】【发布消息】[MsgType]{modelExpiration.MsgType}[messsage]{message}失败", "publish", modelExpiration.QueueName);

}

}

}

}

catch (Exception ex)

{

Common.WriteLog($"【Error_Ex】【发布消息】[MsgType]{modelExpiration.MsgType}发布消息异常:{ex.Message}[message]{message}", "publish", modelExpiration.QueueName);

}

}

2.2消费消息

public static string GetConfig(string key)

{

return ConfigurationManager.AppSettings[key];

}

public static bool IsTest

{

get { return (ConfigurationManager.AppSettings["IsTest"] ?? "") == "1"; }

}

private static ConnectionFactory connFactory = new ConnectionFactory()

{

HostName = IsTest ? GetConfig("MQ.HostName.Test") : GetConfig("MQ.HostName"),

UserName = IsTest ? GetConfig("MQ.UserName.Test") : GetConfig("MQ.UserName"),

Password = IsTest ? GetConfig("MQ.Password.Test") : GetConfig("MQ.Password"),

VirtualHost = IsTest ? GetConfig("MQ.VirtualHost.Test") : GetConfig("MQ.VirtualHost"),

Port = int.Parse(IsTest ? GetConfig("MQ.Port.Test") : GetConfig("MQ.Port")),

AutomaticRecoveryEnabled = true,

RequestedHeartbeat = 30

};

private static IConnection conn = connFactory.CreateConnection();

private static IModel channel = conn.CreateModel();

private const string queueAllOrder = "user_order_idex_to_log";

private static EventingBasicConsumer consumerOrder,;

private static void ListenMQ(object queue)

{

string[] queueInfo = (string[])queue;

string queueName = queueInfo[0];

string nowThread = queueInfo[1];

PublishMsgModel model = new PublishMsgModel() { Durable = true, ExchangeName = queueName, ExchangeType = ExchangeType.Direct, QueueName = queueName, RouteKey = queueName, MsgType = "" };

channel.BasicQos(0, 1, false);

channel.QueueDeclare(queue: model.QueueName, durable: model.Durable, exclusive: false, autoDelete: false, arguments: null);

switch (queueName)

{

case queueOrder:

model.MsgType = "test";

consumerOrder = new EventingBasicConsumer(channel);

consumerOrder.Received += ConsumOrderMsg;

channel.BasicConsume(model.QueueName, false, consumerOrder);//消费确认,逐条

break;

}

}

private static void ConsumOrderMsg(object sender, BasicDeliverEventArgs eventArgs)

{

string logName = "order_idex_to_devcenter";

try

{

byte[] body = eventArgs.Body;

if (body != null && body.Length > 0)

{

string message = Encoding.UTF8.GetString(body);

++consumOrderMessageTotalOrder;

string msgType = Encoding.UTF8.GetString((byte[])eventArgs.BasicProperties.Headers["messageType"]),

msgId = Encoding.UTF8.GetString((byte[])eventArgs.BasicProperties.Headers["messageId"]), userId = Encoding.UTF8.GetString((byte[])eventArgs.BasicProperties.Headers["userId"]);

}catch{}

}

RabbitMQ及延时队列的更多相关文章

  1. 基于rabbitMQ 消息延时队列方案 模拟电商超时未支付订单处理场景

    前言 传统处理超时订单 采取定时任务轮训数据库订单,并且批量处理.其弊端也是显而易见的:对服务器.数据库性会有很大的要求,并且当处理大量订单起来会很力不从心,而且实时性也不是特别好 当然传统的手法还可 ...

  2. rabbitMq实现延时队列

    原文:https://my.oschina.net/u/3266761/blog/1926588 rabbitMq是受欢迎的消息中间件之一,相比其他的消息中间件,具有高并发的特性(天生具备高并发高可用 ...

  3. RabbitMq 实现延时队列-Springboot版本

    rabbitmq本身没有实现延时队列,但是可以通过死信队列机制,自己实现延时队列: 原理:当队列中的消息超时成为死信后,会把消息死信重新发送到配置好的交换机中,然后分发到真实的消费队列: 步骤: 1. ...

  4. 【日常摘要】- RabbitMq实现延时队列

    简介 什么是延时队列? 一种带有延迟功能的消息队列 过程: 使用场景 比如存在某个业务场景 发起一个订单,但是处于未支付的状态?如何及时的关闭订单并退还库存? 如何定期检查处于退款订单是否已经成功退款 ...

  5. rabbitmq实现延时队列(死信队列)

    基于队列和基于消息的TTL TTL是time to live 的简称,顾名思义指的是消息的存活时间.rabbitMq可以从两种维度设置消息过期时间,分别是队列和消息本身. 队列消息过期时间-Per-Q ...

  6. Rabbitmq的延时队列的使用

    配置: spring: rabbitmq: addresses: connection-timeout: username: guest password: guest publisher-confi ...

  7. rabbitmq 安装延时队列插件rabbitmq-delayed-message-exchange

    1.下载rabbitmq-delayed-message-exchange(注意版本对应) 链接:https://github.com/rabbitmq/rabbitmq-delayed-messag ...

  8. IOS IAP 自动续订 之 利用rabbitmq延时队列自动轮询检查是否续订成功

    启用针对自动续期订阅的服务器通知: - 官方地址: - https://help.apple.com/app-store-connect/#/dev0067a330b - 相关字段, 相关类型地址:  ...

  9. 面试官:RabbitMQ过期时间设置、死信队列、延时队列怎么设计?

    哈喽!大家好,我是小奇,一位不靠谱的程序员 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 文章持续更新 一.前言 RabbitMQ我们经常的使用, ...

随机推荐

  1. EVE模拟器的配置

    (注:本文整理自达叔的EVE模拟器使用说明https://blog.51cto.com/dashu666/1971728) 基础部署篇 所需要准备的东西: 1.VMWare (虚拟化软件,用来承载模拟 ...

  2. Winsock select server 与 client 示例代码

    参考 https://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancediomethod5.html ...

  3. 反向代理负载均衡之haproxy

    在上篇安装的nginx的机器环境上将nginx停掉 /usr/local/nginx/sbin/nginx -s stop 在linux-node2上编译安装haproxy作为反向代理服务器 [roo ...

  4. element-ui 通用表单封装及VUE JSX应用

    一.存在及需要解决的问题 一般在做后台OA的时候会发现表单重复代码比较多,且逻辑基本一样,每次新加一个表单都需要拷贝基本一致的代码结构,然后只是简单地修改对应的字段进行开发 二.预期结果 提取重复的表 ...

  5. Android 项目 Android 学习手册(一)

    前言: 当每次查询android 知识的时候,内心是凌乱的,总觉得要是有一个工具多好, 尤其在手机端如何可以查询的话,会非常完美,能大大减少选择查询的时间, 之前见了很多java 学习手册,把一些重要 ...

  6. python 遍历, 获取目录下所有文件名和文件夹的方法-----os.walk(), os.listdir

    http://www.runoob.com/python/os-walk.html https://www.cnblogs.com/dreamer-fish/p/3820625.html 转载于:ht ...

  7. Jdbc批处理一点异同

    同样的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class TestBatch {   public stati ...

  8. CF思维联系– Codeforces-988C Equal Sums (哈希)

    ACM思维题训练集合 You are given k sequences of integers. The length of the i-th sequence equals to ni. You ...

  9. Integer和int及String的总结

    秉承着总结发表是最好的记忆,我把之前遇到的问题在这里总结和大家分享一下,希望大家共同进步: 一.Integer和int首先说下自动拆装箱,基本数据类型转换为包装类型的过程叫装箱,反之则是拆箱,其中最特 ...

  10. Git 向远端仓库推文件

    第一次推送: 1.git init (创建本地仓库) 2. git remote add origin <远端仓库地址> (与远端仓库建立链接) 3.git checkout -b < ...