spring-boot整合RabbitMQ(单生产以及多消费)

转载自:https://blog.csdn.net/zht741322694/article/details/79828088

spring-boot整合RabbitMQ(单生产以及多消费)

      单生产多消费相比于单生产单消费来说只是多了一个消费者而已,需要注意的是,这两个消费者都是监听一个消息队列并且默认情况下针对生产者发布的消息是进行分摊的,而不是同时拥有消费,

       因为rabbitmq默认的消费确认原则是生产者发布了一个消息,只要有一个消费者消费了,那么此消息会立即从队列中移除,不会管这个消息的消费时长以及是否成功。这种方式存在着一个问题就是,如果此消费者拿到此消息之后,处理超时异常了等,那么就造成了此消息没有被成功消费而丢失,想要解决这个问题可以关闭rabbitMQ的自动消息确认功能,从而在代码中进行手动确认。

      手动确认一定要确保消息处理后消息回执,否则将造成消息二次消费以及消息积压到队列中。举个栗子:默认情况下如果生产者发送了一条消息到消息队列中,两个消费者只能有一个消费者能够消费到此消息,下面直接上代码:

消费者1:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 
/**
 * 消费者监听消息队列
 */
@Component
@RabbitListener(queues = "hello-queue")
public class QueueListener {
    @RabbitHandler
    public void proces(String message){
        System.out.println("消费者:"+message);
    }
 
}

消费者2:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 
/**
 * 消费者监听消息队列
 */
@Component
@RabbitListener(queues = "hello-queue")
public class QueueListener2 {
    @RabbitHandler
    public void proces(String message){
        System.out.println("消费者2:"+message);
    }
 
}

生产者:

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/rabbitMq")
public class HelloRabbitMQController {
    @Autowired
    private AmqpTemplate amqpTemplate;
 
    /**实现消息的发送*/
    @RequestMapping(value = "/helloQueue",method = RequestMethod.GET)
    public void send(){
        String message = "this is queue test!";
        for(int i=0;i<10;i++){ --如果这里只是发送一次,只会有一个消费者能够消费到此消息,多条消息是均摊的
            this.amqpTemplate.convertAndSend("hello-queue",message);
 
        }
    }
}

结果:

消费者2:this is queue test!
消费者:this is queue test!
消费者:this is queue test!
消费者2:this is queue test!
消费者:this is queue test!
消费者2:this is queue test!
消费者:this is queue test!
消费者2:this is queue test!
消费者:this is queue test!
消费者2:this is queue test!
 ———————————————— 
版权声明:本文为CSDN博主「Yeah-小海」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zht741322694/article/details/79828088

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot与RabbitMQ整合可以通过使用Spring AMQP实现。下面是一个的步骤: 1. 添加依赖:在`pom.xml`文件中添加以下依赖关系: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置RabbitMQ连接:在`application.properties`或`application.yml`文件中配置RabbitMQ的连接信息,例如: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 3. 创建生产者:创建一个生产者,用于向RabbitMQ发送消息。你可以使用`RabbitTemplate`类来发送消息,例如: ```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MessageProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("exchangeName", "routingKey", message); } } ``` 4. 创建消费者:创建一个消费者,用于接收RabbitMQ发送的消息。你可以使用`@RabbitListener`注解来定义一个消息监听器,例如: ```java import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageConsumer { @RabbitListener(queues = "queueName") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 5. 启用RabbitMQ:通过在Spring Boot应用程序的主类上添加`@EnableRabbit`注解来启用RabbitMQ功能,例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.amqp.rabbit.annotation.EnableRabbit; @SpringBootApplication @EnableRabbit public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这就是整合Spring Boot和RabbitMQ的基本步骤。你可以根据自己的需求进行更多的高级配置和定制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值