使用Spring Data Redis访问Redis连接池

huangapple go评论70阅读模式
英文:

Access Redis connection pool using Spring Data Redis

问题

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

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

有没有办法访问连接池?

英文:

I want to monitor and periodically log information about the Redis Connection Pool usage.

I use Redis through spring-data-redis RedisTemplate object.

Is there any way to access pool?

答案1

得分: 0

我能够使用反射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();
  }
}
英文:

I was able to access internal pool using reflection API.

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

huangapple
  • 本文由 发表于 2020年9月14日 19:39:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63883545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定