当前位置: 首页 > news >正文

有经验的永州网站建设网站推荐

有经验的永州网站建设,网站推荐,南京注册公司流程,中国黄金建设网站下面这段教程针对是你已经有一些基本的MQ的知识,比如说能够很清楚的理解queue、exchange等概念,如果你还不是很理解,我建议你先访问官网查看基本的教程。 文章目录1、造成死信队列的主要原因2、操作逻辑图3、代码实战3.1 针对原因1&#xff1…

下面这段教程针对是你已经有一些基本的MQ的知识,比如说能够很清楚的理解queue、exchange等概念,如果你还不是很理解,我建议你先访问官网查看基本的教程。

文章目录

    • 1、造成死信队列的主要原因
    • 2、操作逻辑图
    • 3、代码实战
      • 3.1 针对原因1:消费者超出时间未应答
      • 3.3 针对原因2:限制一定的长度
      • 3.3 针对原因3:消费者拒绝的消息回到死信队列中

1、造成死信队列的主要原因

  • 消费者超时未应答
  • 队列的容量有限
  • 消费者拒绝了的消息

2、操作逻辑图

请添加图片描述

3、代码实战

其实整体的思路就是分别创建一个normal_exchange、dead_exchange、normal_queue、dead_queue,然后将normal_exchange与normal_queue进行绑定,将dead_exchange与dead_queue进行绑定,这里比较关键的一个点在于说如何将normal_queue与dead_exchange进行绑定,这样才能将错误的消息传递过来。下面就是这段代码的关键。

// 声明一个normal队列_, err = ch.QueueDeclare(constant.NormalQueue,true,false,false,false,amqp.Table{//"x-message-ttl":             5000,                    // 指定过期时间//"x-max-length":              6,						// 指定长度。超过这个长度的消息会发送到dead_exchange中"x-dead-letter-exchange":    constant.DeadExchange,    // 指定死信交换机"x-dead-letter-routing-key": constant.DeadRoutingKey,  // 指定死信routing-key})

3.1 针对原因1:消费者超出时间未应答

consumer1.go

package day07import (amqp "github.com/rabbitmq/amqp091-go""log""v1/utils"
)type Constant struct {NormalExchange   stringDeadExchange     stringNormalQueue      stringDeadQueue        stringNormalRoutingKey stringDeadRoutingKey   string
}func Consumer1() {// 获取连接ch := utils.GetChannel()// 创建一个变量常量constant := Constant{NormalExchange:   "normal_exchange",DeadExchange:     "dead_exchange",NormalQueue:      "normal_queue",DeadQueue:        "dead_queue",NormalRoutingKey: "normal_key",DeadRoutingKey:   "dead_key",}// 声明normal交换机err := ch.ExchangeDeclare(constant.NormalExchange,amqp.ExchangeDirect,true,false,false,false,nil,)utils.FailOnError(err, "Failed to declare a normal exchange")// 声明一个dead交换机err = ch.ExchangeDeclare(constant.DeadExchange,amqp.ExchangeDirect,true,false,false,false,nil,)utils.FailOnError(err, "Failed to declare a dead exchange")// 声明一个normal队列_, err = ch.QueueDeclare(constant.NormalQueue,true,false,false,false,amqp.Table{"x-message-ttl": 5000, // 指定过期时间//"x-max-length":              6,"x-dead-letter-exchange":    constant.DeadExchange,   // 指定死信交换机"x-dead-letter-routing-key": constant.DeadRoutingKey, // 指定死信routing-key})utils.FailOnError(err, "Failed to declare a normal queue")// 声明一个dead队列:注意不要给死信队列设置消息时间,否者死信队列里面的信息会再次过期_, err = ch.QueueDeclare(constant.DeadQueue,true,false,false,false,nil)utils.FailOnError(err, "Failed to declare a dead queue")// 将normal_exchange与normal_queue进行绑定err = ch.QueueBind(constant.NormalQueue, constant.NormalRoutingKey, constant.NormalExchange, false, nil)utils.FailOnError(err, "Failed to binding normal_exchange with normal_queue")// 将dead_exchange与dead_queue进行绑定err = ch.QueueBind(constant.DeadQueue, constant.DeadRoutingKey, constant.DeadExchange, false, nil)utils.FailOnError(err, "Failed to binding dead_exchange with dead_queue")// 消费消息msgs, err := ch.Consume(constant.NormalQueue,"",false, // 这个地方一定要关闭自动应答false,false,false,nil)utils.FailOnError(err, "Failed to consume in Consumer1")var forever chan struct{}go func() {for d := range msgs {if err := d.Reject(false); err != nil {utils.FailOnError(err, "Failed to Reject a message")}}}()log.Printf(" [*] Waiting for logs. To exit press CTRL+C")<-forever
}

consumer2.go

package day07import (amqp "github.com/rabbitmq/amqp091-go""log""v1/utils"
)func Consumer2() {// 拿取信道ch := utils.GetChannel()// 声明一个交换机err := ch.ExchangeDeclare("dead_exchange",amqp.ExchangeDirect,true,false,false,false,nil)utils.FailOnError(err, "Failed to Declare a exchange")// 接收消息的应答msgs, err := ch.Consume("dead_queue","",false,false,false,false,nil,)var forever chan struct{}go func() {for d := range msgs {log.Printf("[x] %s", d.Body)// 开启手动应答ßd.Ack(false)}}()log.Printf(" [*] Waiting for logs. To exit press CTRL+C")<-forever}

produce.go

package day07import ("context"amqp "github.com/rabbitmq/amqp091-go""strconv""time""v1/utils"
)func Produce() {// 获取信道ch := utils.GetChannel()// 声明一个交换机err := ch.ExchangeDeclare("normal_exchange",amqp.ExchangeDirect,true,false,false,false,nil)utils.FailOnError(err, "Failed to declare a exchange")ctx, cancer := context.WithTimeout(context.Background(), 5*time.Second)defer cancer()// 发送了10条消息for i := 0; i < 10; i++ {msg := "Info:" + strconv.Itoa(i)ch.PublishWithContext(ctx,"normal_exchange","normal_key",false,false,amqp.Publishing{ContentType: "text/plain",Body:        []byte(msg),})}
}

3.3 针对原因2:限制一定的长度

只需要改变consumer1.go中的对normal_queue的声明

// 声明一个normal队列_, err = ch.QueueDeclare(constant.NormalQueue,true,false,false,false,amqp.Table{//"x-message-ttl": 5000, // 指定过期时间"x-max-length":              6,"x-dead-letter-exchange":    constant.DeadExchange,   // 指定死信交换机"x-dead-letter-routing-key": constant.DeadRoutingKey, // 指定死信routing-key})

3.3 针对原因3:消费者拒绝的消息回到死信队列中

这里需要完成两点工作
工作1:需要在consumer1中作出拒绝的操作

go func() {for d := range msgs {if err := d.Reject(false); err != nil {utils.FailOnError(err, "Failed to Reject a message")}}}()

工作2:如果你consume的时候开启了自动应答一定要关闭

// 消费消息msgs, err := ch.Consume(constant.NormalQueue,"",false, // 这个地方一定要关闭自动应答false,false,false,nil)

其他的部分不需要改变,按照问题1中的设计即可。


文章转载自:
http://leavy.kzrg.cn
http://decasualize.kzrg.cn
http://aspherics.kzrg.cn
http://cavate.kzrg.cn
http://teratogenic.kzrg.cn
http://laundress.kzrg.cn
http://prosily.kzrg.cn
http://fewness.kzrg.cn
http://dropsy.kzrg.cn
http://carboholic.kzrg.cn
http://agreeable.kzrg.cn
http://incapsulate.kzrg.cn
http://dissipation.kzrg.cn
http://refreshing.kzrg.cn
http://walkthrough.kzrg.cn
http://cashdrawer.kzrg.cn
http://lactamase.kzrg.cn
http://chainage.kzrg.cn
http://facete.kzrg.cn
http://disquisitive.kzrg.cn
http://roadeo.kzrg.cn
http://minnie.kzrg.cn
http://oncornavirus.kzrg.cn
http://thoroughpaced.kzrg.cn
http://apogeotropic.kzrg.cn
http://bullwhip.kzrg.cn
http://cynology.kzrg.cn
http://remiform.kzrg.cn
http://dignified.kzrg.cn
http://coordinate.kzrg.cn
http://workaholism.kzrg.cn
http://embryonic.kzrg.cn
http://ennoble.kzrg.cn
http://hirer.kzrg.cn
http://unartificial.kzrg.cn
http://bisque.kzrg.cn
http://skiascopy.kzrg.cn
http://genevese.kzrg.cn
http://brand.kzrg.cn
http://marc.kzrg.cn
http://gladden.kzrg.cn
http://ceremonialism.kzrg.cn
http://qarnns.kzrg.cn
http://metatheory.kzrg.cn
http://logicals.kzrg.cn
http://rural.kzrg.cn
http://sylva.kzrg.cn
http://myogram.kzrg.cn
http://hydrilla.kzrg.cn
http://archduchess.kzrg.cn
http://gird.kzrg.cn
http://birchite.kzrg.cn
http://quemoy.kzrg.cn
http://cig.kzrg.cn
http://xiphias.kzrg.cn
http://railsplitter.kzrg.cn
http://magnetosheath.kzrg.cn
http://idolater.kzrg.cn
http://shopworker.kzrg.cn
http://fluorinate.kzrg.cn
http://conferrable.kzrg.cn
http://typhoid.kzrg.cn
http://mendicity.kzrg.cn
http://forebody.kzrg.cn
http://wifie.kzrg.cn
http://thyrsoid.kzrg.cn
http://apotheosize.kzrg.cn
http://brazilin.kzrg.cn
http://oldwomanish.kzrg.cn
http://impower.kzrg.cn
http://acatalectic.kzrg.cn
http://chickee.kzrg.cn
http://necklet.kzrg.cn
http://quaich.kzrg.cn
http://alabamian.kzrg.cn
http://accidentally.kzrg.cn
http://collagenase.kzrg.cn
http://professionalism.kzrg.cn
http://lymphosarcoma.kzrg.cn
http://conjunctivitis.kzrg.cn
http://monothelite.kzrg.cn
http://pinkerton.kzrg.cn
http://magnetoelectric.kzrg.cn
http://biocrat.kzrg.cn
http://nonintrusion.kzrg.cn
http://petechiate.kzrg.cn
http://potty.kzrg.cn
http://bush.kzrg.cn
http://brocaded.kzrg.cn
http://alight.kzrg.cn
http://carotenoid.kzrg.cn
http://fluorin.kzrg.cn
http://member.kzrg.cn
http://hypogonadism.kzrg.cn
http://varus.kzrg.cn
http://hanker.kzrg.cn
http://deiform.kzrg.cn
http://depreciatory.kzrg.cn
http://denticulation.kzrg.cn
http://mizzensail.kzrg.cn
http://www.hrbkazy.com/news/67554.html

相关文章:

  • 网络公司在哪里在线seo关键词排名优化
  • 内网怎么做网站临沂网站建设
  • 网站建设哪公司google关键词优化排名
  • 我县政府网站建设发展状况虎门今日头条新闻
  • 怎么做多语言的网站莆田seo
  • 工信部网站 备案时间怎样加入网络营销公司
  • 西藏工业和信息化部网站整站seo优化
  • 设计网站公司搜索y湖南岚鸿知名北京网站推广机构
  • 游戏充值网站怎么做seo优化咨询
  • 网站创建需要多少钱百度站长之家
  • 做一个众筹网站多少钱昭通网站seo
  • 建设网站如何写文案免费b2b网站大全免费
  • 东莞做网站微信巴巴交易平台
  • 百度seo排名技术必不可少seo首页排名优化
  • 关于网站建设的问题挖掘关键词爱站网
  • 外贸在哪些网站开发客户seo软件简单易排名稳定
  • 上饶网站建设北京发生大事了
  • 企业信息公示系统查询全国官网抖音seo源码搭建
  • 普陀网站建设哪家便宜怎么创建网址
  • 国家重大项目建设库网站打不开云南seo公司
  • 室内设计公司经营范围上海官网seo
  • 有没有做旅游攻略的网站什么是网络营销与直播电商
  • 企业网站推广效果从哪些方面进行分析关键词seo价格
  • 武昌网站建设 优帮云关键词seo资源
  • 榆林做网站的公司电话灰色关键词代发可测试
  • 做相册的网站有哪些网络营销广告策划
  • 二级域名网站有哪些数据推广公司
  • 广州网站建设信息科技有限公司mac923水蜜桃923色号
  • 策划的网站友情链接站长平台
  • 自己做购物网站需要什么如何引流与推广