RabbitMQ帮助类
一、RabbitMQHelper
/// <summary>
/// RabbitMQHelper 的摘要说明
/// </summary>
public class RabbitMQHelper
{
//主机
private readonly static string host = ConfigurationManager.AppSettings["MqHost"].ToString().Trim();
//端口
private readonly static string port = ConfigurationManager.AppSettings["MqPort"].ToString().Trim();
//用户
private readonly static string userName = ConfigurationManager.AppSettings["MqUser"].ToString().Trim();
//密码
private readonly static string passWord = ConfigurationManager.AppSettings["MqPwd"].ToString().Trim();
//交换机
private readonly static string exchangeName = ConfigurationManager.AppSettings["MqExchange"].ToString().Trim();
//队列
//private readonly static string queueName = ConfigurationManager.AppSettings["MqQueue"].ToString().Trim();
//队列
private readonly static string queueName1 = "Can_Gather";
private readonly static string queueName2 = "Can_Tire";
private readonly static string queueName3 = "Can_Fault";
private readonly static string queueName4 = "Can_Alarm";
private readonly static string queueName5 = "Can_Behavior";
private readonly static string queueName6 = "Can_Violate";
/// <summary>
/// 获取RabbitMQ连接
/// </summary>
/// <returns></returns>
public static IConnection GetConnection()
{
//实例化链接工厂
var factory = new ConnectionFactory
{
HostName = host, //ip
Port = 5672, // 端口
UserName = userName, // 账户
Password = passWord, // 密码
VirtualHost = "/" , // 虚拟主机
AutomaticRecoveryEnabled = true,//断开默认重连
TopologyRecoveryEnabled = true
};
return factory.CreateConnection();
}
/// <summary>
/// 建立链接
/// </summary>
/// <returns></returns>
public static IModel CreateConnection()
{
var connection = RabbitMQHelper.GetConnection();
var channel = connection.CreateModel();
// 声明Direct交换机
channel.ExchangeDeclare(exchangeName, "direct");
// 声明创建队列
channel.QueueDeclare(queueName1, false, false, false, null);
channel.QueueDeclare(queueName2, false, false, false, null);
channel.QueueDeclare(queueName3, false, false, false, null);
channel.QueueDeclare(queueName4, false, false, false, null);
channel.QueueDeclare(queueName5, false, false, false, null);
channel.QueueDeclare(queueName6, false, false, false, null);
// 绑定到交互机 指定routingKey
channel.QueueBind(queue: queueName1, exchange: exchangeName, routingKey: "gather");//完成
channel.QueueBind(queue: queueName2, exchange: exchangeName, routingKey: "tire");
channel.QueueBind(queue: queueName3, exchange: exchangeName, routingKey: "fault");//完成
channel.QueueBind(queue: queueName4, exchange: exchangeName, routingKey: "alarm");//完成
channel.QueueBind(queue: queueName5, exchange: exchangeName, routingKey: "behavior");//完成
channel.QueueBind(queue: queueName6, exchange: exchangeName, routingKey: "violate");
return channel;
}
}
二、MqModel
/// <summary>
/// MqModel 的摘要说明
/// </summary>
public class MqModel
{
/// <summary>
/// 消息类型,1:gather,2:behavior,3:alarm,4:fault,5:tire, 6:violate
/// </summary>
public int type { get; set; }
/// <summary>
/// 消息实体
/// </summary>
public object content { get; set; }
}
三、PublishMsg
/// <summary>
/// 根据routingKey发布消息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <param name="routingKey"></param>
/// <param name="et"></param>
private void PublishMsg<T>(int type,string routingKey, T et)
{
#region 发布MQ消息
//按照消费端格式定义
MqModel publishModel = new MqModel()
{
type = 1,
content = et
};
//转换
string jsonStr = JsonConvert.SerializeObject(publishModel);
var body = Encoding.UTF8.GetBytes(jsonStr);//消息以二进制形式传输
channel.BasicPublish(exchange: exchangeName, routingKey: routingKey, null, body);
#endregion
}
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/16934380.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2021-11-29 byte数组操作
2021-11-29 .ini文件处理帮助类
2021-11-29 记录log的方法
2021-11-29 Dictionary<string, string>拆分字符串