feat(商城): 优化rabbitMQ配置

master
wayn 3 years ago
parent 4cbe2a6052
commit fa02ce3cab

@ -38,7 +38,7 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring-boot.version>2.5.8</spring-boot.version>
<mysql.connector.java.version>8.0.25</mysql.connector.java.version>
<lettuce.version>6.1.3.RELEASE</lettuce.version>
<lettuce.version>6.1.6.RELEASE</lettuce.version>
<elasticsearch.version>7.13.4</elasticsearch.version>
<mybatis-plus.version>3.5.0</mybatis-plus.version>
<druid.version>1.2.8</druid.version>
@ -50,7 +50,7 @@
<easy-captcha.version>1.6.2</easy-captcha.version>
<swagger.version>3.0.0</swagger.version>
<easypoi.version>4.4.0</easypoi.version>
<jwt.version>3.18.2</jwt.version>
<jwt.version>3.18.3</jwt.version>
<qiniu.version>7.9.1</qiniu.version>
<mail.version>1.4.7</mail.version>
</properties>

@ -27,4 +27,8 @@ public class WorkRabbitConfig {
return new OrderDirectReceiver(2);
}
@Bean
public OrderDirectReceiver orderWorkReceiver3() {
return new OrderDirectReceiver(3);
}
}

@ -1,5 +1,6 @@
package com.wayn.message.core.config;
import com.wayn.message.core.constant.SysConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
@ -22,7 +23,7 @@ public class DirectRabbitConfig {
*/
@Bean
public Queue EmailDirectQueue() {
return new Queue("EmailDirectQueue", true);
return new Queue(SysConstants.email_direct_queue, true);
}
/**
@ -32,7 +33,7 @@ public class DirectRabbitConfig {
*/
@Bean
DirectExchange EmailDirectExchange() {
return new DirectExchange("EmailDirectExchange");
return new DirectExchange(SysConstants.EMAIL_DIRECT_EXCHANGE);
}
/**
@ -48,12 +49,12 @@ public class DirectRabbitConfig {
/************************************ 订单队列、交换机 begin *******************************************/
@Bean
public Queue OrderDirectQueue() {
return new Queue("OrderDirectQueue", true);
return new Queue(SysConstants.ORDER_DIRECT_QUEUE, true);
}
@Bean
DirectExchange OrderDirectExchange() {
return new DirectExchange("OrderDirectExchange");
return new DirectExchange(SysConstants.ORDER_DIRECT_EXCHANGE);
}
@Bean

@ -1,6 +1,7 @@
package com.wayn.message.core.config;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@ -8,27 +9,28 @@ import org.springframework.stereotype.Component;
/**
* RabbitTemplateconfirm
*/
@Slf4j
@Component
public class RabbitTemplateConfig {
@Bean
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.setConnectionFactory(connectionFactory);
public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// 设置开启Mandatory才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
rabbitTemplate.setMandatory(true);
// 服务器收到消息确认回调
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
System.out.println("ConfirmCallback: " + "相关数据:" + correlationData);
System.out.println("ConfirmCallback: " + "确认情况:" + ack);
System.out.println("ConfirmCallback: " + "原因:" + cause);
log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause);
});
// 消息投递到队列失败回调处理
rabbitTemplate.setReturnsCallback(returned -> {
System.out.println("ReturnCallback: " + "消息:" + returned.getMessage());
System.out.println("ReturnCallback: " + "回应码:" + returned.getReplyCode());
System.out.println("ReturnCallback: " + "回应信息:" + returned.getReplyText());
System.out.println("ReturnCallback: " + "交换机:" + returned.getExchange());
System.out.println("ReturnCallback: " + "路由键:" + returned.getRoutingKey());
log.info("ReturnCallback: " + "消息:" + returned.getMessage());
log.info("ReturnCallback: " + "回应码:" + returned.getReplyCode());
log.info("ReturnCallback: " + "回应信息:" + returned.getReplyText());
log.info("ReturnCallback: " + "交换机:" + returned.getExchange());
log.info("ReturnCallback: " + "路由键:" + returned.getRoutingKey());
});
return rabbitTemplate;
}
}

@ -3,4 +3,12 @@ package com.wayn.message.core.constant;
public class SysConstants {
public static final int RESULT_SUCCESS_CODE = 200;
public static final String email_direct_queue = "EmailDirectQueue";
public static final String EMAIL_DIRECT_EXCHANGE = "EmailDirectExchange";
public static final String EMAIL_DIRECT_ROUTING = "EmailDirectRouting";
public static final String ORDER_DIRECT_QUEUE = "OrderDirectQueue";
public static final String ORDER_DIRECT_EXCHANGE = "OrderDirectExchange";
public static final String ORDER_DIRECT_ROUTING = "OrderDirectRouting";
}

@ -39,6 +39,7 @@ import com.wayn.common.util.R;
import com.wayn.common.util.bean.MyBeanUtil;
import com.wayn.common.util.ip.IpUtils;
import com.wayn.data.redis.manager.RedisCache;
import com.wayn.message.core.constant.SysConstants;
import com.wayn.message.core.messsage.OrderDTO;
import com.wayn.mobile.api.domain.Cart;
import com.wayn.mobile.api.mapper.OrderMapper;
@ -262,7 +263,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
.setDeliveryMode(MessageDeliveryMode.PERSISTENT)
.build();
rabbitTemplate.convertAndSend("OrderDirectExchange", "OrderDirectRouting", message, correlationData);
rabbitTemplate.convertAndSend(SysConstants.ORDER_DIRECT_EXCHANGE, SysConstants.ORDER_DIRECT_ROUTING, message, correlationData);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
}

Loading…
Cancel
Save