首页 > 其他分享 >RabbitMQ

RabbitMQ

时间:2022-10-31 11:57:09浏览次数:31  
标签:string 队列 RabbitMQ 死信 exchangeName props model

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

(具体一点:我这边的系统哦,数据库用的是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代码了~

 

标签:string,队列,RabbitMQ,死信,exchangeName,props,model
From: https://www.cnblogs.com/MuZiLily/p/16843798.html

相关文章

  • 某 .NET RabbitMQ SDK 有采集行为,你怎么看?
    一:背景1.讲故事前几天有位朋友在微信上找到我,说他的一个程序上了生产之后,被运维监控定位到这个程序会向一个网址为:http://m.365ey.net上不定期打数据,而且还是加密的格......
  • Spring Cloud Stream RabbitMQ消费消息失败
    丢弃消息这种场景生产环境应该不怎么用。入死信队列cloud:stream:bindings:hking:destination:hkingcontent-type:application/j......
  • .Net Core&RabbitMQ限制循环消费
    前言当消费者端接收消息处理业务时,如果出现异常或是拒收消息将消息又变更为等待投递再次推送给消费者,这样一来,则形成循环的条件。循环场景生产者发送100条消息到Rabbit......
  • RabbitMQ 进阶 -- 阿里云服务器部署RabbitMQ集群
    一、为什么要搭建RabbitMQ集群?未部署集群的缺点如果RabbitMQ集群只有一个broker节点,那么该节点的失效将导致整个服务临时性的不可用,并且可能会导致message的丢失(尤其是在非......
  • Python RabbitMQ pika的安装及direct路由模式的使用
    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件,RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有......
  • java-rabbitmq-官网实例02
    java-rabbitmq-官网实例02描述:  1.定义持久化队列,发送持久化消息,消息接受者需要手动应答,MQ才会删除队列中的消息 2.使用channel.basicQos......
  • java-rabbitmq-官网实例01
    java-rabbitmq-官网实例01描述:最简单实例,使用非持久化队列,生产者发布消息,MQ将消息推送给消费者消费,之后MQ在队列中删除该消息依次运行:D1_Send.......
  • java-rabbitmq-交换机介绍
    java-rabbitmq-交换机介绍RabbitMQ的四种交换机直连交换机:Directexchange扇出交换机:Fanoutexchange主题交换机:Topicexchange首部交换机:H......
  • RabbitMQ(1)
    1、初始MQ1.1微服务间通讯有同步和异步两种方式:同步通讯:就像打电话,需要实时响应,时效性强,可以立即得到结果,耦合度高,性能吞吐能力低,有级联失......
  • Python RabbitMQ pika的安装及fanout消息订阅模式的使用
    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件,RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有......