SpringBoot整合Redis实现简单的Pub/Sub(发布/订阅)
Posted 代码不是马
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合Redis实现简单的Pub/Sub(发布/订阅)相关的知识,希望对你有一定的参考价值。
直接上代码
引入依赖
```html/xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
创建 RedisListener实现MessageListener
```java
package com.test.demotest.service;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
/**
* @Author: yipeng.liu
* @Date: 2022/3/2 18:20
* @Description: RedisListener
*/
@Log4j2
@Configuration
public class RedisListener implements MessageListener
@Override
public void onMessage(Message message, byte[] bytes)
//获取订阅消息内容
String topic = new String(bytes);
String context = new String(message.getBody());
log.info("topic:,context:",topic,context);
重写RedisMessageListenerContainer Bean订阅所要监听的topic
package com.test.demotest.service;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* @Author: yipeng.liu
* @Date: 2022/3/2 18:20
* @Description: RedisListener
*/
@Log4j2
@Configuration
public class RedisListener implements MessageListener
@Override
public void onMessage(Message message, byte[] bytes)
String topic = new String(bytes);
String context = new String(message.getBody());
log.info("topic:,context:",topic,context);
@Bean
RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory redisConnectionFactory,RedisListener redisListener)
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
//订阅topic - subscribe
redisMessageListenerContainer.addMessageListener(redisListener,new ChannelTopic("subscribe"));
return redisMessageListenerContainer;
编写测试类
package com.test.demotest.contoller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
/** @Author: yipeng.liu @Date: 2022/3/2 18:43 @Description: RedisTest */
@SpringBootTest
public class RedisTest
@Autowired private StringRedisTemplate stringRedisTemplate;
@Test
void redisTest() throws InterruptedException
int i = 0;
while (i < 10)
i++;
stringRedisTemplate.convertAndSend("subscribe", "发布信息" + i);
Thread.sleep(3000);
运行结果有如下:
这里要说一下
为什么使用StringRedisTemplate而不使用RedisTemplate
RedisTemplate是使用JdkSerializationRedisSerializer进行序列化
StringRedisTemplate是使用StringRedisSerializer进行序列化
RedisTemplate会将数据序列化为字节数组储存到Redis中
StringRedisTemplate会将数据序列化为可读数组储存下来
注意:只要当你要存的数据是字符串时才是用StringRedisTemplate
以上是关于SpringBoot整合Redis实现简单的Pub/Sub(发布/订阅)的主要内容,如果未能解决你的问题,请参考以下文章
jedis的使用,spring整合redis,springboot整合redis
springboot 整合redis 以及redis的简单使用