spring data redis master slave config

ris*_*p89 3 java spring redis jedis spring-data-redis

以下是我的jedis配置

@Bean
public JedisConnectionFactory getJedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setUsePool(true);
    return jedisConnectionFactory;
}

@Bean
public RedisTemplate<String, Object> getRedisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(getJedisConnectionFactory());
    return redisTemplate;
}
Run Code Online (Sandbox Code Playgroud)

当我有单个服务器时,此配置很有效.我想要做的是拥有1个redis master和多个redis slave.根据redis文档,读取应该从slave发生,写入应该从master发生.如何更改上述配置以使用master进行写入和slave进行读取?

可以说我的主人在192.168.10.10,奴隶在localhost.

谢谢!

Chr*_*obl 5

目前,Spring Data Redis中没有可以实现所需行为的配置选项.Jedis也没有为这种情况提供支持(参见jedis#458). RedisConnection在执行操作时请求来自工厂的连接.此时,请求它的资源的使用目的不明确,因为命令可能是r,wrw.

一个可能的解决方案是RedisConnectionFactory能够提供连接的自定义- 在您执行只读命令的情况下,为您拥有的一个从属设备.

SlaveAwareJedisConnectionFactory factory = new SlaveAwareJedisConnectionFactory();
factory.afterPropertiesSet();

RedisConnection connection = factory.getConnection();

// writes to master
connection.set("foo".getBytes(), "bar".getBytes());

// reads from slave
connection.get("foo".getBytes());

/**
 * SlaveAwareJedisConnectionFactory wraps JedisConnection with a proy that delegates readonly commands to slaves.
 */
class SlaveAwareJedisConnectionFactory extends JedisConnectionFactory {

  /**
    * Get a proxied connection to Redis capable of sending
    * readonly commands to a slave node
    */
  public JedisConnection getConnection() {

    JedisConnection c = super.getConnection();

    ProxyFactory proxyFactory = new ProxyFactory(c);
    proxyFactory.addAdvice(new ConnectionSplittingInterceptor(this));
    proxyFactory.setProxyTargetClass(true);

    return JedisConnection.class.cast(proxyFactory.getProxy());
  };

  /**
   * This one will get the connection to one of the slaves to read from there
   * 
   * @return
   */
  public RedisConnection getSlaveConnection() {

    //TODO: find the an available slave serving the data
    return new JedisConnection(new Jedis("your slave host lookup here"));
  }

  static class ConnectionSplittingInterceptor implements MethodInterceptor,
      org.springframework.cglib.proxy.MethodInterceptor {

    private final SlaveAwareJedisConnectionFactory factory;

    public ConnectionSplittingInterceptor(SlaveAwareJedisConnectionFactory factory) {
      this.factory = factory;
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

      RedisCommand commandToExecute = RedisCommand.failsafeCommandLookup(method.getName());

      if (!commandToExecute.isReadonly()) {
        return invoke(method, obj, args);
      }

      RedisConnection connection = factory.getSlaveConnection();

      try {
        return invoke(method, connection, args);
      } finally {
        // properly close the connection after executing command
        if (!connection.isClosed()) {
          connection.close();
        }
      }
    }

    private Object invoke(Method method, Object target, Object[] args) throws Throwable {

      try {
        return method.invoke(target, args);
      } catch (InvocationTargetException e) {
        throw e.getCause();
      }
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
      return intercept(invocation.getThis(), invocation.getMethod(), invocation.getArguments(), null);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的解决方案存在几个问题.例如. MULTI EXEC您的应用程序中的块可能不再按预期工作,因为命令现在可能会被管道传送到您不希望它们出现的位置.因此,也许这也将是有意义的有多个RedisTemplates专用的读,的目的.


kha*_*are 5

您应该使用Redis Sentinel来保留 Redis 的主/从配置...您可以使用下面的连接到哨兵池 -

@Bean
public RedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
    .sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
return new JedisConnectionFactory(sentinelConfig);
}
Run Code Online (Sandbox Code Playgroud)

参考:Spring Sentinel 支持

  • 单独使用 Redis Sentinel 支持,当 Sentinal 将新节点提升为 master 时,您会得到故障转移,但您不会获得负载平衡,一切仍然会转到 master 上。 (2认同)