RabbitMQ

我这边的使用场景:多名用户进行同一操作。

(具体一点:我这边的系统哦,数据库用的是mysql,操作是导入excel,可以这么说百分之六十的页面都有批量导入,而且一个页面不止一个批量导入按钮,多的有六个批量导入按钮。可想而知,同时操作!恍恍惚惚o(╥﹏╥)o)

基础配置

        public ConnectionFactory rabbitmqFactory;
        private string hostName = Model.AppConfigurtaionServices.Configuration["AppSettings:RabHost"];//RabbitMq网址
        private string userName = Model.AppConfigurtaionServices.Configuration["AppSettings:RabUserName"];//RabbitMq用户名
        private string pwd = Model.AppConfigurtaionServices.Configuration["AppSettings:RabPwd"];//RabbitMq密码
        private int port = int.Parse(Model.AppConfigurtaionServices.Configuration["AppSettings:RabPort"]);//RabbitMq端口
        /// <summary>
        /// 构造函数
        /// </summary>
        public RabbitMqUtil()
        {
            rabbitmqFactory = new ConnectionFactory()
            {
                HostName = hostName,
                UserName = userName,
                Password = pwd,
                Port = port,
                AutomaticRecoveryEnabled = true,
            };
        }
        /// <summary>
        /// 创建工厂
        /// </summary>
        public RabbitMqUtil(string hostName, string userName, string pwd, int port)
        {
            rabbitmqFactory = new ConnectionFactory()
            {
                HostName = hostName,
                UserName = userName,
                Password = pwd,
                Port = port,
                RequestedHeartbeat = TimeSpan.FromSeconds(100),
                AutomaticRecoveryEnabled = true
                //ContinuationTimeout= TimeSpan.FromSeconds(3000)
            };
        }

生产端代码

        /// <summary>
        /// 生产端
        /// </summary>
        /// <param name="exchangeName">交换机名称</param>
        /// <param name="queueName">队列名称</param>
        /// <param name="routeKey">路由关键字</param>
        /// <param name="msg">消息内容</param>
        public void Producter(string exchangeName, string queueName, string routeKey, string msg)
        {
            IConnection conn = rabbitmqFactory.CreateConnection();
            IModel model = conn.CreateModel();
            //设置交换器的类型
            model.ExchangeDeclare(exchangeName, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
            //声明一个队列,设置队列是否持久化,排他性,与自动删除
            model.QueueDeclare(queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
            //绑定消息队列,交换器,routingkey
            model.QueueBind(queueName, exchangeName, routingKey: routeKey);
            model.ConfirmSelect();
            var props = model.CreateBasicProperties();
            props.ContentType = "text/plain";
            props.ContentEncoding = "UTF-8";
            props.DeliveryMode = 1;// 设置消息是否持久化,1: 非持久化 2:持久化
            var mes = Encoding.UTF8.GetBytes(msg);
            model.BasicPublish(exchangeName, routingKey: routeKey, basicProperties: props, body: mes);
        }
        /// <summary>
        /// 生产端(死信)
        /// </summary>
        /// <param name="exchangeName">交换机名称</param>
        /// <param name="queueName">队列名称</param>
        /// <param name="routeKey">路由关键字</param>
        /// <param name="deadLetterExchangeName">死信交换机名称</param>
        /// <param name="deadLetterQueueName">死信队列名称</param>
        /// <param name="deadLetterRouting">死信路由关键字</param>
        /// <param name="msg">消息内容</param>
        public void DeadProducter(string exchangeName, string queueName, string routeKey, string deadLetterExchangeName, string deadLetterQueueName, string deadLetterRouting, string msg)
        {
            IConnection conn = rabbitmqFactory.CreateConnection();
            IModel model = conn.CreateModel();
            //死信队列设置
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("x-dead-letter-exchange", deadLetterExchangeName);//死信交换器
            dic.Add("x-dead-letter-routing-key", deadLetterRouting);//列信路由
            dic.Add("x-message-ttl", 1000 * 60 * 2);//过期时间
            //设置交换器的类型
            model.ExchangeDeclare(exchangeName, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
            //声明一个队列,设置队列是否持久化,排他性,与自动删除
            model.QueueDeclare(queueName, durable: true, exclusive: false, autoDelete: false, arguments: dic);
            //绑定消息队列,交换器,routingkey
            model.QueueBind(queueName, exchangeName, routingKey: routeKey);
            model.ConfirmSelect();
            var props = model.CreateBasicProperties();
            props.ContentType = "text/plain";
            props.ContentEncoding = "UTF-8";
            props.DeliveryMode = 1;// 设置消息是否持久化,1: 非持久化 2:持久化
            var mes = Encoding.UTF8.GetBytes(msg);
            model.BasicPublish(exchangeName, routingKey: routeKey, basicProperties: props, body: mes);
        }

消费端是写成控制台程序单独执行,就不po代码了~

 

posted @ 2022-10-31 11:47  MuZiLily  阅读(28)  评论(0编辑  收藏  举报