RabbitMQ消息的异步发送

RabbitMQ 是一个消息队列中间件,主要用于异步处理、解耦和缓冲。它实现了高级的消息队列协议(AMQP),并提供了可靠性、强大的管理界面和丰富的客户端库,是非常流行的分布式消息系统。

RabbitMQ 的基本思想是生产者将消息发送到队列中,然后消费者从队列中取出消息进行处理。在 RabbitMQ 中,生产者和消费者不知道对方的存在,并且服务于 RabbitMQ Broker 上,即使有一端暂停了也不会影响另外一端的工作,从而保证了整个系统的稳定性和可靠性。

RabbitMQ 通过 AMQP(Advanced Message Queuing Protocol)实现了传输超时、负载均衡、分发路由等一系列功能,具有以下几个优点:

  1. 可靠性:RabbitMQ 通过持久化和数据备份来保证消息可靠性。

  2. 可拓展性:可以通过添加新服务器来扩展应用程序的容量,从而增加资源利用率。

  3. 高可用性:多节点环境下,在某个节点失效的情况下,其它节点负责接替工作,从而保证 RabbitMQ 服务的高可用性。

  4. 灵活性:RabbitMQ 具有灵活的交换机和堆栈路由器等机制,能够实现多种消息传递模型。

  5. 可维护性:RabbitMQ 提供了Web管理控制台,可以方便地查看队列状态、消息大小等信息,并进行开发工具集成。

总之,RabbitMQ 具有高性能、高可用性、简单易用、灵活可扩展等优点,被广泛应用于分布式系统和云计算架构中。

导入依赖

<!-- Maven -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置yml

spring:
  rabbitmq:
    host: localhost
    port: 5123
    username: qql
    password: qql

配置交换机与消息队列及绑定关系

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

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

    @Bean
    public DirectExchange myExchange() {
        return new DirectExchange("qqlExchange");
    }

    @Bean
    public Binding binding(Queue myQueue, DirectExchange myExchange) {
        return BindingBuilder.bind(myQueue).to(myExchange).with("qqlRoutingKey");
    }
}

创建一个消息生产者

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String exchange, String routingKey, String message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }
}

创建一个消息消费者

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQConsumer {

    @RabbitListener(queues = "qqlQueue")
    public void handleMessage(String message) {
        System.out.println("message: " + message);
    }
}

测试

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Autowired
    private RabbitMQProducer rabbitMQProducer;

    @PostMapping("/send")
    public String sendMessage(@RequestParam("message") String message) {
        rabbitMQProducer.send("qqlExchange", "qqlRoutingKey", message);
        return "Success!";
    }
}

启动项目后,访问 http://localhost:端口号/send?message=Hello+World,您将看到控制台输出接收到的消息:“message: Hello World”。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring BootRabbitMQ 结合使用可以方便地构建基于消息队列的异步通信系统。在 Spring Boot 中,RabbitMQ 提供了一个简单易用的整合方案,让你能够轻松地处理消息的生产者(Producer)和消费者(Consumer)。 1. 引入依赖:首先,你需要在你的 Maven 或 Gradle 项目中添加 Spring AMQP 和 RabbitMQ 的依赖。例如,Maven 项目的 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 的配置类:Spring Boot 会自动扫描该类下的配置,比如创建 RabbitTemplate 实例。 ```java import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setHost("localhost"); factory.setUsername("guest"); factory.setPassword("guest"); return factory; } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); return template; } } ``` 4. 生产者(发送消息):使用 `RabbitTemplate` 发送消息到指定队列。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RabbitProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message, String queueName) { rabbitTemplate.convertAndSend(queueName, message); } } ``` 5. 消费者(接收消息):创建一个消费者类,监听指定队列,并处理接收到的消息。 ```java import org.springframework.amqp.annotation.DeleteQueue; import org.springframework.amqp.annotation.QueueBind; import org.springframework.amqp.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @QueueBind(value = "myQueue") public class RabbitConsumer { @RabbitListener public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值