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