96SEO 2026-02-19 17:00 0
证发送的消息可靠RabbitMQ的Confirm模式和2.Return模式

1、RabbitMQ消息Confirm模式保证从生产者到交换机的消息可靠1.1、Confirm模式简介1.2、具体代码实现1.2.1、application.yml
开启确认模式1.2.2、生产者方式1实现RabbitTemplate.ConfirmCallback生产者发送消息方式2直接写在生产者发送消息类实现RabbitTemplate.ConfirmCallback方式3匿名内部类写法方式4lambda表达式写法
1.2.3、RabbitConfig做交换机和队列的绑定1.2.4、pom.xml配置文件1.2.5、测试
2、RabbitMQ消息Return模式保证从交换机的到队列的消息可靠2.1、具体代码实现2.1.1、applicaton.yml2.1.2、pom.xml2.1.3、启动类2.1.4、业务层方式1实现RabbitTemplate.ReturnsCallback回调类MyReturnCallback配置类service业务层
方式2MessageService类实现RabbitTemplate.ReturnsCallback方式3匿名内部类实现RabbitTemplate.ReturnsCallback方式4lambda表达式实现RabbitTemplate.ReturnsCallback2.1.5、测试
1、RabbitMQ消息Confirm模式保证从生产者到交换机的消息可靠
消息的confirm确认机制是指生产者投递消息后到达了消息服务器Broker里面的exchange交换机exchange交换机会给生产者一个应答生产者接收到应答用来确定这条消息是否正常的发送到Broker的exchange中这也是消息可靠性投递的重要保障。
开启确认模式spring.rabbitmq.publisher-confirm-typecorrelated
写一个类实现RabbitTemplate.ConfirmCallback判断成功和失败的ack结果可以根据具体的结果如果ack为false对消息进行重新发送或记录日志等处理
rabbitTemplate.setConfirmCallback(messageConfirmCallBack);1.2.1、application.yml
#开启生产者的确认模式设置关联模式my:exchangeName:
方式1实现RabbitTemplate.ConfirmCallback
单独写一个类实现RabbitTemplate.ConfirmCallback
org.springframework.amqp.rabbit.connection.CorrelationData;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.stereotype.Component;Component
{if(ack){log.info(消息正确到达交换机);return;}//ack为false,消息没有到达交换机log.error(消息没有到达交换机原因是{},cause);}
com.power.config.MyConfirmCallback;
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.rabbit.connection.CorrelationData;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
confirmCallback;PostConstruct//构造方法后执行相当于初始化作用public
init(){rabbitTemplate.setConfirmCallback(confirmCallback);}Beanpublic
world.getBytes()).build();CorrelationData
CorrelationData();//关联数据correlationData.setId(order_123456);//发送订单信息rabbitTemplate.convertAndSend(exchange.confirm.01,info,message,correlationData);log.info(消息发送完毕发送时间是{},new
}方式2直接写在生产者发送消息类实现RabbitTemplate.ConfirmCallback
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.rabbit.connection.CorrelationData;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
rabbitTemplate;PostConstruct//构造方法后执行相当于初始化作用public
init(){rabbitTemplate.setConfirmCallback(this);}Beanpublic
world.getBytes()).build();CorrelationData
CorrelationData();//关联数据correlationData.setId(order_123456);//发送订单信息rabbitTemplate.convertAndSend(exchange.confirm.01,info,message,correlationData);log.info(消息发送完毕发送时间是{},new
{if(ack){log.info(消息正确到达交换机);return;}//ack为false,消息没有到达交换机log.error(消息没有到达交换机原因是{},cause);}
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.rabbit.connection.CorrelationData;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
rabbitTemplate;PostConstruct//构造方法后执行相当于初始化作用public
init(){rabbitTemplate.setConfirmCallback(new
RabbitTemplate.ConfirmCallback()
{if(ack){log.info(消息正确到达交换机);return;}//ack为false,消息没有到达交换机log.error(消息没有到达交换机原因是{},cause);}});}Beanpublic
world.getBytes()).build();CorrelationData
CorrelationData();//关联数据correlationData.setId(order_123456);//发送订单信息rabbitTemplate.convertAndSend(exchange.confirm.01,info,message,correlationData);log.info(消息发送完毕发送时间是{},new
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.rabbit.connection.CorrelationData;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
rabbitTemplate;PostConstruct//构造方法后执行相当于初始化作用public
init(){rabbitTemplate.setConfirmCallback(//lambda表达式写法(correlationData,
cause)-{log.info(关联id{},correlationData.getId());if(ack){log.info(消息正确到达交换机);return;}//ack为false,消息没有到达交换机log.error(消息没有到达交换机原因是{},cause);});}Beanpublic
world.getBytes()).build();CorrelationData
CorrelationData();//关联数据correlationData.setId(order_123456);//发送订单信息rabbitTemplate.convertAndSend(exchange.confirm.03,info,message,correlationData);log.info(消息发送完毕发送时间是{},new
Date());}}1.2.3、RabbitConfig做交换机和队列的绑定
org.springframework.amqp.core.*;
org.springframework.beans.factory.annotation.Value;
org.springframework.context.annotation.Bean;
org.springframework.context.annotation.Configuration;Configuration
{Value(${my.exchangeName})private
exchangeName;Value(${my.queueName})private
ExchangeBuilder.directExchange(exchangeName).build();}//创建队列Beanpublic
QueueBuilder.durable(queueName).build();}//交换机绑定队列Beanpublic
BindingBuilder.bind(queueName).to(exchangeName).with(info);}
xmlnshttp://maven.apache.org/POM/4.0.0
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.power/groupIdartifactIdrabbit_08_confirm01/artifactIdversion1.0-SNAPSHOT/versionnamerabbit_08_confirm01/namepropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.13/versionrelativePath//parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project1.2.5、测试
如果程序存在异常消息不会正常发送到交换机如果当交换机的名字不对时消息不会正常到底交换机的。
2、RabbitMQ消息Return模式保证从交换机的到队列的消息可靠
使用rabbitTemplate.setConfirmCallback设置回调函数当消息发送到exchange后回调confirm方法。
在方法中判断ack如果为true则发送成功如果为false则发送失败需要处理
spring.rabbitmq.publisher-returns:
true使用rabbitTemplate.setReturnCallback设置退回函数当消息从exchange路由到queue失败后则会将消息退回给producer并执行回调函数returnedMessage
xmlnshttp://maven.apache.org/POM/4.0.0
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.power/groupIdartifactIdrabbit_09_return01/artifactIdversion1.0-SNAPSHOT/versionnamerabbit_09_return01/namepropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.13/versionrelativePath//parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2.1.3、启动类
com.power.service.MessageService;
org.springframework.boot.ApplicationArguments;
org.springframework.boot.ApplicationRunner;
org.springframework.boot.SpringApplication;
org.springframework.boot.autoconfigure.SpringBootApplication;import
javax.annotation.Resource;SpringBootApplication
{SpringApplication.run(Application.class);}Overridepublic
方式1实现RabbitTemplate.ReturnsCallback
org.springframework.amqp.core.ReturnedMessage;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.stereotype.Component;/***
returnedMessage(ReturnedMessage
{log.error(消息从交换机没有正确的投递到队列原因是{},returnedMessage.getReplyText());}
org.springframework.amqp.core.*;
org.springframework.beans.factory.annotation.Value;
org.springframework.context.annotation.Bean;
org.springframework.context.annotation.Configuration;Configuration
{Value(${my.exchangeName})private
exchangeName;Value(${my.queueName})private
ExchangeBuilder.directExchange(exchangeName).build();}//创建队列Beanpublic
QueueBuilder.durable(queueName).build();}//交换机绑定队列Beanpublic
BindingBuilder.bind(queue).to(directExchange).with(info);}
com.power.config.MyReturnCallback;
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
myReturnCallback;PostConstructpublic
init(){rabbitTemplate.setReturnsCallback(myReturnCallback);//设置回调}Beanpublic
world.getBytes()).build();rabbitTemplate.convertAndSend(exchange.return.01,info111,message);log.info(消息发送完毕发送时间是{},new
}方式2MessageService类实现RabbitTemplate.ReturnsCallback
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.core.ReturnedMessage;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
rabbitTemplate;PostConstructpublic
init(){rabbitTemplate.setReturnsCallback(this);//设置回调}Beanpublic
world.getBytes()).build();rabbitTemplate.convertAndSend(exchange.return.01,info111,message);log.info(消息发送完毕发送时间是{},new
returnedMessage(ReturnedMessage
{log.error(消息从交换机没有正确的投递到队列原因是{},returnedMessage.getReplyText());}
}方式3匿名内部类实现RabbitTemplate.ReturnsCallback
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.core.ReturnedMessage;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
rabbitTemplate;PostConstructpublic
init(){rabbitTemplate.setReturnsCallback(new
RabbitTemplate.ReturnsCallback()
returnedMessage(ReturnedMessage
{log.error(消息从交换机没有正确的投递到队列原因是{},returned.getReplyText());}});}Beanpublic
world.getBytes()).build();rabbitTemplate.convertAndSend(exchange.return.01,info111,message);log.info(消息发送完毕发送时间是{},new
方式4lambda表达式实现RabbitTemplate.ReturnsCallback
org.springframework.amqp.core.Message;
org.springframework.amqp.core.MessageBuilder;
org.springframework.amqp.rabbit.core.RabbitTemplate;
org.springframework.context.annotation.Bean;
org.springframework.stereotype.Service;import
javax.annotation.PostConstruct;
rabbitTemplate;PostConstructpublic
init(){rabbitTemplate.setReturnsCallback(returned-
{log.error(消息从交换机没有正确的投递到队列原因是{},returned.getReplyText());});}Beanpublic
world.getBytes()).build();rabbitTemplate.convertAndSend(exchange.return.04,info111,message);log.info(消息发送完毕发送时间是{},new
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback