Spring Boot整合 RabbitMQ

文章目录

一. 引入依赖

创建spring项目

或者直接引入依赖

xml 复制代码
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>

二. 添加配置

yml 复制代码
#配置RabbitMQ的基本信息
spring:
 rabbitmq:
 host: 110.41.51.65
 port: 5672 #默认为5672
 username: study
 password: study
 virtual-host: bite #默认值为 /
 # 或者:
 #amqp://username:password@Ip:port/virtual-host
spring:
 rabbitmq:
 addresses: amqp://admin:admin@139.9.84.204:5673/good

三. Work Queue(工作队列模式)

声明队列

java 复制代码
import com.example.demo.constants.Constants;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //声明队列
    @Bean("workQueue")
    public Queue workQueue(){
        return QueueBuilder.durable(Constants.WORK_QUEUE).build();
    }
}

生产者

发送消息

java 复制代码
import com.example.demo.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/work")
    public String work(){
        for(int i = 0 ; i < 10; i++){
            //使用内置交换机发送消息
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello spring amqp: work....");
        }
        return "发送成功";
    }
}

消费者

java 复制代码
import com.example.demo.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WorkListener {
    @RabbitListener(queues = Constants.WORK_QUEUE)
    public void listenerQueue1(Message message){
        System.out.println("listener 1[" + Constants.WORK_QUEUE + "]收到消息: " + message);
    }

    @RabbitListener(queues = Constants.WORK_QUEUE)
    public void listenerQueue2(Message message){
        System.out.println("listener 2[" + Constants.WORK_QUEUE + "]收到消息: " + message);
    }
}


四. Publish/Subscribe(发布订阅模式)

声明队列和交换机

java 复制代码
//发布订阅模式
    //声明队列
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }
    //声明交换机
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange(){
        return ExchangeBuilder
                .fanoutExchange(Constants.FANOUT_EXCHANGE)
                .durable(true)
                .build();
    }
    //队列和交换机绑定
    @Bean
    public Binding fanoutBinding1(@Qualifier("fanoutExchange") FanoutExchange exchange, 
                                 @Qualifier("fanoutQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange exchange,
                                 @Qualifier("fanoutQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }

生产者

java 复制代码
 @RequestMapping("/fanout")
    public String fanoutProduct() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "hello spring boot: fanout....");
        return "发送成功";
    }

消费者

java 复制代码
@Component
public class FanoutListener {
    
    @RabbitListener(queues = Constants.FANOUT_QUEUE1)
    public void listenerQueue1(Message message) {
        System.out.println("listener 1[" + Constants.FANOUT_QUEUE1 + "]收到消息: " + message);
    }
    
    @RabbitListener(queues = Constants.FANOUT_QUEUE2)
    public void listenerQueue2(Message message) {
        System.out.println("listener 1[" + Constants.FANOUT_QUEUE2 + "]收到消息: " + message);
    }

}

五. Routing(路由模式)

声明队列和交换机

java 复制代码
//路由模式
    @Bean("directQueue1")
    public Queue directQueue1(){
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
    }
    @Bean("directQueue2")
    public Queue directQueue2(){
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
    }
    @Bean("directExchange")
    public DirectExchange directExchange(){
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE).durable(true).build();
    }
    @Bean
    public Binding directBinding1(@Qualifier("directExchange")DirectExchange exchange,
                                  @Qualifier("directQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("aaa");
    }
    @Bean
    public Binding directBinding2(@Qualifier("directExchange")DirectExchange exchange,
                                  @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("bbb");
    }
    @Bean
    public Binding directBinding3(@Qualifier("directExchange")DirectExchange exchange,
                                  @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("ccc");
    }

生产者

java 复制代码
@RequestMapping("/direct")
    public String directProduct(String routingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "hello spring boot: direct " + routingKey);
        return "发送成功";
    }

消费者

java 复制代码
@Component
public class DirectListener {
    @RabbitListener(queues = Constants.DIRECT_QUEUE1)
    public void listenerQueue1(Message message) {
        System.out.println("listener 1[" + Constants.DIRECT_QUEUE1 + "]收到消息: " + message);
    }

    @RabbitListener(queues = Constants.DIRECT_QUEUE2)
    public void listenerQueue2(Message message) {
        System.out.println("listener 2[" + Constants.DIRECT_QUEUE2 + "]收到消息: " + message);
    }
}

六. Topics(通配符模式)

声明队列和交换机

java 复制代码
    //通配符模式
    @Bean("topicQueue1")
    public Queue topicQueue1(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
    }
    @Bean("topicExchange")
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
    }
    @Bean
    public Binding topicBinding1(@Qualifier("topicExchange") TopicExchange exchange,
                                 @Qualifier("topicQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("*.aaa");
    }
    @Bean
    public Binding topicBinding2(@Qualifier("topicExchange") TopicExchange exchange,
                                 @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("bbb.*");
    }
    @Bean
    public Binding topicBinding3(@Qualifier("topicExchange") TopicExchange exchange,
                                 @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("bbb.#");
    }

生产者

java 复制代码
 @RequestMapping("/topic")
    public String topicProduct(String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring boot: topic " + routingKey);
        return "发送成功";
    }


消费者

java 复制代码
@Component
public class TopicListener {
    @RabbitListener(queues = Constants.TOPIC_QUEUE1)
    public void listenerQueue1(Message message) {
        System.out.println("listener 1[" + Constants.TOPIC_QUEUE1 + "]收到消息: " + message);
    }

    @RabbitListener(queues = Constants.TOPIC_QUEUE2)
    public void listenerQueue2(Message message) {
        System.out.println("listener 2[" + Constants.TOPIC_QUEUE2 + "]收到消息: " + message);
    }
}
相关推荐
一线大码5 分钟前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
weixin_4250230021 分钟前
Spring Boot 配置文件优先级详解
spring boot·后端·python
weixin_4250230022 分钟前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端
VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue校园社团管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
+VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vueOA工程项目管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
独自破碎E4 小时前
Spring Boot工程启动以后,怎么将数据库中已有的固定内容打入到Redis缓存中?
数据库·spring boot·缓存
为所欲为、Lynn5 小时前
用FastJson的Filter自动映射枚举
java·spring boot
JIngJaneIL6 小时前
基于java+ vue学生成绩管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
老华带你飞6 小时前
智能菜谱推荐|基于java + vue智能菜谱推荐系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
h7ml7 小时前
在Spring Boot中集成企业微信API的统一异常处理与日志追踪方案
spring boot·企业微信