springboot整合rabbitmq 实现数据的发送与接收

一、添加依赖

java 复制代码
<!--rabbitmq-需要的 AMQP 依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

二、配置队列、交换机以及路由

交换机--->路由--->队列

比如,你现在需要去北京,打开高德地图,导航,导航会提供很多路线,然后你选择其中一个路线,然后开车上路。

交换机就是北京,目的;路由就是很多路线;选择某条路,就是队列。

复制代码
RabbitMQConfig
java 复制代码
package com.juxin.gatewaysdk.controller.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //fanout广播
    private static final String QUEUE1 = "queue_fanout01";
    private static final String QUEUE2 = "queue_fanout02";
    private static final String EXCHANGE = "fanoutExchange";
    //direct路由
    private static final String QUEUE_DIRECT1 = "queue_direct01";
    private static final String QUEUE_DIRECT2 = "queue_direct02";
    private static final String EXCHANGE_DIRECT = "directExchange";
    //路由
    private static final String routing_key01 = "queue.red";
    private static final String routing_key02 = "queue.green";


    //topic
    private static final String QUEUE_TOPIC1 = "queue_topic01";
    private static final String QUEUE_TOPIC2 = "queue_topic02";
    private static final String EXCHANGE_TOPIC = "topicExchange";
    //路由
    private static final String routing_topic_key01 = "#.queue.#";
    private static final String routing_topic_key02 = "*.queue.#";
    //--------fanout广播模式---------

    /**
     * 1. 配置队列
     * 2. 队列名为 queue
     * 3. true 表示: 持久化 (不填,默认为true,默认持久化)
     * durable: 队列是否持久化。 队列默认是存放到内存中的,rabbitmq 重启则丢失,
     * 若想重启之后还存在则队列要持久化,
     * 保存到 Erlang 自带的 Mnesia 数据库中,当 rabbitmq 重启之后会读取该数据库
     *
     * @return
     */
    @Bean
    public Queue queue1() {
        return new Queue(QUEUE1);
    }


    @Bean
    public Queue queue2() {
        return new Queue(QUEUE2);
    }

    //创建交换机
    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(EXCHANGE);
    }

    //将队列和交换机进行绑定
    @Bean
    public Binding binding01() {
        //将队列queue1和交换机进行绑定
        return BindingBuilder.bind(queue1()).to(exchange());
    }

    @Bean
    public Binding binding02() {
        //将队列queue1和交换机进行绑定
        return BindingBuilder.bind(queue2()).to(exchange());
    }

    //--------direct路由模式---------
    @Bean
    public Queue queue_direct1() {
        return new Queue(QUEUE_DIRECT1);
    }

    @Bean
    public Queue queue_direct2() {
        return new Queue(QUEUE_DIRECT2);
    }

    @Bean
    public DirectExchange exchange_direct() {
        return new DirectExchange(EXCHANGE_DIRECT);
    }

    @Bean
    public Binding binding_direct1() {
        //将队列queue_direct1和交换机进行绑定,并给队列绑定路由
        return BindingBuilder.bind(queue_direct1()).to(exchange_direct()).with(routing_key01);
    }

    @Bean
    public Binding binding_direct2() {
        //将队列queue_direct2和交换机进行绑定,并给队列绑定路由
        return BindingBuilder.bind(queue_direct2()).to(exchange_direct()).with(routing_key02);
    }

    //----------------topic 模式------------------

    @Bean
    public Queue queue_topic1() {
        return new Queue(QUEUE_TOPIC1);
    }

    @Bean
    public Queue queue_topic2() {
        return new Queue(QUEUE_TOPIC2);
    }

    @Bean
    public TopicExchange exchange_topic() {
        return new TopicExchange(EXCHANGE_TOPIC);
    }

    @Bean
    public Binding binding_topic1() {
        return BindingBuilder.bind(queue_topic1()).to(exchange_topic()).with(routing_topic_key01);
    }

    @Bean
    public Binding binding_topic2() {
        return BindingBuilder.bind(queue_topic2()).to(exchange_topic()).with(routing_topic_key02);
    }
}
复制代码
/**
 * 消息接收者
 */
复制代码
MQReceiver
java 复制代码
package com.juxin.gatewaysdk.controller.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * 消息接收者
 */
@Service
@Slf4j
public class MQReceiver {

    //方法:接收消息
    @RabbitListener(queues = "queue")
    public void receive(Object msg) {
        log.info("接收到消息--" + msg);
    }

    //queues对应接收消息的队列
    @RabbitListener(queues = "queue_fanout01")
    public void receive1(Object msg) {
        log.info("从 queue_fanout01 接收消息-" + msg);
    }

    @RabbitListener(queues = "queue_fanout02")
    public void receive2(Object msg) {
        log.info("从 queue_fanout02 接收消息-" + msg);
    }

    @RabbitListener(queues = "queue_direct01")
    public void queue_direct1(Object msg) {
        log.info("从 queue_direct1 接收消息-" + msg);
    }

    @RabbitListener(queues = "queue_direct02")
    public void queue_direct2(Object msg) {
        log.info("从 queue_direct2 接收消息-" + msg);
    }

    @RabbitListener(queues = "queue_topic01")
    public void queue_topic1(Object msg) {
        log.info("从 queue_topic1 接收消息-" + msg);
    }

    @RabbitListener(queues = "queue_topic02")
    public void queue_topic2(Object msg) {
        log.info("从 queue_topic2 接收消息-" + msg);
    }
}
复制代码
/**
 * 消息发送者
 */
复制代码
MQSender
java 复制代码
package com.juxin.gatewaysdk.controller.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消息发送者
 */
@Slf4j
@Service
public class MQSender {

    @Resource
    private RabbitTemplate rabbitTemplate;

    //方法:发送消息
    public void send(Object msg) {
        log.info("发送消息-" + msg);
        //没有指定交换机会走默认的交换机,AMQP default
        //AMQP default是一个direct路由模式的交换机
        rabbitTemplate.convertAndSend("queue", msg);
    }

    //fanout广播模式发送消息
    public void sendFanout(Object msg) {
        log.info("发送消息-" + msg);
        //因为是fanout广播模式,不需要指定路由,这里路由赋空值处理
        rabbitTemplate.convertAndSend("fanoutExchange", "", msg);
    }

    public void sendDirect1(Object msg) {
        log.info("发送消息-" + msg);
        rabbitTemplate.convertAndSend("directExchange", "queue.red", msg);
    }

    public void sendDirect2(Object msg) {
        log.info("发送消息-" + msg);
        rabbitTemplate.convertAndSend("directExchange", "queue.green", msg);
    }

    //topic主题
    public void sendTopic1(Object msg) {
        log.info("发送消息-" + msg);
        rabbitTemplate.convertAndSend("topicExchange", "queue.red.message", msg);
    }

    public void sendTopic2(Object msg) {
        log.info("发送消息-" + msg);
        rabbitTemplate.convertAndSend("topicExchange", "green.queue.green.message", msg);
    }
}
复制代码
RabbitMQHandler
java 复制代码
package com.juxin.gatewaysdk.controller;

import com.juxin.gatewaysdk.controller.rabbitmq.MQSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class RabbitMQHandler {

    //装配MQSender
    @Resource
    private MQSender mqSender;

    //方法:调用消息生产者,发送消息
    @RequestMapping("/mq")
    @ResponseBody
    public void mq(){
        mqSender.send("hello llp");
    }

    //调用消息生产者,发送消息到交换机
    @RequestMapping("/mq/fanout")
    @ResponseBody
    public void fanout(){
        mqSender.sendFanout("hello fanout~");
    }

    //direct 模式
    @GetMapping("/mq/direct01")
    @ResponseBody
    public void direct01() {
        mqSender.sendDirect1("hello aimee");
    }

    //direct 模式
    @GetMapping("/mq/direct02")
    @ResponseBody
    public void direct02() {
        mqSender.sendDirect2("hello llp");
    }

    //topic模式
    @GetMapping("/mq/topic01")
    @ResponseBody
    public void topic01() {
        mqSender.sendTopic1("hello topic1");
    }

    //topic模式
    @GetMapping("/mq/topic02")
    @ResponseBody
    public void topic02() {
        mqSender.sendTopic2("hello topic2");
    }
}

这个不需要我们打开rabbitmq去创建队列,创建交换机的

配置rabbitmq的配置信息

application.yml

XML 复制代码
server:
  port: 8071
  #rabbitmq 配置
spring:
  rabbitmq:
    host: 192.168.123.29
    username: OPC_102
    password: Opc_2020
    #虚拟主机
    virtual-host: /
    #端口
    port: 5672
    listener:
      simple:
        #消费者最小数量
        concurrency: 10
        #消费者最大数量
        max-concurrency: 10
        #限制消费者,每次只能处理一条消息,处理完才能继续下一条消息
        prefetch: 1
        #启动时是否默认启动容器,默认为 true
        auto-startup: true
        #被拒绝时重新进入队列的
        default-requeue-rejected: true
    template:
      retry:
        #启用消息重试机制,默认为 false
        enabled: true
        #初始重试间隔时间
        initial-interval: 1000ms
        #重试最大次数,默认为 3 次
        max-attempts: 3
        #重试最大时间间隔,默认 10000ms
        max-interval: 10000ms
        #重试的间隔乘数,
        #配置 2 的话,第一次等 1s,第二次等 2s,第三次等 4s
        multiplier: 1

        #在 RabbitMQ 中,initial-interval 和 max-interval 是用于指定消息重试机制的两个参数,
        #它们的区别如下:
        #1. initial-interval(初始间隔时间):表示第一次重试的时间间隔,也就是在消息第一次处
        #理失败后,等待多长时间再尝试重新发送消息。这个参数的默认值是 1 秒。
        #2.max-interval(最大间隔时间):表示重试过程中的最大时间间隔,也就是每次重试时,
        #最长等待多长时间再尝试重新发送消息。这个参数的默认值是 10 秒。
相关推荐
lang201509287 小时前
Spring Boot缓存机制全解析
spring boot·后端·缓存
摇滚侠7 小时前
Spring Boot 3零基础教程,WEB 开发 默认页签图标 Favicon 笔记29
java·spring boot·笔记
lang201509287 小时前
Spring Boot SQL数据库全攻略
数据库·spring boot·sql
是梦终空9 小时前
计算机毕业设计241—基于Java+Springboot+vue的爱心公益服务系统(源代码+数据库+11000字文档)
java·spring boot·vue·毕业设计·课程设计·毕业论文·爱心公益系统
泉城老铁12 小时前
springboot 对接发送钉钉消息,消息内容带图片
前端·spring boot·后端
qq_124987075312 小时前
基于Spring Boot的高校实习实践管理系统(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
韩宁羽12 小时前
SpringBoot开发双11商品服务系统[完结19章]
spring boot
5pace14 小时前
【JavaWeb|第二篇】SpringBoot篇
java·spring boot·后端
纳就这样吧14 小时前
Spring Cloud中@EnableDiscoveryClient注解详解
spring boot·后端·spring cloud
李慕婉学姐14 小时前
【开题答辩过程】以《重庆市社区养老服务小程序设计与实现》为例,不会开题答辩的可以进来看看
java·spring boot