springboot整合redis的subscribe
时间: 2025-05-11 16:19:28 浏览: 11
### 整合 Redis 实现订阅功能
在 Spring Boot 中整合 Redis 并实现发布/订阅(Publish/Subscribe)模式的功能,可以通过 `RedisTemplate` 和 `MessageListenerAdapter` 来完成。以下是具体的实现方法:
#### 配置依赖项
首先,在项目的 `pom.xml` 文件中引入必要的依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
此配置会自动加载 Redis 的支持并启用相应的自动化配置[^2]。
#### 创建 Redis 订阅者类
定义一个用于监听消息的类,并通过继承 `ChannelTopic` 或其他主题来指定要订阅的主题名称。
```java
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
public class RedisSubscriber implements MessageListener {
@Override
public void onMessage(Message message, byte[] pattern) {
String channel = new String(message.getChannel());
String body = new String(message.getBody());
System.out.println("Received Message: Channel=" + channel + ", Body=" + body);
}
}
```
该类实现了 `MessageListener` 接口中的 `onMessage()` 方法,当接收到新消息时会被触发[^1]。
#### 设置 Redis 消息侦听器适配器
创建一个自定义的消息侦听器适配器实例并与前面定义好的订阅者关联起来。
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@Configuration
public class RedisConfig {
private final RedisSubscriber redisSubscriber;
public RedisConfig(RedisSubscriber redisSubscriber){
this.redisSubscriber = redisSubscriber;
}
@Bean
MessageListenerAdapter listenerAdapter() {
return new MessageListenerAdapter(redisSubscriber);
}
@Bean
ChannelTopic topic() {
return new ChannelTopic("pubsub:channel");
}
}
```
这里我们注入了一个名为 `topic()` 的 bean 表示我们要关注的具体频道名 `"pubsub:channel"`。
#### 启动 Redis 消息监听服务
最后一步是在应用程序启动期间注册我们的消息监听器到 Redis 连接工厂上。
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@Component
public class RedisPublisherInitializer {
@Autowired
ApplicationContext applicationContext;
@Autowired
RedisMessageListenerContainer container;
@Autowired
StringRedisTemplate template;
@Autowired
RedisConfig config;
public RedisPublisherInitializer(){
var adapter = (MessageListenerAdapter)applicationContext.getBean("listenerAdapter");
container.addMessageListener(adapter,new PatternTopic(config.topic().getTopic()));
}
}
```
这样就完成了整个流程设置,现在可以测试发送和接收来自 Redis 主题的消息了。
#### 测试代码片段
下面是一个简单的例子展示如何向已设定好的频道广播一条信息:
```java
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void testSendMessageToSubscribers(){
stringRedisTemplate.convertAndSend("pubsub:channel","Hello Subscriber!");
}
```
以上就是关于如何利用 Spring Boot Framework 结合 Redis 数据库达成基本 Pub/Sub 功能的一个完整示范[^3]。
---
阅读全文
相关推荐

















