以下是我的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;
}
当我有单个服务器时,此配置很有效。我想要做的是拥有1个redis master和多个redis slave。根据redis文档,读取应该从slave发生,写入应该从master发生。如何更改上述配置以使用master进行写入和slave进行读取?
假设我的主人在192.168.10.10,奴隶在本地。
谢谢!
答案 0 :(得分:5)
目前,Spring Data Redis中没有可以实现所需行为的配置选项。 Jedis 自己也没有为这种情况提供支持(参见jedis #458)。
RedisConnection
在执行操作时请求来自工厂的连接。此时,请求资源的使用目的不明确,因为命令可以是r
,w
或rw
。
一个可能的解决方案是一个自定义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);
}
}
}
上面的解决方案存在几个问题。例如。您的应用程序中的MULTI
EXEC
块可能不再按预期工作,因为命令现在可能会被管道传送到您不希望它们到达的位置。因此,对于专用的读取,写目的,可能有多个RedisTemplates
也是有意义的。
答案 1 :(得分:2)
您应该使用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);
}
答案 2 :(得分:0)
您要write-to-master-read-from-slave
这是如何使用哨兵群集和spring-boot对其进行配置
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);
}
}
答案 3 :(得分:0)
具有主/从体系结构的复制与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设置。
https://www.vinsguru.com/redis-master-slave-with-spring-boot/