使用redis实现发布订阅功能及问题

如何使用redis实现发布订阅及遇到的问题

使用背景:

服务A通过接口操作服务B,实现相应逻辑。生产环境上,服务A有两个pod,服务B有3个pod

通过接口调用时,请求只能打到服务B的一个pod上,而我们想要的是服务B的每个pod都能收到服务A发送的请求数据,所以使用redis的发布订阅来实现

实现方法:

1、发送方-服务A

构造需要发送的实体数据objectData

String jsonStr = JSONUtil.toJsonStr(objectData);

使用redisTemplate.convertAndSend("channel_operate",jsonStr);方法像指定的通道"channel_operate"中发送的数据为Object类型

2、接收方-服务B

1)redis配置类中,增加消息监听的逻辑

cpp 复制代码
/**
* 消息监听
*/
@Bean
public MessageListenerAdapter messageListenerAdapter(RedisSubscriber scriber){
    return new MessageListenerAdapter(scriber);
}

/**
* 消息监听容器
*/
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,MessageListenerAdapter messageListenerAdapter){

    RedisMessageListenerContainer  container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(messageListenerAdapter,new ChannelTopic("channel_operate"));
    return container;
}

2)增加订阅到的redis消息类

cpp 复制代码
@Component
public class RedisSubscriber  implements MessageListener {
     
     /**
     * 处理接收到的消息
     */
     public void onMessage(Message message){
         
         //接收到数据字符串
         String valueStr = message.toString();

         //去掉数据中的转义字符
         StringEscapeUtils.unescapeJava(valueStr);
         //如果处理后的数据开头和结尾有多余的字符,使用substring截取你需要的数据即可
         
         //转换为实体对象,JSONUtil是hutool-all包中的工具类
         JSONUtil.toBean();
         //处理自己的逻辑
         ......  
    }
}

代码咔咔写完了,然后就是启动服务测试逻辑了

。。启动报错了。。一个错误是说啥超时,另一个问题就是

java.lang.NoClassDefFoundError: org/springframework/data/redis/connection/SubscriptionListener

提示就是加载SubscriptionListener这个类失败了,找不到,搜一圈之后各种尝试,然后找到了解决办法

说是redis包的问题,项目中用的是

cpp 复制代码
<dependency>
     <groupId>org.redisson</groupId>
     <artifactId>redisson-spring-boot-starter</artifactId>
</dependency>

需要改成

cpp 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
</dependency>

再启动就正常了

相关推荐
拾光Ծ1 小时前
【MySQL】对表数据的操作:增删查改(CRUD)
android·数据库·sql·mysql
这个DBA有点耶14 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
这个DBA有点耶17 小时前
从MySQL 5.7到8.0:JSON查询性能差在哪?虚拟列索引vs多值索引怎么选
数据库·mysql·架构
胡写代码17 小时前
数据库审计字段,别再每张表各写一套了——我统一成这 6 个字段
数据库
SelectDB21 小时前
为什么 JSON 正在成为分析数据库新的竞争点?
数据库·json·agent
ClouGence1 天前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
发霉的馒头1 天前
ORA-00845: MEMORY_TARGET not supported on this system的解决方法
数据库
草莓熊Lotso1 天前
【Redis 初阶】Set 类型深度解析:去重集合的运算能力与实战场景
linux·网络·数据库·windows·redis·tcp/ip·缓存
煎饼皮皮侠1 天前
【设计】设计一个web版的数据库管理平台后端(之五) --借鉴mybatis
数据库·mybatis
东风破_1 天前
danci 2:创建的单词书到底存在哪里?从 Supabase 一路理解 ORM、Drizzle 和 RLS
数据库·后端·node.js