首页 > 解决方案 > 使用 Spring Data Redis 访问 Redis 连接池

问题描述

我想监视并定期记录有关 Redis 连接池使用情况的信息。

我通过 spring-data-redis RedisTemplate 对象使用 Redis。

有什么办法可以进入游泳池吗?

标签: javaredisspring-data-redisjedis

解决方案


我能够使用反射 API 访问内部池。

  private GenericObjectPool<Jedis> jedisPool() {
    try {
      Field pool = JedisConnectionFactory.class.getDeclaredField("pool");
      pool.setAccessible(true);
      Pool<Jedis> jedisPool = (Pool<Jedis>) pool.get(jedisConnectionFactory());
      Field internalPool = Pool.class.getDeclaredField("internalPool");
      internalPool.setAccessible(true);
      return (GenericObjectPool<Jedis>) internalPool.get(jedisPool);
    } catch (NoSuchFieldException | IllegalAccessException e) {
      e.printStackTrace();
    }
  }

推荐阅读