SpringBoot 整合 MQ

本文详细介绍了如何在SpringBoot中整合ActiveMQ、Kafka、RocketMQ和RabbitMQ,包括配置步骤、消息模型、操作接口和监听器的使用。讲解了JMS、AMQP和MQTT的消息规范及优缺点,并提供了相应的代码实现链接。
摘要由CSDN通过智能技术生成

目录

简介

整合ActiveMQ

整合KAFKA

整合RocketMQ

整合RabbitMQ


简介

对于消息的生产者与消费者的工作模式,还可以将消息划分成两种模式,同步消费与异步消息。

同步消费

所谓同步消息就是生产者发送完消息,等待消费者处理,消费者处理完将结果告知生产者,然后生产者继续向下执行业务。

异步消息

所谓异步消息就是生产者发送完消息,无需等待消费者处理完毕,生产者继续向下执行其他动作。

Java处理消息的标准规范

目前企业级开发中广泛使用的消息处理技术共三大类,具体如下:

  • JMS

  • AMQP

  • MQTT

JMS

JMS(Java Message Service),这是一个规范,作用等同于JDBC规范,提供了与消息服务相关的API接口。

JMS消息模型

JMS规范中规范了消息有两种模型。分别是点对点模型发布订阅模型

点对点模型:peer-2-peer,生产者会将消息发送到一个保存消息的容器中,通常使用队列模型,使用队列保存消息。一个队列的消息只能被一个消费者消费,或未被及时消费导致超时。这种模型下,生产者和消费者是一对一绑定的。

发布订阅模型:publish-subsc

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合MQ(消息队列)可以使用Spring Boot集成的JMS(Java消息服务)或AMQP(高级消息队列协议)实现。下面是使用AMQP的步骤: 1. 添加AMQP依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置连接信息 在application.properties文件中添加以下配置: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 3. 创建消息发送者 ```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("my-exchange", "my-routing-key", message); } } ``` 4. 创建消息接收者 ```java import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageReceiver { @RabbitListener(queues = "my-queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 5. 配置消息队列 ```java import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { @Bean public Queue myQueue() { return new Queue("my-queue"); } @Bean public TopicExchange myExchange() { return new TopicExchange("my-exchange"); } @Bean public Binding binding(Queue myQueue, TopicExchange myExchange) { return BindingBuilder.bind(myQueue).to(myExchange).with("my-routing-key"); } } ``` 6. 测试 创建一个Spring Boot应用程序,然后使用MessageSender发送消息,MessageReceiver将接收到该消息。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private MessageSender messageSender; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { messageSender.sendMessage("Hello, world!"); } } ``` 可以在控制台看到以下输出: ``` Received message: Hello, world! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值