yamlredis
# redis 主從哨兵配置 spring: redis: database: 0 host: 127.0.0.1 port: 6379 password: pool: max-active: 8 max-wait: -1 # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) max-idle: 8 min-idle: 0 timeout: 0 # 主節點哨兵名 sentinel: master: mymaster # comma-separated list of host:port pairs 哨兵節點配置列表 nodes: 127.0.0.1:26380,127.0.0.1:26381
依賴spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency>
鏈接池緩存
@Configuration @EnableAutoConfiguration public class RedisCacheConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout = 0; @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的工廠類 */ @Bean public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration()); factory.setHostName(host); factory.setPort(port); factory.setTimeout(timeout); factory.setPassword(password); factory.setDatabase(database); return factory; } /** * 配置RedisTemplate 序列化器 */ @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; } /** * 設置RedisCacheManager * 使用cache註解管理redis緩存 * * @return */ @Override @Bean public RedisCacheManager cacheManager() { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate()); return redisCacheManager; } /** * 自定義生成redis-key * * @return */ @Override public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()).append("."); sb.append(method.getName()).append("."); for (Object obj : objects) { sb.append(obj.toString()); } System.out.println("keyGenerator=" + sb.toString()); return sb.toString(); } }; } }