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
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。
![](https://images.cnblogs.com/cnblogs_com/wml-it/2196906/o_230823011905_wxgzhss.png)