SpringBoot 整合 RabbitMQ

简单整合

在SpringBoot中使用RabbitMQ主要包括以下步骤:

  1. 添加依赖
    在SpringBoot项目的pom.xml文件中添加以下依赖:
xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 配置RabbitMQ
    在application.properties或者application.yml配置文件中添加RabbitMQ的配置,如下:
properties 复制代码
spring.rabbitmq.host=本机地址
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
  1. 创建RabbitMQ相关的Java配置类
java 复制代码
@Configuration
public class RabbitConfig {

    @Bean
    public Queue Queue() {
        return new Queue("hello");
    }

}
  1. 生产者发送消息
java 复制代码
@Service
public class Sender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context);
    }

}
  1. 消费者接收消息
java 复制代码
@Component
@RabbitListener(queues = "hello")
public class Receiver {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver  : " + hello);
    }

}

注意:@RabbitListener注解定义了该类需要监听的队列名,@RabbitHandler注解用于标注用于消息处理的方法。

  1. 测试发送和接收消息
    我们可以在SpringBoot的启动类中注入Sender,并调用send方法发送消息,如下:
java 复制代码
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private Sender sender;

    @Override
    public void run(String... args) throws Exception {
        sender.send();
    }

}

运行SpringBoot应用,观察控制台打印的日志,我们可以看到发送的消息和接收的消息。

好的,以下是发布/订阅模式和路由模式的示例代码:

一、发布/订阅模式

  1. 配置类(定义交换器,队列,并将队列绑定到交换器)
java 复制代码
@Configuration
public class FanoutRabbitConfig {
    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }
}
  1. 发送消息
java 复制代码
public class FanoutSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hi, fanout msg ";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
    }
}
  1. 接收消息
java 复制代码
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
    @RabbitHandler
    public void process(String msg) {
        System.out.println("FanoutReceiverA  : " + msg);
    }
}
@Component
@RabbitListener(queues = "fanout.B")
public class FanoutReceiverB {
    @RabbitHandler
    public void process(String msg) {
        System.out.println("FanoutReceiverB  : " + msg);
    }
}
@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {
    @RabbitHandler
    public void process(String msg) {
        System.out.println("FanoutReceiverC  : " + msg);
    }
}

二、路由模式

  1. 配置类(定义交换器,队列,并将队列绑定到交换器)
java 复制代码
@Configuration
public class DirectRabbitConfig {
    @Bean
    public Queue directQueue1() {
        return new Queue("direct.queue1");
    }

    @Bean
    public Queue directQueue2() {
        return new Queue("direct.queue2");
    }

    @Bean
    DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    @Bean
    Binding bindingDirectExchange1(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("routingKey1");
    }

    @Bean
    Binding bindingDirectExchange2(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("routingKey2");
    }
}
  1. 发送消息
java 复制代码
public class DirectSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send1() {
        String context = "hi, direct msg ";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("directExchange", "routingKey1", context);
    }

    public void send2() {
        String context = "hi, direct msg ";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("directExchange", "routingKey2", context);
    }
}
  1. 接收消息
java 复制代码
@Component
@RabbitListener(queues = "direct.queue1")
public class DirectReceiver1 {
    @RabbitHandler
    public void process(String msg) {
        System.out.println("DirectReceiver1  : " + msg);
    }
}
@Component
@RabbitListener(queues = "direct.queue2")
public class DirectReceiver2 {
    @RabbitHandler
    public void process(String msg) {
        System.out.println("DirectReceiver2  : " + msg);
    }
}

以上代码示例中,"directExchange" 和 "routingKey1"、"routingKey2" 都是我随意定义的名称,你可以根据你的实际需求来定义。

相关推荐
不一样的信息安全1 小时前
Spring Boot框架下的上海特产销售商城网站开发之旅
网络·spring boot
lozhyf2 小时前
基于SpringBoot + Mybatis Plus + SaToken + Thymeleaf + Layui的后台管理系统
spring boot·layui·mybatis
乙卯年QAQ3 小时前
【Elasticsearch】Springboot编写Elasticsearch的RestAPI
spring boot·elasticsearch
生产队队长4 小时前
项目练习:若依后台管理系统-后端服务开发步骤(springboot单节点版本)
java·spring boot·后端
m0_748236834 小时前
【wiki知识库】08.添加用户登录功能--后端SpringBoot部分
java·spring boot·后端
小扳4 小时前
博客之星2024年度-技术总结:技术探险家小板的一年的征程
java·大数据·spring boot·elasticsearch·搜索引擎·spring cloud·微服务
m0_512744645 小时前
如何在idea中搭建SpringBoot项目
java·spring boot·intellij-idea
中國移动丶移不动6 小时前
分布式系统通信解决方案:Netty 与 Protobuf 高效应用
java·spring boot·后端
m0_748234087 小时前
Spring Boot 3.3.4 升级导致 Logback 之前回滚策略配置不兼容问题解决
java·spring boot·logback
橘子海全栈攻城狮7 小时前
【源码+文档+调试讲解】基于Spring Boot的协作会话平台
java·spring boot·后端·spring·微信小程序·小程序