SpringBoot2x整合RabbitMQ

        SpringBoot2x整合RabbitMQ,消息队列有很多优秀的中间件,但是我比较喜欢而且项目中实际用到过并且比较好用的就是RabbitMQ,下面是整合过程。

        1、xml

  # RabbitMQ 配置
  rabbitmq:
    host: xxx.xxx.xxx.xxx
    port: 5672
    username: xxxx
    password: xxxx
    virtual-host: /
    listener:
      simple:
        prefetch: 1

        2、pom

        <!-- rabbitmq 配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        3、config(配置成功后启动程序,自动创建交换机和队列)

@Configuration
public class RabbitMqConfig {

    @Bean
    DirectExchange exchange() {
        // EXCHANGE 交换机,自己配置
        return new DirectExchange(CommonConstants.EXCHANGE);
    }

    @Bean
    Queue sendMsgQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-max-priority", 40);
        // UNTREATED_MSG_QUEUE 消息队列名称
        return new Queue(CommonConstants.UNTREATED_MSG_QUEUE, true, false, false, map);
    }

    @Bean
    public Binding bindingSendMsg() {
        return BindingBuilder.bind(sendMsgQueue()).to(exchange()).with(CommonConstants.UNTREATED_MSG_QUEUE);
    }


}

        4、发送消息

@Component
@RequiredArgsConstructor
public class SendMsgHandle {

    private static Logger logger = LoggerFactory.getLogger(SendMsgHandle.class);

    private final AmqpTemplate amqpTemplate;

    public void send(String queue,String msg) {
        logger.info("向队列:{},发送消息:{}",queue,msg);
        try {
            amqpTemplate.convertAndSend(queue, msg);
        } catch (AmqpException e) {
            logger.error("向队列:{},发送消息:{},出现异常",queue,msg,e.getMessage(),e);
        }
    }

}

        5、接收消息

@Component
public class ReceiveMsgHandle {

    private static Logger logger = LoggerFactory.getLogger(ReceiveMsgHandle.class);

    // PROCESSED_MSG_QUEUE 监听的消息队列
    @RabbitListener(queues = {CommonConstants.PROCESSED_MSG_QUEUE}, concurrency = "1")
    public void processedQueue(String content, Message message, Channel channel) {
        try {
            // 接收到的消息可以转化成对象之后进行处理
            /*SendMsgVO msg = JSON.parseObject(content, SendMsgVO.class);*/
            logger.info("接收到消息==> content:{},message :{},channel:{}",content, message, channel);
        }catch (Exception e){
            logger.info(e.getMessage());
        }finally {
            return;
        }
    }

}

        6、集成测试

    @ApiOperation(value = "RabbitMQ集成测试" , notes = "RabbitMQ集成测试")
    @GetMapping("/rabbitMqTest")
    public Result rabbitMqTest(){
        sendMsgHandle.send(CommonConstants.PROCESSED_MSG_QUEUE,"RabbitMQ集成测试");
        return Result.ok();
    }

        7、综上,集成完毕,可以使用MQ了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值