2026-01-16 14:13:44 +08:00
|
|
|
|
package com.sipai.config;
|
|
|
|
|
|
|
|
|
|
|
|
import org.redisson.Redisson;
|
|
|
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
|
|
|
import org.redisson.client.codec.Codec;
|
|
|
|
|
|
import org.redisson.codec.JsonJacksonCodec;
|
|
|
|
|
|
import org.redisson.config.Config;
|
|
|
|
|
|
import org.redisson.config.SingleServerConfig;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
|
2026-03-08 04:08:33 +08:00
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
2026-01-16 14:13:44 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @Author : YYJ
|
|
|
|
|
|
* @CreateTime : 2021/9/14
|
|
|
|
|
|
* @Description :
|
|
|
|
|
|
**/
|
|
|
|
|
|
@Configuration
|
|
|
|
|
|
public class RedissonConfig {
|
|
|
|
|
|
private static String MODE_SINGLE = "single";
|
|
|
|
|
|
private static String MODE_CLUSTER = "cluster";
|
|
|
|
|
|
@Value("${redis.mode}")
|
|
|
|
|
|
public String mode;
|
|
|
|
|
|
@Value("${redis.host}")
|
|
|
|
|
|
public String host;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.port}")
|
|
|
|
|
|
public String port;
|
|
|
|
|
|
|
|
|
|
|
|
// @Value("${redis.database}")
|
|
|
|
|
|
// public String database;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.password}")
|
|
|
|
|
|
public String password;
|
2026-03-08 04:08:33 +08:00
|
|
|
|
|
|
|
|
|
|
@Value("${redis.timeout:10000}")
|
|
|
|
|
|
public int timeout;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.maxIdle:100}")
|
|
|
|
|
|
public int maxIdle;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.maxActive:300}")
|
|
|
|
|
|
public int maxActive;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.testOnBorrow:true}")
|
|
|
|
|
|
public boolean testOnBorrow;
|
2026-01-16 14:13:44 +08:00
|
|
|
|
|
|
|
|
|
|
@Value("${cluster1.host.port:}")
|
|
|
|
|
|
public String cluster1;
|
|
|
|
|
|
@Value("${cluster2.host.port:}")
|
|
|
|
|
|
public String cluster2;
|
|
|
|
|
|
@Value("${cluster3.host.port:}")
|
|
|
|
|
|
public String cluster3;
|
|
|
|
|
|
@Value("${cluster4.host.port:}")
|
|
|
|
|
|
public String cluster4;
|
|
|
|
|
|
@Value("${cluster5.host.port:}")
|
|
|
|
|
|
public String cluster5;
|
|
|
|
|
|
@Value("${cluster6.host.port:}")
|
|
|
|
|
|
public String cluster6;
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
public RedissonClient redissonClient(){
|
|
|
|
|
|
RedissonClient redisson = null;
|
|
|
|
|
|
Config conf = new Config();
|
|
|
|
|
|
|
|
|
|
|
|
if(MODE_SINGLE.equals(mode)) {//单机
|
|
|
|
|
|
//单节点模式
|
|
|
|
|
|
SingleServerConfig singleServerConfig = conf.useSingleServer();
|
|
|
|
|
|
//设置连接地址:redis://127.0.0.1:6379
|
|
|
|
|
|
singleServerConfig.setAddress("redis://" + host + ":" + port);
|
|
|
|
|
|
//设置连接密码
|
|
|
|
|
|
singleServerConfig.setPassword(password);
|
2026-03-08 04:08:33 +08:00
|
|
|
|
// 设置超时时间(毫秒)
|
|
|
|
|
|
singleServerConfig.setTimeout(timeout);
|
|
|
|
|
|
// 设置连接池大小
|
|
|
|
|
|
singleServerConfig.setConnectionMinimumIdleSize(Math.min(10, maxIdle));
|
|
|
|
|
|
singleServerConfig.setConnectionPoolSize(maxActive);
|
|
|
|
|
|
// 设置空闲连接超时
|
|
|
|
|
|
singleServerConfig.setIdleConnectionTimeout(10000);
|
|
|
|
|
|
// 设置 ping 间隔,保持连接活跃
|
|
|
|
|
|
singleServerConfig.setPingConnectionInterval(10000);
|
|
|
|
|
|
// 设置失败重试次数
|
|
|
|
|
|
singleServerConfig.setRetryAttempts(3);
|
|
|
|
|
|
// 设置重试间隔
|
|
|
|
|
|
singleServerConfig.setRetryInterval(1500);
|
|
|
|
|
|
// 设置数据库索引
|
2026-01-16 14:13:44 +08:00
|
|
|
|
// singleServerConfig.setDatabase(Integer.valueOf(database));
|
|
|
|
|
|
}else if(MODE_CLUSTER.equals(mode)){//集群
|
|
|
|
|
|
conf.useClusterServers()
|
2026-03-08 04:08:33 +08:00
|
|
|
|
.setScanInterval(2000)
|
|
|
|
|
|
.setTimeout(timeout)
|
|
|
|
|
|
.setMasterConnectionMinimumIdleSize(Math.min(10, maxIdle))
|
|
|
|
|
|
.setMasterConnectionPoolSize(maxActive)
|
|
|
|
|
|
.setSlaveConnectionMinimumIdleSize(Math.min(10, maxIdle))
|
|
|
|
|
|
.setSlaveConnectionPoolSize(maxActive)
|
|
|
|
|
|
.setIdleConnectionTimeout(10000)
|
|
|
|
|
|
.setPingConnectionInterval(10000)
|
|
|
|
|
|
.setRetryAttempts(3)
|
|
|
|
|
|
.setRetryInterval(1500);
|
2026-01-16 14:13:44 +08:00
|
|
|
|
if(cluster1!=null && !cluster1.isEmpty()){
|
|
|
|
|
|
conf.useClusterServers().addNodeAddress("redis://" + cluster1);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(cluster2!=null && !cluster2.isEmpty()){
|
|
|
|
|
|
conf.useClusterServers().addNodeAddress("redis://" + cluster2);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(cluster3!=null && !cluster3.isEmpty()){
|
|
|
|
|
|
conf.useClusterServers().addNodeAddress("redis://" + cluster3);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(cluster4!=null && !cluster4.isEmpty()){
|
|
|
|
|
|
conf.useClusterServers().addNodeAddress("redis://" + cluster4);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(cluster5!=null && !cluster5.isEmpty()){
|
|
|
|
|
|
conf.useClusterServers().addNodeAddress("redis://" + cluster5);
|
|
|
|
|
|
}
|
|
|
|
|
|
if(cluster6!=null && !cluster6.isEmpty()){
|
|
|
|
|
|
conf.useClusterServers().addNodeAddress("redis://" + cluster6);
|
|
|
|
|
|
}
|
|
|
|
|
|
// .setPassword(password);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 04:08:33 +08:00
|
|
|
|
//使用 json 序列化方式
|
2026-01-16 14:13:44 +08:00
|
|
|
|
Codec codec = new JsonJacksonCodec();
|
|
|
|
|
|
conf.setCodec(codec);
|
|
|
|
|
|
redisson = Redisson.create(conf);
|
|
|
|
|
|
return redisson;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|