SpringBoot整合RabbitMQ-V1.0.0完整版本源码

Douglas-RabbitMQ+SpringBoot-V1.0.0版本

1.生产者整合

1.1创建生产者SpringBoot工程

在这里插入图片描述
图解:以上创建的是一个大的单纯的springboot_rabbitmq项目,在其内将创建两个服务模块(生产者服务/消费者服务)


1.2引入依赖坐标

注意:给mq_producer生产者服务pom引入springboot整合rabbitmq的坐标

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

在这里插入图片描述


1.3编写启动类

在这里插入图片描述


1.4编写yml配置

server:
  port: 8081 #当前服务的端口
spring:
  rabbitmq:                #消息中间件
    virtual-host: /douglas   #mq的虚拟主机
    host: 127.0.0.1        #mq的ip地址
    username: douglas
    password: douglas
    template:             #生产者发送消息后等待10s未收到回复,确认后重新发送消息,
      retry:
        enabled: true     #开启ask消息回复监听
        initial-interval: 10000ms   # 10s未收到回复
        max-interval: 80000ms       # 最多80s秒未收到回复执行
        multiplier: 2               # 最多发送两次
    publisher-confirms: true        # 开启生产者确认

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GILAqfy0-1649693438265)(C:\Users\Dougl\AppData\Roaming\Typora\typora-user-images\1649577560750.png)]


1.5创建MQ的配置类

package com.douglas.mqproducer.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

/**
 * @Description
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2022-04-10 16:00
 **/
public class RabbitMqConfig {

    /**
     * 定义交换机名称
     */
    public static final String EXCHANGE_NAME = "boot_topic_exchange";

    /**
     * 定义队列名称
     */
    public static final String QUEUE_NAME = "boot_queue";

    /**
     * 定义路由
     */
    public static final String ROUTING_KEY = "boot.#";
    
    /** 
    * 创建一个主题模式的交换机
    * 将交换机Bean放入spring容器
    * topicExchange : 交换机模式 为topic主题模式,并且设置交换机名称
    * durable : 是否持久化 选择是
    * @author douglas
    * @date 2022/4/10 16:08 
    * @param 
    * @return com.rabbitmq.client.AMQP.Exchange
    **/
    @Bean("bootExchange")
    public Exchange bootExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
    
    /** 
    * 创建一个队列,并将其放入spring容器
    * durable : 选择持久化队列,设置队列名称
    * @author douglas
    * @date 2022/4/10 16:14 
    * @param 
    * @return org.springframework.amqp.core.Queue
    **/
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
    
    /** 
    * 绑定队列与交换机
    * @author douglas
    * @date 2022/4/10 16:22 
    * @param 
    * @return org.springframework.amqp.core.Binding
    **/
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue")Queue queue,
                                     @Qualifier("bootExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
    }   
}

在这里插入图片描述


1.6编写消息发送代码

1.6.1启动服务前需要检查
1.6.1.1rabbitmq服务账号密码

在这里插入图片描述

1.6.1.2检查rabbitmq的虚拟主机

1.创建虚拟主机
在这里插入图片描述

2.绑定虚拟主机

在这里插入图片描述

1.6.1.3检查交换机

1.创建douglas虚拟主机的交换机

在这里插入图片描述

1.6.1.4检查队列

1.创建douglas虚拟主机的队列
在这里插入图片描述

注意:至此rabbitmq服务的配置检查完毕!

1.6.2启动服务发送消息
1.6.2.1发送端接口源码
package com.douglas.mqproducer.controller;

import com.douglas.mqproducer.config.RabbitMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2022-04-10 16:28
 **/
@RestController
@RequestMapping("/mq")
public class MqController {

    /**
     * 注入rabbitmq
     */
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
    * 消息发送入口,接口测试
    * @author douglas
    * @date 2022/4/10 16:32
    * @param
    * @return java.lang.String
    **/
    @PostMapping("/sendMqMessage")
    @ResponseBody
    public String sendMqMessage(String message){
        if(StringUtils.isEmpty(message)){
            return "消息内容不能为空!!!";
        }
        try{
            //交换机名称
            //需要发送到的队列 路由
            //消息内容
            rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,
                                          "boot.douglas",
                                        message);
        }catch (Exception e){
            e.printStackTrace();
            return "消息发送失败";
        }
        return "消息发送成功,内容为:"+message;
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KHhnI70f-1649693438268)(C:\Users\Dougl\AppData\Roaming\Typora\typora-user-images\1649582473274.png)]

1.6.2.2启动服务

1.启动服务,使用postman进行消息发送接口测试,验证成功。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XJ4UnTVT-1649693438268)(C:\Users\Dougl\AppData\Roaming\Typora\typora-user-images\1649582953469.png)]
注意:至此springboot整合rabbitmq消息生产端完成消息发送!!!


2.消费者整合

2.1创建消费者SpringBoot工程

2.2引入依赖坐标

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9SjxifRr-1649693438268)(C:\Users\Dougl\AppData\Roaming\Typora\typora-user-images\1649690562701.png)]


2.3编写yml配置

2.4定义监听类

package com.douglas.mqconsumer.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

import static org.springframework.amqp.core.ExchangeTypes.TOPIC;

/**
 * @Description
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2022-04-11 23:25
 **/
@Component
public class RabbitMqListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "boot_queue",durable = "true"), //配置队列
            exchange = @Exchange(value = "boot_topic_exchange",type = TOPIC), //配置交换机,交换机类型
            key = {"boot.#"} //配置路由
    ))
    public void ListenerQueue(Message message){
        String body = new String(message.getBody());
        System.out.println("收到消息:"+body);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yvWcVJJs-1649693438269)(C:\Users\Dougl\AppData\Roaming\Typora\typora-user-images\1649692366384.png)]

2.5启动消费者测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c34qDYnR-1649693438269)(C:\Users\Dougl\AppData\Roaming\Typora\typora-user-images\1649692771975.png)]
注意至此springboot整合rabbitmq服务搭建完成。


免费源码:源码地址

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

以梦为馬Douglas

您的鼓励是对我最大的支持

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

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

打赏作者

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

抵扣说明:

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

余额充值