matrix.boot.redis.config.RedissonAutoConfiguration Maven / Gradle / Ivy
package matrix.boot.redis.config;
import matrix.boot.common.exception.ServiceException;
import matrix.boot.redis.properties.RedisProperties;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* @author wangcheng
* 2021/4/22
**/
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnProperty(value = {"matrix.redis.enabled"})
public class RedissonAutoConfiguration {
/**
* redis配置
*/
private final RedisProperties redisProperties;
public RedissonAutoConfiguration(RedisProperties redisProperties) {
this.redisProperties = redisProperties;
}
/**
* 创建redisson客户端
* @return redisson客户端
*/
@Bean
@ConditionalOnProperty(value = {"matrix.redis.redisson"})
public RedissonClient redisson() {
Config config = new Config();
int timeout = redisProperties.getTimeout().intValue() * 1000;
if (redisProperties.getStandalone().isEnabled()) {
//单机模式
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress("redis://" + redisProperties.getStandalone().getHost() + ":" + redisProperties.getStandalone().getPort())
.setTimeout(timeout)
.setDatabase(redisProperties.getStandalone().getDatabase());
if(!StringUtils.isEmpty(redisProperties.getStandalone().getPassword())) {
serverConfig.setPassword(redisProperties.getStandalone().getPassword());
}
return Redisson.create(config);
}
if (redisProperties.getSentinel().isEnabled()) {
//哨兵模式
SentinelServersConfig serverConfig = config.useSentinelServers()
.setMasterName(redisProperties.getSentinel().getMaster())
.setTimeout(timeout)
.setDatabase(redisProperties.getSentinel().getDatabase());
if (!StringUtils.isEmpty(redisProperties.getSentinel().getNodes())) {
String[] nodes = redisProperties.getSentinel().getNodes().split(",");
for (String node: nodes) {
serverConfig.addSentinelAddress("redis://" + node);
}
}
if(!StringUtils.isEmpty(redisProperties.getSentinel().getPassword())) {
serverConfig.setPassword(redisProperties.getSentinel().getPassword());
}
return Redisson.create(config);
}
if (redisProperties.getCluster().isEnabled()) {
//集群模式
ClusterServersConfig serverConfig = config.useClusterServers()
.setTimeout(timeout);
if (!StringUtils.isEmpty(redisProperties.getCluster().getNodes())) {
String[] nodes = redisProperties.getCluster().getNodes().split(",");
for (String node: nodes) {
serverConfig.addNodeAddress("redis://" + node);
}
}
if(!StringUtils.isEmpty(redisProperties.getCluster().getPassword())) {
serverConfig.setPassword(redisProperties.getCluster().getPassword());
}
return Redisson.create(config);
}
throw new ServiceException("standalone, sentinel, cluster don't deactivate all");
}
}