refactor(RedissonConfig.java): Redisson分布式锁配置读取优化

This commit is contained in:
郝先瑞 2022-02-15 23:22:56 +08:00
parent dee3f2962b
commit b53e923bfc

View File

@ -1,47 +1,47 @@
package com.youlai.common.redis;
import cn.hutool.core.util.StrUtil;
import lombok.Setter;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 分布式锁 Redisson net配置
* 分布式锁 Redisson 配置
*
* @author huawei
* @email huawei_code@163.com
* @date 2021/2/22
*/
@ConditionalOnProperty(prefix = "redisson",name = "address")
@ConfigurationProperties(prefix = "redisson")
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
@Setter
private String address;
@Setter
private String password;
@Value("${spring.redis.database}")
@Setter
private Integer database;
@Value("${spring.redis.lettuce.pool.min-idle}")
private Integer connectionMinimumIdleSize = 1;// 默认最小空闲连接数
@Setter
private Integer minIdle;// 默认最小空闲连接数
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setAddress("redis://" + host + ":" + port);
singleServerConfig.setAddress(address);
singleServerConfig.setDatabase(database);
singleServerConfig.setConnectionMinimumIdleSize(connectionMinimumIdleSize);
if(StrUtil.isNotBlank(password)){
singleServerConfig.setConnectionMinimumIdleSize(minIdle);
if (StrUtil.isNotBlank(password)) {
singleServerConfig.setPassword(password);
}
singleServerConfig.setConnectTimeout(10000);
return Redisson.create(config);
}