05、SpringBoot整合RabbitMQ的实战开发问题-可靠生产的的问题?
1:全局写法 – 配置文件的方式
spring:
rabbitmq:
host: 120.77.34.190
port: 5672
username: admin
password: admin
virtual-host: /
publisher-returns: true
template:
mandatory: true
publisher-confirm-type: correlated
2:全局写法 —配置类的方式
package com.pugrabbitmq.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class RabbitTemplateConfiguration {
@Autowired
private CachingConnectionFactory connectionFactory;
@Bean
public RabbitTemplate rabbitTemplate() {
connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
connectionFactory.setPublisherReturns(true);
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true);
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if(!ack){
log.info("发消息出现了异常...{}",correlationData)
return;
}
log.info("消息发送成功!!!!,correlationData:{},ack:{},casuse:{}",correlationData,ack,cause);
});
rabbitTemplate.setReturnsCallback((resultMessage) -> {
log.info("消息确认了:,message:{},replycode:{},replytext:{},exchange:{},routingKey:{}",
resultMessage.getMessage().getBody(),resultMessage.getReplyCode(),resultMessage.getReplyText(),resultMessage.getExchange(),resultMessage.getRoutingKey());
});
return rabbitTemplate;
}
}
3:局部写法 - 发消息类的写法
package com.pugrabbitmq.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class RabbitTemplateConfiguration {
@Autowired
private CachingConnectionFactory connectionFactory;
@Bean
public RabbitTemplate rabbitTemplate() {
connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
connectionFactory.setPublisherReturns(true);
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
}
package com.pugrabbitmq.service.topic;
import com.pugrabbitmq.config.DirectOrderRabbitMQConfiguration;
import com.pugrabbitmq.config.TopicOrderRabbitMQConfiguration;
import com.pugrabbitmq.entity.PugMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ResponsitoryTopicMQService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void publishMongoDb(PugMessage pugMessage, String routeKey) {
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
log.info("消息发送成功!!!!,correlationData:{},ack:{},casuse:{}", correlationData, ack, cause);
});
rabbitTemplate.setReturnsCallback((resultMessage) -> {
log.info("消息确认了:,message:{},replycode:{},replytext:{},exchange:{},routingKey:{}",
resultMessage.getMessage().getBody(), resultMessage.getReplyCode(), resultMessage.getReplyText(), resultMessage.getExchange(), resultMessage.getRoutingKey());
});
rabbitTemplate.setExchange(TopicOrderRabbitMQConfiguration.TOPIC_ORDER_EXCHANGE);
rabbitTemplate.setRoutingKey(routeKey);
rabbitTemplate.convertAndSend(pugMessage, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
MessageProperties messageProperties = message.getMessageProperties();
messageProperties.setHeader("msgtype", "1");
return message;
}
});
}
public void publishESDb(PugMessage pugMessage, String routeKey) {
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if(!ack){
System.out.println("没有收到rabbitmq server 确认.....");
return;
}
log.info("1--------------------消息发送成功!!!!,correlationData:{},ack:{},casuse:{}", correlationData, ack, cause);
});
rabbitTemplate.setReturnsCallback((resultMessage) -> {
System.out.println("2=================returnCallback触发。消息路由到queue失败===========");
System.out.println("msg=" + new String(resultMessage.getMessage().getBody()));
System.out.println("replyCode=" + resultMessage.getReplyCode());
System.out.println("replyText=" + resultMessage.getReplyText());
System.out.println("exchange=" + resultMessage.getExchange());
});
rabbitTemplate.setExchange(TopicOrderRabbitMQConfiguration.TOPIC_ORDER_EXCHANGE);
rabbitTemplate.setRoutingKey(routeKey);
rabbitTemplate.convertAndSend(pugMessage, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
MessageProperties messageProperties = message.getMessageProperties();
messageProperties.setHeader("msgtype", "1");
return message;
}
});
}
}
整体可靠生成和可靠消费的完成示意图![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/af4c3e214f197d22bbc4a223828c69d1.png#pic_center)