前言
一、先改好RedisConfig
前提是已经建好Redis的springboot项目,按网上的操作就行。我的是在这基础上操作
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
private final RedisProperties properties;
public RedisConfig(RedisProperties properties){
this.properties = properties;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(getConnectionFactory());
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisConnectionFactory getConnectionFactory() {
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
configuration.setMaster(properties.getSentinel().getMaster());
configuration.setPassword(properties.getPassword());
configuration.setDatabase(properties.getDatabase());
List<String> nodes = properties.getSentinel().getNodes();
nodes.forEach(node -> {
String[] str = node.split(":");
RedisNode redisServer = new RedisServer(str[0], Integer.parseInt(str[1]));
configuration.sentinel(redisServer);
});
LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, getPool());
factory.setValidateConnection(true);
return factory;
}
@Bean
public LettuceClientConfiguration getPool() {
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder
builder = LettucePoolingClientConfiguration.builder().
commandTimeout(Duration.ofMillis(60000));
RedisProperties.Pool pool = properties.getLettuce().getPool();
genericObjectPoolConfig.setMaxIdle(pool.getMaxIdle());
genericObjectPoolConfig.setMinIdle(pool.getMinIdle());
genericObjectPoolConfig.setMaxTotal(pool.getMaxActive());
genericObjectPoolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());
builder.shutdownTimeout(Duration.ofMillis(4000));
builder.poolConfig(genericObjectPoolConfig);
return builder.build();
}
二、修改nacos中Redis的配置
redis:
sentinel:
master: master
nodes: 192.168.2.237:6776,192.168.2.37:6777,192.168.2.37:6778
password: 123456
timeout: 1000
database: 3
lettuce:
pool:
max-active: 8
max-wait: 1000
max-idle: 8
min-idle: 0
三、其他
1.踩到的坑如下:
java一直无法连接sentinel,关闭防火墙也不行 解决方法:因为之前redis配置的防火墙打开,开放端口,所以这里也不能关闭防火墙,而是要开放sentinel的几个端口。
1.下期将讲解:ngnix实现sentinel的负载均衡
总结
*随心所往,看见未来。Follow your heart,see night!* **欢迎点赞、关注、留言,一起学习、交流!**
|