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

一、添加依赖

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

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

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

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

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

RabbitMQConfig
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
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
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
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

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 秒。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 是一个方便快捷的轻量级框架,提供了许多便利的功能和特性,其中就包括了 RabbitMQ整合功能。 RabbitMQ 是一个广泛使用的消息队列系统,具有高可靠性和可扩展性,能够在分布式系统中实现异步通信、解耦和任务调度等功能,因此对于分布式系统而言具备非常重要的价值。 在Spring Boot中,整合RabbitMQ 可以通过以下步骤完成: 1. 添加 RabbitMQ 的相关依赖:在 pom.xml 文件中添加 spring-boot-starter-amqp 依赖。 2. 配置 RabbitMQ 相关信息:通过 application.yml 或 application.properties 配置文件配置RabbitMQ 的基本信息,例如:连接地址、用户名、密码等。 3. 创建 RabbitMQ 模板:通过 RabbitTemplate 类提供的方法向 RabbitMQ 发送消息并接收响应信息。 4. 创建 Exchange 和 Queue:在 RabbitMQ 中创建 Exchange 和 Queue,Exchange 用于将消息路由到指定的 Queue 中。 5. 编写发送消息的代码:通过 RabbitTemplate 提供的方法send()发送消息,可以是简单字符串/对象/json等。 至此,我们已经实现了一个简单的 RabbitMQ 消息发送程序。但是在实际应用场景中,还需要具备更进一步完善的功能,例如:消息确认、消息持久化、消费者监听等功能,这些可以通过配置listener容器、消息确认机制以及使用DurableQueue,DurableExchange等参数来实现。 总之,RabbitMQ’s and Spring Boot的集成非常方便和快捷。通过简单的配置和实现,我们就可以使用 RabbitMQ 在分布式系统中实现异步通信、解决问题、任务调度等功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_37591637

请给我持续更新的动力~~

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

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

打赏作者

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

抵扣说明:

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

余额充值