springcloud使用RabbitMQ消息队列

1.第一步首先在maven中引入RabbitMQ的依赖关系

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.第一步消息配置

/** 消息发送 1.短信 2.站内信 **/
public static final String QUEUE_MSG_SEND = "msg:send";
/** 短信发送队列 */
public static final String QUEUE_MSG_SMS_SEND = "msg:sms:send";

/**
 * 消息配置
 * @author swt
 */
@Configuration
public class RabbitConfig {

    /**
     * 短信发送队列
     * @return
     */
    @Bean
    public Queue smsQueue() {
        return new Queue(QUEUE_MSG_SMS_SEND, true);
    }

    /**
     * 消息发送队列(1.短信 2.站内信)
     * @return
     */
    @Bean
    public Queue msgQueue() {
        return new Queue(QUEUE_MSG_SEND, true);
    }
}

3.第三步配置服务端

/**
 * 消息管理(1.短信 2.站内信)
 * @auther ywl
 */
@Component
@RabbitListener(queues = QUEUE_MSG_SEND)
public class MsgReceiver {

    private Log log = LogFactory.getLog(MsgReceiver.class);

    @Resource
    private MsgService msgService;

    @Resource
    private MemberUserService memberUserService;

    private ObjectMapper mapper = new ObjectMapper();



    @RabbitHandler
    public void process(Msg msg) {
        try {
            log.info("-----消息发送开始------");
            log.info(mapper.writeValueAsString(msg));
            msgService.sendMsg(msg);
            log.info("-----消息发送结束------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

4.第四步配置消费端

@Resource
private AmqpTemplate amqpTemplate;
//添加资金账户支出变动消息队列
Msg msg = new Msg();
msg.setMemberId(trade.getMemberId());
msg.setType(cn.ug.msg.bean.status.CommonConstants.MsgType.ACCOUNT_PAY_CHANGE.getIndex());

Map<String, String> paramMap = new HashMap<>();
paramMap.put("date", UF.getFormatDate("MM月dd日", UF.getDateTime()));
paramMap.put("changeAmount", BigDecimalUtil.to2Point(trade.getAmount()).toString());
paramMap.put("amount", BigDecimalUtil.to2Point(fundAmount).toString());
msg.setParamMap(paramMap);
logger.info("-----消息-------"+paramMap.toString());
amqpTemplate.convertAndSend(QUEUE_MSG_SEND, msg);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云梦楼兰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值