redis主从配置 master:127.0.0.1:6379 slave:127.0.0.1:6378
-
master配置
redis.conf 默认无需修改
sentinel.conf
port 26379
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel monitor <master-name> <ip> <redis-port> <quorum>
-
salve配置
redis.conf
port 6378
slaveof 127.0.0.1 6379
sentinel.conf
port 26378
sentinel monitor mymaster 127.0.0.1 6379 1
-
redis启动
redis-server redis.conf
redis-server sentinel.conf --sentinel
-
springboot访问配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisSentinelConfig {
@Value("${redis.hosts:testhost}")
private String redisHosts;
@Value("${redis.master:mymaster}")
private String master;
@Value("${redis.maxIdle:300}")
private int maxIdle;
@Value("${redis.maxTotal:600}")
private int maxTotal;
@Value("${redis.maxWaitMillis:3000}")
private int maxWaitMillis;
@Value("${redis.testOnBorrow:true}")
private boolean testOnBorrow;
@Value("${redis.pass:redis}")
private String pass;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMinIdle(10);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
return jedisPoolConfig;
}
@Bean
public RedisSentinelConfiguration redisSentinelConfiguration(){
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
String[] host = redisHosts.split(",");
for(String redisHost : host){
String[] item = redisHost.split(":");
String ip = item[0];
String port = item[1];
configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
}
configuration.setMaster(master);
return configuration;
}
@Autowired
private JedisPoolConfig jedisPoolConfig;
@Autowired
private RedisSentinelConfiguration redisSentinelConfiguration;
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory =
new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig);
return jedisConnectionFactory;
}
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
@Bean
public RedisTemplate<?,?> redisTemplate() {
@SuppressWarnings("rawtypes")
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
参考: redis哨兵模式问题 > http://www.oschina.net/question/1273973_2134749?fromerr=FMuxr4eP
Redis哨兵 > http://redis.majunwei.com/topics/sentinel.html
来源:oschina
链接:https://my.oschina.net/u/2007391/blog/625879