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);
    }
}
相关推荐
武昌库里写JAVA1 小时前
iOS runtime总结数据结构,消息传递、转发和应用场景
spring boot·spring·毕业设计·layui·课程设计
Allen Bright1 小时前
RabbitMQ中点对点(Point-to-Point)通讯方式的Java实现
java·rabbitmq
小马爱打代码2 小时前
SpringBoot + MyBatis 实现号段模式的分布式ID
spring boot·分布式·mybatis
Nijika...2 小时前
RabbitMQ 基本使用方法详解
java·后端·spring·rabbitmq
技术栈人员2 小时前
SpringBoot 整合 RabbitMQ 实现流量消峰
spring boot·rabbitmq·java-rabbitmq
喝醉酒的小白3 小时前
RabbitMQ 和 Kafka
分布式·kafka·rabbitmq
计算机毕设孵化场4 小时前
计算机毕设-基于springboot的宠物寄领养网站的设计与实现(附源码+lw+ppt+开题报告)
spring boot·课程设计·计算机毕设论文·计算机毕设ppt·计算机毕业设计如何选题·计算机毕业设计选题推荐·宠物寄领养网站
一只IT攻城狮4 小时前
Spring Boot集成Kafka:最佳实践与详细指南
java·spring boot·后端·中间件·kafka
大梦百万秋4 小时前
Spring Boot 实战:构建一个社交平台 API
java·spring boot·后端