哨兵redis配置
何为redis哨兵模式:
哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
先看一下application.yml配置文件里redis哨兵的基本配置,和单点redis配置不同的是它的sentinel
redis:
sentinel:
masters: mymaster
address: 10.2.1.222:16380,10.2.1.223:16380
#数据库索引
database: 0
password:
#连接超时时间
timeout: 5000
lettuce:
pool:
max-active: 200 #连接池最大连接数(使用负值表示没有限制)
max-idle: 100 # 连接池中的最大空闲连接
min-idle: 5 #连接池中的最小空闲连接
max-wait: 5000 # 当连接池耗尽时, 抛出异常之前,连接分配被阻塞的时间,也就是连接池满后最长等待时间,负值表示无限期等待
接下来看一下RedisConfig配置类
首先获取配置文件定义的参数值
@Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.sentinel.masters}") private String masters; @Value("${spring.redis.sentinel.address}") private Set<String> address; @Value("${spring.redis.timeout}") private long timeout; @Value("${spring.redis.lettuce.pool.max-active}") private int maxActive; @Value("${spring.redis.lettuce.pool.max-wait}") private int maxWait; @Value("${spring.redis.lettuce.pool.max-idle}") private int maxIdle; @Value("${spring.redis.lettuce.pool.min-idle}") private int minIdle;
再定义一下数据源的配置
@Bean public RedisTemplate redisTemplate() { RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(masters, address); redisSentinelConfiguration.setPassword(RedisPassword.of(password.toCharArray())); redisSentinelConfiguration.setDatabase(database); GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); genericObjectPoolConfig.setMaxIdle(maxIdle); genericObjectPoolConfig.setMinIdle(minIdle); genericObjectPoolConfig.setMaxTotal(maxActive); genericObjectPoolConfig.setMaxWaitMillis(maxWait); LettucePoolingClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder() .poolConfig(genericObjectPoolConfig) .commandTimeout(Duration.ofSeconds(timeout)) .build(); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration); connectionFactory.afterPropertiesSet(); return createRedisTemplate(connectionFactory); }
public RedisTemplate<Object, Object> createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); //序列化 FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); // value值的序列化采用fastJsonRedisSerializer template.setValueSerializer(fastJsonRedisSerializer); template.setHashValueSerializer(fastJsonRedisSerializer); // 全局开启AutoType,不建议使用 // ParserConfig.getGlobalInstance().setAutoTypeSupport(true); // 建议使用这种方式,小范围指定白名单 ParserConfig.getGlobalInstance().addAccept("cn.lbccrm.domain"); ParserConfig.getGlobalInstance().addAccept("cn.lbccrm.modules.system.service.dto"); ParserConfig.getGlobalInstance().addAccept("cn.lbccrm.service.dto"); ParserConfig.getGlobalInstance().addAccept("cn.lbccrm.modules.system.domain");// key的序列化采用StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; }
设置缓存序列化方式以及过期时间
@Bean public CacheManager CacheManager(RedisTemplate redisTemplate) { FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class); RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig(); configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(1)); CacheManager redisCacheManager = RedisCacheManager.builder(redisTemplate.getConnectionFactory()) .cacheDefaults(configuration) .transactionAware() .build(); return redisCacheManager; }
序列化定义
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private Class<T> clazz; public FastJsonRedisSerializer(Class<T> clazz) { super(); this.clazz = clazz; } @Override public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; } return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); } @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length <= 0) { return null; } String str = new String(bytes, DEFAULT_CHARSET); return (T) JSON.parseObject(str, clazz); } }