@Autowired
RedisProperties redisProperties;
/**
* 哨兵模式自动装配
*
* @return
*/
@Bean(destroyMethod = "shutdown")
@ConditionalOnProperty(name="spring.redis.strategy", havingValue = "3")
public RedissonClient redissonSentinel() {
Config config = new Config();
String[] nodes = redisProperties.getSentinel().getNodes().split(",");
List<String> newNodes = new ArrayList(nodes.length);
Arrays.stream(nodes).forEach((index) -> newNodes.add(
index.startsWith("redis://") ? index : "redis://" + index));
SentinelServersConfig sentinelServersConfig = config.useSentinelServers()
.addSentinelAddress(newNodes.toArray(new String[0]))
.setMasterName(redisProperties.getSentinel().getMaster())
.setMasterConnectionPoolSize(250)
.setSlaveConnectionPoolSize(250);
if (StringUtils.isNotBlank(redisProperties.getPassword())) {
sentinelServersConfig.setPassword(redisProperties.getPassword());
}
config.setCodec(new JsonJacksonCodec(objectMapper()));
return Redisson.create(config);
}
/**
* 集群模式自动装配
*
* @return
*/
@Bean(destroyMethod = "shutdown")
@ConditionalOnProperty(name="spring.redis.strategy", havingValue = "2")
public RedissonClient redissonCluster() {
Config config = new Config();
List<String> nodes = redisProperties.getCluster().getNodes();
List<String> newNodes = Lists.newArrayListWithCapacity(nodes.size());
nodes.forEach((index) -> newNodes.add(
index.startsWith("redis://") ? index : "redis://" + index));
ClusterServersConfig clusterServersConfig = config.useClusterServers()
.addNodeAddress(newNodes.toArray(new String[0]));
if (StringUtils.isNotBlank(redisProperties.getPassword())) {
clusterServersConfig.setPassword(redisProperties.getPassword());
}
config.setCodec(new JsonJacksonCodec(objectMapper()));
return Redisson.create(config);
}
/**
* 单机模式自动装配
*
* @return
*/
@Bean(destroyMethod = "shutdown")
@ConditionalOnProperty(name="spring.redis.strategy", havingValue = "1")
public RedissonClient redissonSingle() {
Config config = new Config();
String redisAddress=String.format("redis://%s:%d",redisProperties.getHost(),redisProperties.getPort());
SingleServerConfig singleServerConfig = config.useSingleServer()
.setAddress(redisAddress)
.setConnectionMinimumIdleSize(redisProperties.getPool().getMinIdle());
if (StringUtils.isNotBlank(redisProperties.getPassword())) {
singleServerConfig.setPassword(redisProperties.getPassword());
}
config.setCodec(new JsonJacksonCodec(objectMapper()));
return Redisson.create(config);
}
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
//大小写不敏感
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setDateFormat(new SimpleDateFormat(GlobalConstant.DEFAULT_DATE_TIME_FORMAT));
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(GlobalConstant.DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(GlobalConstant.DEFAULT_DATE_TIME_FORMAT)));
mapper.registerModule(javaTimeModule);
return mapper;
}
SpringBoot连接redis,支持单点、集群、哨兵模式自适应配置(二、redissonClient)
最新推荐文章于 2024-10-06 10:52:42 发布