介紹:springboot的哨兵模式若是隻是使用redis的狀況下,在單機redis的前提下,直接在配置文件添加幾行配置就能夠了,由於springboot有自動配置加載的功能就能夠了node
一、單機的redis中的配置文件redis註釋redis
#spring.redis.database=0 #spring.redis.host=localhost #spring.redis.port=6379 #spring.redis.password= #spring.redis.timeout=10000ms
註釋掉以後,改成:spring
#redis哨兵模式 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=192.168.1.20:26379,192.168.1.20:36379,192.168.1.20:46379
添加配置類springboot
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CachingConfigurerSupport; 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 extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.sentinel.nodes}") private String redisNodes; @Value("${spring.redis.sentinel.master}") private String master; //redis哨兵配置 @Bean public RedisSentinelConfiguration redisSentinelConfiguration(){ RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(); String[] host = redisNodes.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; } //鏈接redis的工廠類 @Autowired private JedisPoolConfig jedisPoolConfig; @Autowired private RedisSentinelConfiguration redisSentinelConfiguration; @Bean public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig); return jedisConnectionFactory; } //配置RedisTemplate,設置添加序列化器,key 使用string序列化器,value 使用Json序列化器,還有一種簡答的設置方式,改變defaultSerializer對象的實現。 @Autowired private JedisConnectionFactory jedisConnectionFactory; @Bean public RedisTemplate<Object, Object> redisTemplate() { //StringRedisTemplate的構造方法中默認設置了stringSerializer RedisTemplate<Object, Object> template = new RedisTemplate<>(); //設置開啓事務 template.setEnableTransactionSupport(true); //set key serializer StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setConnectionFactory(jedisConnectionFactory()); template.afterPropertiesSet(); return template; } }
這樣咱們的springboot與redis的哨兵模式搭建成功了。spa
二、疑問:.net
咱們pom只添加了redis依賴,並無jedis!!!沒有jedis,文中出現的jedis是咱們依賴redis中裏面的java類,不是單獨集成jedis的,並且使用RedisTemplate這樣就能夠使用哨兵模式了,springboot會自動分配處理。對象
但願你們不要把集成redis和集成jedis當作同樣的,若是有問題,請你們幫忙提出來留言,我會更改的!!!blog