Java spring数据redis主从配置

Java spring数据redis主从配置,java,spring,redis,jedis,spring-data-redis,Java,Spring,Redis,Jedis,Spring Data Redis,以下是我的绝地配置 @Bean public JedisConnectionFactory getJedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setUsePool(true); return jedisConnectionFactory; } @Bean pub

以下是我的绝地配置

@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;
}
@Bean
public-JedisConnectionFactory getJedisConnectionFactory(){
JedisConnectionFactory JedisConnectionFactory=新的JedisConnectionFactory();
jedisConnectionFactory.setUsePool(true);
返回绝地连接工厂;
}
@豆子
公共RedisTemplate getRedisTemplate(){
RedisTemplate RedisTemplate=新RedisTemplate();
redisTemplate.setConnectionFactory(getJedisConnectionFactory());
返回模板;
}
当我只有一台服务器时,这个配置工作得很好。我想做的是有一个redis主设备和多个redis从设备。根据redis文档,从机读取,从主机写入。如何更改上述配置以使用master进行写入,使用slave进行读取

假设我的主服务器位于192.168.10.10,从服务器位于localhost


谢谢

此时,Spring Data Redis中没有能够启用所需行为的配置选项。绝地武士也没有为这种情况提供支持(见)。
RedisConnection
在执行操作时从工厂请求连接。此时,请求的资源的使用目的尚不清楚,因为该命令可能是
r
w
rw

一种可能的解决方案是定制的
RedisConnectionFactory
,它能够在执行只读命令的情况下提供连接到您拥有的一个从机

slavewarejedisconnectionfactory=newslavewarejedisconnectionfactory();
factory.AfterPropertieSet();
RedisConnection=factory.getConnection();
//写信给主人
connection.set(“foo.getBytes(),“bar.getBytes());
//从奴隶那里读
get(“foo.getBytes());
/**
*SlavewarEjedisconnectionFactory用一个代理将只读命令委托给从属设备的代理来包装JedisConnection。
*/
类slavewarejedisconnectionfactory扩展了JedisConnectionFactory{
/**
*获取到Redis的代理连接,该连接能够发送
*从节点的只读命令
*/
公共连接getConnection(){
JedisConnection c=super.getConnection();
ProxyFactory ProxyFactory=新的ProxyFactory(c);
addAdvice(新连接plitinginterceptor(this));
setProxyTargetClass(true);
返回JedisConnection.class.cast(proxyFactory.getProxy());
};
/**
*这一个将获得一个从机的连接,从那里读取数据
* 
*@返回
*/
公共重新连接getSlaveConnection(){
//TODO:查找提供数据服务的可用从属服务器
返回新的绝地连接(新绝地(“此处查找您的奴隶主机”);
}
静态类连接PlittingInterceptor实现MethodInterceptor,
org.springframework.cglib.proxy.MethodInterceptor{
私人最终Slavewarejedisconnection工厂;
公共连接扩展接口(SlavewareEJB连接工厂){
这个工厂=工厂;
}
@凌驾
公共对象截获(对象obj、方法Method、对象[]参数、方法代理)抛出Throwable{
RedisCommand commandToExecute=RedisCommand.failsafeCommandLookup(method.getName());
如果(!commandToExecute.isReadonly()){
返回调用(方法、对象、参数);
}
RedisConnection=factory.getSlaveConnection();
试一试{
返回调用(方法、连接、参数);
}最后{
//执行命令后正确关闭连接
如果(!connection.isClosed()){
connection.close();
}
}
}
私有对象调用(方法、对象目标、对象[]args)抛出Throwable{
试一试{
return method.invoke(target,args);
}捕获(调用TargetException e){
抛出e.getCause();
}
}
@凌驾
公共对象调用(MethodInvocation调用)抛出可丢弃的{
返回截获(invocation.getThis()、invocation.getMethod()、invocation.getArguments()、null);
}
}
}
上述解决方案包含了几个问题。例如,应用程序中的
MULTI
EXEC
块可能不再像预期的那样工作,因为命令现在可能被输送到您不希望它们出现的地方。因此,也许有多个RedisTemplates用于专用的读写目的也是有意义的。

您应该使用它来保持redis的主/从配置……您可以使用下面的连接到sentinel池-

@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);
}
参考资料:

您想要什么

这是如何配置sentinal集群和spring引导

spring:
  data:
    redis:
      sentinel: 
        master: mymaster
        nodes: my.sentinel.hostname1:26379,my.sentinel.hostname2:26379
      port: 6379
和spring配置

@Configuration
public class RedisDatasourceConfig {

  @Bean
  public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer() {
    return p -> p.readFrom(SLAVE_PREFERRED);
  }
}

主/从体系结构的复制不同于Sentinel

设置redis读取副本非常简单

我的spring boot应用程序yaml

redis:
  master:
    host: localhost
    port: 6379
  slaves:
    - host: localhost
      port: 16379
    - host: localhost
      port: 26379
对于上述实例,应更新从属实例redis config,如下所示

################################# REPLICATION #################################

# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
replicaof master 6379
在这里查看docker设置


两个绝地连接工厂和两个redis模板如何,一个用于读取,另一个用于写入?这(理论上)行得通吗?仅使用Redis Sentinel支持,sentinal将一个新节点升级为主节点时,您就可以进行故障转移,但您无法实现负载平衡,所有内容仍归主节点所有。