Sentinel整合redis问题

来源:17-14 SpringCloud Alibaba Sentinel 实现网关动态限流总结

慕运维9598587

2025-01-20

学习了Sentinel 打算整合redis 如果redis挂了 直接走数据库 ,之前配置了spring:redis:timeout: 30000ms ,发现 如果停止了redis, 程序会根据spring:redis:timeout: 30000ms 过了30s时间才会走fallback, 后来设置 Sentinel RuleConstant.DEGRADE_GRADE_RT 依然需要 过30s时间才会走fallback ,按照代码过了阈值10ms会自动fallback , 问一下怎么处理这个问题

/**
 * Redis工具类*/
@Component
public class RedisUtil {


    static {
        // 设置熔断规则
        DegradeRule rule = new DegradeRule();
        rule.setResource("get");
        rule.setGrade(RuleConstant.DEGRADE_GRADE_RT); // 设置熔断策略为响应时间
        rule.setCount(10); // 设置响应时间的阈值为10ms
        rule.setTimeWindow(5); // 设置时间窗口,单位是秒
        DegradeRuleManager.loadRules(Collections.singletonList(rule));
    }


    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;



    /**
     * 设置指定 key 的值
     * @param key
     * @param value
     */
    @SentinelResource(value = "set", fallback = "redisFallback", fallbackClass = {RedisUtil.class })
    public void set(String key, Object value) {
        int i = 1/0;
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 获取指定 key 的值
     * @param key
     * @return
     */
    @SentinelResource(value = "get", fallback = "getRedisFallback", fallbackClass = {RedisUtil.class })
    public Object get(String key) {
        System.out.println("key");
        LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) SpringContextUtils.getApplicationContext().getBean("redisConnectionFactory");
//        LettuceConnectionFactory conn = (LettuceConnectionFactory) lettuceConnectionFactory.ge;
        System.out.println(lettuceConnectionFactory.getClientConfiguration().getCommandTimeout().getSeconds());
        System.out.println(lettuceConnectionFactory.getTimeout());
//        int i = 1/0;
        return redisTemplate.opsForValue().get(key);
    }


    public static void redisFallback(String key, Object value) {
       System.out.println("redisFallback");
    }

    public static Object getRedisFallback(String key) {
        System.out.println("getredisFallback");
//        RedisConnectionFactory redisConnectionFactory= redisTemplate.getConnectionFactory();
//        LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) SpringContextUtils.getApplicationContext().getBean("redisConnectionFactory");
//        LettuceConnectionFactory conn = (LettuceConnectionFactory) lettuceConnectionFactory.ge;
//        lettuceConnectionFactory.setTimeout(100);
//        lettuceConnectionFactory.getClientConfiguration()
//        lettuceConnectionFactory.
//        RedisProperties redisProperties = (RedisProperties) SpringContextUtils.getApplicationContext().getBean("lettuceConnectionConfiguration");
//        System.out.println("getredisFallback" + JSONObject.toJSONString(redisProperties));
//        lettuceConnectionFactory.resetConnection();
        System.out.println();
        return  null;

    }

}

2025-01-20 19:29:37.522  INFO 15492 --- [xecutorLoop-1-1] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 127.0.0.1:6379
2025-01-20 19:29:38.525  WARN 15492 --- [ioEventLoop-4-1] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [127.0.0.1:6379]: Connection refused: no further information: /127.0.0.1:6379
key
30
30000
2025-01-20 19:30:08.621  INFO 15492 --- [xecutorLoop-1-3] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was 127.0.0.1:6379
getredisFallback
写回答

1回答

张勤一

2025-04-25

不好意思,没有这样使用过,可以查查博客

0
0

Spring Cloud / Alibaba 微服务架构实战

从架构设计到开发实践,手把手实现

1218 学习 · 674 问题

查看课程