首页 > 其他分享 >RabbitMQ帮助类

RabbitMQ帮助类

时间:2022-11-29 08:56:02浏览次数:32  
标签:帮助 false string RabbitMQ readonly static private channel

一、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
        }

标签:帮助,false,string,RabbitMQ,readonly,static,private,channel
From: https://www.cnblogs.com/wml-it/p/16934380.html

相关文章

  • RabbitMQ基础
    1.初识MQ1.1.同步和异步通讯微服务间通讯有同步和异步两种方式:同步通讯:就像打电话,需要实时响应。异步通讯:就像发邮件,不需要马上回复。是你却不能跟多个人同时通话。......
  • Linux 安装RabbitMq
    1、下载Erlang和Rabbithttps://d28dx6y1hfq314.cloudfront.net/828/1039/el/8/package_files/2279064.rpm?t=1669605802_12ad3355a4114c22e9b151acfe4484891078938eh......
  • RabbitMQ 消息队列
    消息队列经过前面的学习,我们已经了解了我们之前的技术在分布式环境下的应用,接着我们来看最后一章的内容。那么,什么是消息队列呢?我们之前如果需要进行远程调用,那么一般......
  • python使用rabbitMQ
    python使用rabbitMQ 生产者消费者模型-------生产者-------------importpikaimportrandom#创建凭证credentials=pika.PlainCredentials('guest','guest')#新......
  • rabbitMQ--基本概念
    MQ的概念优点:1.应用解耦,提升系统容错性和可维护性 图中,假如不加入MQ,如果库存系统宕机,则会导致订单系统宕机,从而导致整个分布式宕机。 2.异步提速,提高系统的吞吐......
  • 《ASP.NET Core技术内幕与项目实战》精简集-DDD准备5.5:集成事件RabbitMQ
     本节内容,部分为补充内容,部分涉及到9.3.10-9.3.12(P335-342)。主要NuGet包:RabbitMQ.Client 微服务间,跨进程的事件发布和订阅,需要借助第三方服务器作为事件总线,目前常......
  • 微服务之RabbitMQ
    同步通讯和异步通讯  微服务基于Feign的调用就属于同步方式,存在一些问题        异步调用方案异步调用常见实现就是事件驱动模式优势一:服务解......
  • [Bug0061] RabbitMQ 报错 An unexpected connection driver error occured
    问题Java项目连接RabbitMQ报错Anunexpectedconnectiondrivererroroccured场景集成RabbitMQ的开源项目启动报错原因yml中端口配置错误,也是小白很容易犯错之一,配......
  • Windows操作系统下RabbitMQ下载安装
    前言Rabbitmq是一个消息队列Erlang可视化工具注意Rabbitmq和Erlang是有版本对应关系的查看地址:https://www.rabbitmq.com/which-erlang.html下文安装使用24.2.2+v3.......
  • Prism框架(一)——概述Prism框架的设计目的是用来帮助构建丰富、灵活、易维护的WPF和Si.
    SiPrism框架(一)——概述Prism框架的设计目的是用来帮助构建丰富、灵活、易维护的WPF和SiPrism框架(一)——概述Prism框架的设计目的是用来帮助构建丰富、灵活......