当前位置: 首页 > news >正文

重学SpringBoot3-集成Redis(六)之消息队列

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》
期待您的点赞👍收藏⭐评论✍

重学SpringBoot3-集成Redis(六)之消息队列

  • 1. 什么是发布/订阅(Pub/Sub)?
  • 2. 场景应用
  • 3. Spring Boot 3 整合 Redis 实现发布/订阅
    • 3.1. 添加依赖
    • 3.2. 配置 Redis 连接
    • 3.3. 实现消息发布功能
    • 3.4. 实现消息订阅功能
    • 3.5. 测试发布/订阅功能
    • 3.6. 使用Redisson
  • 4. 总结

Redis 不仅是一个高效的缓存解决方案,也具备强大的消息队列功能。通过 Redis 的 发布/订阅(Pub/Sub) 机制,开发者可以轻松实现服务之间的通信和消息传递功能,而无需引入专门的消息队列工具。这篇文章将介绍如何通过 Spring Boot 3Redis 实现消息队列的发布与订阅功能。

1. 什么是发布/订阅(Pub/Sub)?

发布/订阅是一种消息传递模式,发布者发送消息到某个频道(channel),而订阅了该频道的所有订阅者都会收到该消息。这种模式与传统的消息队列不同,不会将消息存储下来,而是将其立即广播给所有的订阅者。因此,发布/订阅模式非常适合用于通知、事件广播等实时性较强的场景。

  • 发布者:向一个或多个频道发布消息。
  • 订阅者:订阅一个或多个频道,实时接收消息。
图片来源:https://pdai.tech/md/db/nosql-redis/db-redis-x-pub-sub.html

2. 场景应用

  • 事件驱动系统:如任务通知、状态更新、日志广播。
  • 消息通知服务:如实时的新闻推送、股票行情推送。
  • 微服务通信:不同服务之间的消息传递。

3. Spring Boot 3 整合 Redis 实现发布/订阅

在 Spring Boot 3 中,我们可以通过 Spring Data Redis 轻松集成 Redis 的发布/订阅功能。

3.1. 添加依赖

首先,我们需要在项目的 pom.xml 文件中添加必要的依赖,详细参考重学SpringBoot3-集成Redis(一)基本使用。

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

3.2. 配置 Redis 连接

application.yml 中配置 Redis 连接信息:

spring:data:redis:host: localhostport: 6379            # Redis 端口password:             # 如果有密码可以在这里配置lettuce:pool:max-active: 100    # 最大并发连接数max-idle: 50       # 最大空闲连接数min-idle: 10       # 最小空闲连接数

3.3. 实现消息发布功能

首先,我们需要创建一个 消息发布者,用于发送消息到特定的频道:

package com.coderjia.boot310redis.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;/*** @author CoderJia* @create 2024/10/6 下午 10:44* @Description**/
@Component
public class MessagePublisher {@Autowiredprivate RedisTemplate redisTemplate;public void publish(String channel, String message) {redisTemplate.convertAndSend(channel, message);System.out.println("Message published to channel " + channel + ": " + message);}
}

在这个类中,RedisTemplate 被用来将消息发送到指定的频道。

3.4. 实现消息订阅功能

接下来,我们实现一个 消息订阅者,用于监听特定频道的消息:

package com.coderjia.boot310redis.config;import org.springframework.stereotype.Component;/*** @author CoderJia* @create 2024/10/6 下午 10:45* @Description**/
@Component
public class MessageSubscriber {public void onMessage(String message, String channel) {System.out.println("Received message from channel " + channel + ": " + message);}
}

为了让这个订阅者生效,我们需要注册一个消息监听器:

package com.coderjia.boot310redis.config;import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** @author CoderJia* @create 2024/10/4 下午 12:43* @Description**/
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// 使用String序列化器序列化Keytemplate.setKeySerializer(new StringRedisSerializer());// 使用Jackson2JsonRedisSerializer序列化ValueObjectMapper objectMapper = new ObjectMapper();Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(objectMapper,Object.class);template.setValueSerializer(serializer);return template;}@Beanpublic RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new ChannelTopic("myChannel"));return container;}@Beanpublic MessageListenerAdapter listenerAdapter(MessageSubscriber subscriber) {return new MessageListenerAdapter(subscriber, "onMessage");}
}

在这个配置类中,我们使用 RedisMessageListenerContainer 来监听频道消息,并使用 MessageListenerAdapter 将消息处理委托给 MessageSubscriber

3.5. 测试发布/订阅功能

在我们的控制器或服务中,我们可以调用 MessagePublisher 来发布消息,并观察 MessageSubscriber 是否正确接收消息。

package com.coderjia.boot310redis.demos.web;import com.coderjia.boot310redis.config.MessagePublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @author CoderJia* @create 2024/10/6 下午 10:47* @Description**/
@RestController
public class PubSubController {@Autowiredprivate MessagePublisher messagePublisher;@GetMapping("/publish")public String publishMessage(@RequestParam String message) {messagePublisher.publish("myChannel", message);return "Message published!";}
}

现在,我们可以启动应用程序,并通过访问 curl http://localhost:8080/publish?message=Hello 来测试消息发布,订阅者会自动接收到该消息。

测试

也可以使用 redis 命令 PUBLISH myChannel "Hello, world!" 向渠道发布消息,订阅者同样可以接收到消息。

PUBLISH发布消息

3.6. 使用Redisson

使用 Redisson 同样能能实现发布订阅功能,而且是更接近 MQ 使用方式,下列代码仅供参考。

    public void publish(String channel, String message) {// redisTemplate.convertAndSend(channel, message);// System.out.println("Message published to channel " + channel + ": " + message);// 获取TopicRTopic topic = redissonClient.getTopic("myChannel");// 向渠道发送消息topic.publish("Hello, world!");topic.addListener(String.class, (channel1, message1) -> {System.out.println("Received message from channel " + channel1 + ": " + message1);});}

4. 总结

通过 Spring Boot 3 与 Redis 的整合,消息发布与订阅功能的实现非常简洁且高效。Redis 的发布/订阅功能不仅可以用于简单的消息通知,还可以结合其他业务场景,如微服务通信、日志广播等。虽然 Redis 的 Pub/Sub 并不具备消息持久化的能力,但它在需要即时消息传递的场景下,具有很高的性能和灵活性。

这篇文章为 Redis 消息队列功能奠定了基础,后续将深入 Redis 的其他功能,如缓存管理、分布式锁等。如果你对 Redis 有其他问题或建议,欢迎留言讨论!

http://www.lryc.cn/news/455136.html

相关文章:

  • LeetCode 134 Gas Station 解题思路和python代码
  • 服务攻防
  • leetcode 力扣算法题 快慢指针 双指针 19.删除链表的倒数第n个结点
  • 网络五层模型:物理层、数据链路层、网络层、传输层、应用层,分别解决了什么问题?
  • OpenCV视频I/O(18)视频写入类VideoWriter之初始化 VideoWriter 对象的函数open()的使用
  • 大数据处理从零开始————4.认识HDFS分布式文件系统
  • jwt认证课件讲解
  • 【判断推理】逻辑基础
  • AcWing 655:天数转换 ← 整除、求余
  • 【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
  • 算法笔记(十三)——BFS 解决最短路问题
  • Android 简单实现联系人列表+字母索引联动效果
  • 自动驾驶-问题笔记-待解决
  • 在掌控板中加载人教版信息科技教学指南中的educore库
  • 关于CSS Grid布局
  • 初始爬虫12(反爬与反反爬)
  • 成像基础 -- 最大对焦清晰的物距计算
  • win10服务器启动且未登录时自动启动程序
  • 算法专题四: 前缀和
  • 【Linux】基础IO(文件描述符、缓冲区、重定向)
  • 一篇文章快速学会docker容器技术
  • 【MySQL】使用 JDBC 连接数据库
  • 数据结构与算法笔记:概念与leetcode练习题
  • 十大时间序列预测模型
  • G2O 通过工厂函数类 OptimizationAlgorithmFactory 来生成固定搭配的优化算法
  • 手机USB连接不显示内部设备,设备管理器显示“MTP”感叹号,解决方案
  • SpringBootWeb快速入门!详解如何创建一个简单的SpringBoot项目?
  • RabbitMQ 入门到精通指南
  • ARM base instruction -- movz
  • 安装jdk安装开发环境与maven