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

网站建设订单模板鄂州网吧暂停营业

网站建设订单模板,鄂州网吧暂停营业,在网上做设计赚钱的网站,哪个网站用织梦做的MQ的一些常见问题 后面内容基于springboot 2.3.9.RELEASE 消息可靠性 生产者确认机制 在publisher微服务中application.yml中添加 spring:rabbitmq:publisher-confirm-type: correlatedpublisher-returns: truetemplate:mandatory: true每个RabbitTemplate只能配置一个Return…

MQ的一些常见问题


在这里插入图片描述
后面内容基于springboot 2.3.9.RELEASE

消息可靠性


在这里插入图片描述

生产者确认机制

在这里插入图片描述

  • 在publisher微服务中application.yml中添加
spring:rabbitmq:publisher-confirm-type: correlatedpublisher-returns: truetemplate:mandatory: true

在这里插入图片描述

  • 每个RabbitTemplate只能配置一个ReturnCallback, 因此需要在项目启动过程中配置
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;@Slf4j
@Configuration
public class CommonConfig implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {log.error("消息发送到队列失败, 响应码:{}, 失败原因: {}, 交换机: {}, 路由key: {}, 消息: {}",replyCode, replyText, exchange, routingKey, message);});}
}
  • 发送消息, 指定消息ID,消息ConfirmCallBack
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.UUID;@Slf4j
@SpringBootTest
public class PublishTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testvoid name() throws InterruptedException {CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());correlationData.getFuture().addCallback(result -> {if(result.isAck()){// ACKlog.debug("消息成功投递到交换机! 消息ID: {}", correlationData.getId());}else {// NACKlog.error("消息投递到交换机失败! 消息ID: {}", correlationData.getId());}}, ex -> {log.error("消息发送失败!", ex);});rabbitTemplate.convertAndSend("high.topic", "high.#", "hello amqp", correlationData);}
}

在这里插入图片描述

消息持久化

声明队列和交换机时指定durabletrue,为持久化

spring amqp中交换机、队列、消息默认都是持久的

消费者消息确认

在这里插入图片描述
消费者业务添加配置

spring:rabbitmq:listener:simple:acknowledge-mode: auto 

失败重试机制

在这里插入图片描述

spring:rabbitmq:listener:simple:acknowledge-mode: autoprefetch: 1retry:enabled: true     # 开启消费者失败重试initial-interval: 1000  # 初始的失败等待时长1秒multiplier: 1  # 下次失败的等待时长倍数max-attempts: 3   # 最大重试次数stateless: true  # true无状态, false有状态, 如果业务中包含事务, 这里改为false

配置说明:

初始等待时长1秒,倍数为2, 则等待时长为 1秒 2秒 4秒 8秒 …

消费者失败消息处理策略

在这里插入图片描述

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ErrorMessageConfig {@Beanpublic DirectExchange errorMessageExchange(){return new DirectExchange("error.direct");}@Beanpublic Queue errorQueue(){return new Queue("error.queue");}@Beanpublic Binding errorMessageBinding(){return BindingBuilder.bind(errorQueue()).to(errorMessageExchange()).with("error");}@Beanpublic MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate){return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");}
}

死信交换机


在这里插入图片描述
这个是由队列投递

TTL

在这里插入图片描述

  • 声明死信交换机
	@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "dl.queue", durable = "true"),exchange = @Exchange(name = "dl.direct"),key = "dl"))public void listenDlQueue(String msg){log.info("消费者接收到了dl.queue的延迟消息: {}", msg);}
  • 声明TTL交换机和队列
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class TTLMessageConfig {@Beanpublic DirectExchange ttlDirectExchange(){return new DirectExchange("ttl.direct");}@Beanpublic Queue ttlQueue(){return QueueBuilder.durable("ttl.queue").ttl(10000)   // 指定时间10秒.deadLetterExchange("dl.direct").deadLetterRoutingKey("dl").build();}@Beanpublic Binding ttlBinding(){return BindingBuilder.bind(ttlQueue()).to(ttlDirectExchange()).with("ttl");}
}
  • 发送消息
	@Testvoid name() {MessageBuilder.withBody("hello ttl".getBytes(StandardCharsets.UTF_8)).setDeliveryMode(MessageDeliveryMode.PERSISTENT).setExpiration("5000").build();rabbitTemplate.convertAndSend("ttl.direct", "ttl", "ttl message");}

也可以指定消息的过期时间, 两者都指定时, 以短的为准

在这里插入图片描述

http://www.yayakq.cn/news/200323/

相关文章:

  • 网站建设必须要虚拟主机吗哪个公司做网站最好深圳
  • 网站做301跳转的方法做网站的公司应该税率多少
  • 做网站外包公司名称大全做企业网站大概多少钱
  • html课程教学网站模板wordpress让投稿
  • 网站建设报价单表格模板私人订制管理中心
  • 大型的网站开发畅销的网站建设
  • 做网站送400电话网站推广的基本方法是什么
  • 和政网站建设陕西省住房和城乡建设部网站
  • 为什么要建立企业网站广州网站建设 易点
  • 做石材一般用哪些网站推销wordpress tdk
  • 东莞企业网站建设做外汇看什么网站
  • 北京 好的网站制作app下载量推广
  • 广东网站建设seo优化河北邯郸做wap网站
  • 做网站用asp div代码公司的网站建设做什么费用
  • 北京人力资源网站精益生产管理咨询公司
  • 用vs做的网站怎么打开企业解决方案展示平台
  • 免费网站制作软件平台wordpress 做商城
  • 哪个网站做衣服的wordpress英文怎么读
  • 华为云建设网站需要域名吗好看的免费网站模板下载
  • 免费域名注册和免费建站html网站开发实例视频
  • 哈尔滨住房和城乡建设局网站首页网站无备案无法登入
  • 如何做流量充值网站百度收录了我新网站的2篇文章了
  • 广州顺德网站设计全椒有做网站的吗
  • 学做网站学什么语言合肥建设集团招聘信息网站
  • 深圳罗湖互联网公司百度seo入驻
  • 少女免费观看片tv做网站seo赚钱吗
  • 网站美工外包公司成都哪里做网站便宜
  • 网站建设什么好听的建筑公司名字大全
  • 东莞企业建站申请多少钱湖北省城建设计院网站
  • 中卫网站建设多少钱wordpress非插件使用七牛云存储