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);
    }
}
相关推荐
PRINT!9 分钟前
个人财富全景管理系统 AssetMe【内容均为AI制作】
spring boot·信息可视化·ai编程
jay神1 小时前
基于SpringBoot的宠物生命周期信息管理系统
java·数据库·spring boot·后端·web开发·宠物·管理系统
万亿少女的梦1681 小时前
基于SpringBoot的在线考试管理系统设计与实现
java·spring boot·后端
下次再写2 小时前
【Redis实战】深入理解Redis缓存策略:从原理到Spring Boot实践
java·spring boot·redis·缓存穿透·缓存击穿·分布式缓存·缓存策略
小白君6532 小时前
互联网大厂Java面试:从Spring Boot到微服务的技术场景深度解析
spring boot·redis·微服务·消息队列·java面试·数据库优化
imuliuliang3 小时前
五大编程语言核心对比:特性与应用全解析
运维·spring boot·nginx
过期动态3 小时前
【RabbitMQ基础篇】RabbitMQ从入门到实战
java·jvm·数据库·分布式·spring·rabbitmq·intellij-idea
ahauedu4 小时前
流水账债务
spring boot
普修罗双战士5 小时前
专业Markdown转HTML工具类:修复优化与Spring Boot适配
windows·spring boot·html
阿维的博客日记5 小时前
传统 Spring XML 配置 vs Spring Boot Starter 对比文档
xml·spring boot·spring