redis主从配置sentinel模式springboot访问

…衆ロ難τιáo~ 提交于 2019-12-10 06:18:13

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!