redis主从哨兵模式+Lua报错-READONLY You can‘t write against a read

背景

项目试用SpringBoot+redisTemplate执行redis的lua脚本,实现令牌桶;redis结构使用的是1主2从3哨兵模式+读写分离;

问题分析

READONLY You can't write against a read报这个错的含义在从节点执行了写操作,也就是说我执行Lua脚本是在从节点上执行的,那么问题来了,为什么的我LUA脚本会在从节点执行呢?

我们都知道,redis的主从哨兵模式,再加上配置读写分离,会将读操作优先分配到从节点,也就是说它认为LUA脚本是读操作,看一下具体的报错信息:

1、我们从这一行开始看起,找找是哪一步给我分配到了从节点

复制代码
at org.springframework.data.redis.core.script.DefaultScriptExecutor.eval(DefaultScriptExecutor.java:77)

之后一步一步点击调用,到下面这一步,调用了get方法获取前面的返回值,那就继续看前面是怎么调用的,点击RedisScriptingAsyncCommands::evalsha方法继续向下寻找

2、再继续

3、再继续,可以看到,命令类型给了一个 EVALSHA

4、回到第二部看 dispatch 方法的调用,找到处理链接的地方

5、可以看到write方法中,有一个步骤判断操作类型是读还是写


6、经过查找,CommandType类中包含 EVALSHA 类型,所以判断为ReadOlny,由此可以验证,确实是使用了从节点执行lua脚本。

解决问题

既然它使用的是从节点,那我就想办法让他在执行脚本的时候,强制选择主节点就能解决问题了呀。

修改redisTemplate注入配置

java 复制代码
@Configuration
public class MyRedisConfig {
    @Value("${spring.redis.sentinel.master}")
    private String masterName;

    @Value("${spring.redis.sentinel.nodes}")
    private String sentinelNodes;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private Integer database;

    @Bean(value = "qpsRedisTemplate")
    public RedisTemplate qpsRedisTemplate() {
        List<String> sentinels = Arrays.asList(sentinelNodes.split(","));
        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration();
        sentinelConfig.master(masterName);
        Set<RedisNode> sentinelNodes = new HashSet<>();
        for (String sentinel : sentinels) {
            String[] split = sentinel.split(":");
            sentinelNodes.add(new RedisNode(split[0],Integer.parseInt(split[1])));
        }
        sentinelConfig.setSentinels(sentinelNodes);
        sentinelConfig.setDatabase(database);
        sentinelConfig.setPassword(password);
        LettuceConnectionFactory factory = new LettuceConnectionFactory(sentinelConfig);
        factory.afterPropertiesSet();
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
    //读写分离配置
    @Bean
    public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
        return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }
}
相关推荐
ChinaRainbowSea43 分钟前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
鼠鼠我捏,要死了捏2 小时前
Redis缓存穿透、缓存击穿与雪崩防护及性能优化实战指南
redis·cache·performance
麦兜*4 小时前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
山楂树下懒猴子4 小时前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
失散135 小时前
分布式专题——5 大厂Redis高并发缓存架构实战与性能优化
java·redis·分布式·缓存·架构
十八旬7 小时前
苍穹外卖项目实战(day7-1)-缓存菜品和缓存套餐功能-记录实战教程、问题的解决方法以及完整代码
java·数据库·spring boot·redis·缓存·spring cache
锐策8 小时前
Lua 核心知识点详解
开发语言·lua
2301_781668618 小时前
Redis 面试
java·redis·面试
吐泡泡_9 小时前
Redis(缓存)
redis
lanhuazui109 小时前
lua中 string.match返回值
lua