mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-04-09 01:37:34 +00:00
更新 redis 方法 支持群集
This commit is contained in:
@@ -2,6 +2,8 @@ package cn.keking.config;
|
|||||||
|
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.redisson.Redisson;
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
import org.redisson.client.codec.Codec;
|
import org.redisson.client.codec.Codec;
|
||||||
import org.redisson.config.Config;
|
import org.redisson.config.Config;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||||
@@ -11,42 +13,123 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Redisson 客户端配置
|
||||||
* Created by kl on 2017/09/26.
|
* Created by kl on 2017/09/26.
|
||||||
* redisson 客户端配置
|
|
||||||
*/
|
*/
|
||||||
@ConditionalOnExpression("'${cache.type:default}'.equals('redis')")
|
@ConditionalOnExpression("'${cache.type:default}'.equals('redis')")
|
||||||
@ConfigurationProperties(prefix = "spring.redisson")
|
@ConfigurationProperties(prefix = "spring.redisson")
|
||||||
@Configuration
|
@Configuration
|
||||||
public class RedissonConfig {
|
public class RedissonConfig {
|
||||||
|
|
||||||
private String address;
|
// ========================== 连接配置 ==========================
|
||||||
private int connectionMinimumIdleSize = 10;
|
private static String address;
|
||||||
private int idleConnectionTimeout=10000;
|
private static String password;
|
||||||
private int pingTimeout=1000;
|
private static String clientName;
|
||||||
private int connectTimeout=10000;
|
private static int database = 0;
|
||||||
private int timeout=3000;
|
private static String mode = "single";
|
||||||
private int retryAttempts=3;
|
private static String masterName = "kkfile";
|
||||||
private int retryInterval=1500;
|
|
||||||
private int reconnectionTimeout=3000;
|
|
||||||
private int failedAttempts=3;
|
|
||||||
private String password = null;
|
|
||||||
private int subscriptionsPerConnection=5;
|
|
||||||
private String clientName=null;
|
|
||||||
private int subscriptionConnectionMinimumIdleSize = 1;
|
|
||||||
private int subscriptionConnectionPoolSize = 50;
|
|
||||||
private int connectionPoolSize = 64;
|
|
||||||
private int database = 0;
|
|
||||||
private boolean dnsMonitoring = false;
|
|
||||||
private int dnsMonitoringInterval = 5000;
|
|
||||||
|
|
||||||
private int thread; //当前处理核数量 * 2
|
// ========================== 超时配置 ==========================
|
||||||
|
private static int idleConnectionTimeout = 10000;
|
||||||
|
private static int connectTimeout = 10000;
|
||||||
|
private static int timeout = 3000;
|
||||||
|
|
||||||
private String codec="org.redisson.codec.JsonJacksonCodec";
|
// ========================== 重试配置 ==========================
|
||||||
|
private static int retryAttempts = 3;
|
||||||
|
private static int retryInterval = 1500;
|
||||||
|
|
||||||
|
// ========================== 连接池配置 ==========================
|
||||||
|
private static int connectionMinimumIdleSize = 10;
|
||||||
|
private static int connectionPoolSize = 64;
|
||||||
|
private static int subscriptionsPerConnection = 5;
|
||||||
|
private static int subscriptionConnectionMinimumIdleSize = 1;
|
||||||
|
private static int subscriptionConnectionPoolSize = 50;
|
||||||
|
|
||||||
|
// ========================== 其他配置 ==========================
|
||||||
|
private static int dnsMonitoringInterval = 5000;
|
||||||
|
private static int thread; // 当前处理核数量 * 2
|
||||||
|
private static String codec = "org.redisson.codec.JsonJacksonCodec";
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
Config config() throws Exception {
|
public static RedissonClient config() throws Exception {
|
||||||
Config config = new Config();
|
Config config = new Config();
|
||||||
config.useSingleServer().setAddress(address)
|
|
||||||
|
// 密码处理
|
||||||
|
if (StringUtils.isBlank(password)) {
|
||||||
|
password = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据模式创建对应的 Redisson 配置
|
||||||
|
switch (mode) {
|
||||||
|
case "cluster":
|
||||||
|
configureClusterMode(config);
|
||||||
|
break;
|
||||||
|
case "master-slave":
|
||||||
|
configureMasterSlaveMode(config);
|
||||||
|
break;
|
||||||
|
case "sentinel":
|
||||||
|
configureSentinelMode(config);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
configureSingleMode(config);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Redisson.create(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========================== 配置方法 ==========================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置集群模式
|
||||||
|
*/
|
||||||
|
private static void configureClusterMode(Config config) {
|
||||||
|
String[] clusterAddresses = address.split(",");
|
||||||
|
config.useClusterServers()
|
||||||
|
.setScanInterval(2000)
|
||||||
|
.addNodeAddress(clusterAddresses)
|
||||||
|
.setPassword(password)
|
||||||
|
.setRetryAttempts(retryAttempts)
|
||||||
|
.setTimeout(timeout)
|
||||||
|
.setMasterConnectionPoolSize(100)
|
||||||
|
.setSlaveConnectionPoolSize(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置主从模式
|
||||||
|
*/
|
||||||
|
private static void configureMasterSlaveMode(Config config) {
|
||||||
|
String[] masterSlaveAddresses = address.split(",");
|
||||||
|
validateMasterSlaveAddresses(masterSlaveAddresses);
|
||||||
|
|
||||||
|
String[] slaveAddresses = new String[masterSlaveAddresses.length - 1];
|
||||||
|
System.arraycopy(masterSlaveAddresses, 1, slaveAddresses, 0, slaveAddresses.length);
|
||||||
|
|
||||||
|
config.useMasterSlaveServers()
|
||||||
|
.setDatabase(database)
|
||||||
|
.setPassword(password)
|
||||||
|
.setMasterAddress(masterSlaveAddresses[0])
|
||||||
|
.addSlaveAddress(slaveAddresses);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置哨兵模式
|
||||||
|
*/
|
||||||
|
private static void configureSentinelMode(Config config) {
|
||||||
|
String[] sentinelAddresses = address.split(",");
|
||||||
|
config.useSentinelServers()
|
||||||
|
.setDatabase(database)
|
||||||
|
.setPassword(password)
|
||||||
|
.setMasterName(masterName)
|
||||||
|
.addSentinelAddress(sentinelAddresses);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置单机模式
|
||||||
|
*/
|
||||||
|
private static void configureSingleMode(Config config) throws Exception {
|
||||||
|
config.useSingleServer()
|
||||||
|
.setAddress(address)
|
||||||
.setConnectionMinimumIdleSize(connectionMinimumIdleSize)
|
.setConnectionMinimumIdleSize(connectionMinimumIdleSize)
|
||||||
.setConnectionPoolSize(connectionPoolSize)
|
.setConnectionPoolSize(connectionPoolSize)
|
||||||
.setDatabase(database)
|
.setDatabase(database)
|
||||||
@@ -61,91 +144,35 @@ public class RedissonConfig {
|
|||||||
.setConnectTimeout(connectTimeout)
|
.setConnectTimeout(connectTimeout)
|
||||||
.setIdleConnectionTimeout(idleConnectionTimeout)
|
.setIdleConnectionTimeout(idleConnectionTimeout)
|
||||||
.setPassword(StringUtils.trimToNull(password));
|
.setPassword(StringUtils.trimToNull(password));
|
||||||
Codec codec=(Codec) ClassUtils.forName(getCodec(), ClassUtils.getDefaultClassLoader()).newInstance();
|
|
||||||
config.setCodec(codec);
|
// 设置编码器
|
||||||
|
Class<?> codecClass = ClassUtils.forName(getCodec(), ClassUtils.getDefaultClassLoader());
|
||||||
|
Codec codecInstance = (Codec) codecClass.getDeclaredConstructor().newInstance();
|
||||||
|
config.setCodec(codecInstance);
|
||||||
|
// 设置线程和事件循环组
|
||||||
config.setThreads(thread);
|
config.setThreads(thread);
|
||||||
config.setEventLoopGroup(new NioEventLoopGroup());
|
config.setEventLoopGroup(new NioEventLoopGroup());
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getThread() {
|
/**
|
||||||
return thread;
|
* 验证主从模式地址
|
||||||
|
*/
|
||||||
|
private static void validateMasterSlaveAddresses(String[] addresses) {
|
||||||
|
if (addresses.length == 1) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"redis.redisson.address MUST have multiple redis addresses for master-slave mode.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setThread(int thread) {
|
// ========================== Getter和Setter方法 ==========================
|
||||||
this.thread = thread;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 连接配置
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAddress(String address) {
|
public void setAddress(String address) {
|
||||||
this.address = address;
|
RedissonConfig.address = address;
|
||||||
}
|
|
||||||
|
|
||||||
public int getIdleConnectionTimeout() {
|
|
||||||
return idleConnectionTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIdleConnectionTimeout(int idleConnectionTimeout) {
|
|
||||||
this.idleConnectionTimeout = idleConnectionTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPingTimeout() {
|
|
||||||
return pingTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPingTimeout(int pingTimeout) {
|
|
||||||
this.pingTimeout = pingTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getConnectTimeout() {
|
|
||||||
return connectTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConnectTimeout(int connectTimeout) {
|
|
||||||
this.connectTimeout = connectTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTimeout() {
|
|
||||||
return timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTimeout(int timeout) {
|
|
||||||
this.timeout = timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRetryAttempts() {
|
|
||||||
return retryAttempts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRetryAttempts(int retryAttempts) {
|
|
||||||
this.retryAttempts = retryAttempts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRetryInterval() {
|
|
||||||
return retryInterval;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRetryInterval(int retryInterval) {
|
|
||||||
this.retryInterval = retryInterval;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getReconnectionTimeout() {
|
|
||||||
return reconnectionTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setReconnectionTimeout(int reconnectionTimeout) {
|
|
||||||
this.reconnectionTimeout = reconnectionTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getFailedAttempts() {
|
|
||||||
return failedAttempts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFailedAttempts(int failedAttempts) {
|
|
||||||
this.failedAttempts = failedAttempts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
@@ -153,15 +180,7 @@ public class RedissonConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
RedissonConfig.password = password;
|
||||||
}
|
|
||||||
|
|
||||||
public int getSubscriptionsPerConnection() {
|
|
||||||
return subscriptionsPerConnection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubscriptionsPerConnection(int subscriptionsPerConnection) {
|
|
||||||
this.subscriptionsPerConnection = subscriptionsPerConnection;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClientName() {
|
public String getClientName() {
|
||||||
@@ -169,39 +188,7 @@ public class RedissonConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setClientName(String clientName) {
|
public void setClientName(String clientName) {
|
||||||
this.clientName = clientName;
|
RedissonConfig.clientName = clientName;
|
||||||
}
|
|
||||||
|
|
||||||
public int getSubscriptionConnectionMinimumIdleSize() {
|
|
||||||
return subscriptionConnectionMinimumIdleSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubscriptionConnectionMinimumIdleSize(int subscriptionConnectionMinimumIdleSize) {
|
|
||||||
this.subscriptionConnectionMinimumIdleSize = subscriptionConnectionMinimumIdleSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSubscriptionConnectionPoolSize() {
|
|
||||||
return subscriptionConnectionPoolSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubscriptionConnectionPoolSize(int subscriptionConnectionPoolSize) {
|
|
||||||
this.subscriptionConnectionPoolSize = subscriptionConnectionPoolSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getConnectionMinimumIdleSize() {
|
|
||||||
return connectionMinimumIdleSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConnectionMinimumIdleSize(int connectionMinimumIdleSize) {
|
|
||||||
this.connectionMinimumIdleSize = connectionMinimumIdleSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getConnectionPoolSize() {
|
|
||||||
return connectionPoolSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConnectionPoolSize(int connectionPoolSize) {
|
|
||||||
this.connectionPoolSize = connectionPoolSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDatabase() {
|
public int getDatabase() {
|
||||||
@@ -209,30 +196,130 @@ public class RedissonConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setDatabase(int database) {
|
public void setDatabase(int database) {
|
||||||
this.database = database;
|
RedissonConfig.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDnsMonitoring() {
|
public static String getMode() {
|
||||||
return dnsMonitoring;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDnsMonitoring(boolean dnsMonitoring) {
|
public void setMode(String mode) {
|
||||||
this.dnsMonitoring = dnsMonitoring;
|
RedissonConfig.mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getMasterNamee() {
|
||||||
|
return masterName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMasterNamee(String masterName) {
|
||||||
|
RedissonConfig.masterName = masterName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 超时配置
|
||||||
|
public int getIdleConnectionTimeout() {
|
||||||
|
return idleConnectionTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleConnectionTimeout(int idleConnectionTimeout) {
|
||||||
|
RedissonConfig.idleConnectionTimeout = idleConnectionTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getConnectTimeout() {
|
||||||
|
return connectTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectTimeout(int connectTimeout) {
|
||||||
|
RedissonConfig.connectTimeout = connectTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
RedissonConfig.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试配置
|
||||||
|
public int getRetryAttempts() {
|
||||||
|
return retryAttempts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRetryAttempts(int retryAttempts) {
|
||||||
|
RedissonConfig.retryAttempts = retryAttempts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRetryInterval() {
|
||||||
|
return retryInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRetryInterval(int retryInterval) {
|
||||||
|
RedissonConfig.retryInterval = retryInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接池配置
|
||||||
|
public int getConnectionMinimumIdleSize() {
|
||||||
|
return connectionMinimumIdleSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectionMinimumIdleSize(int connectionMinimumIdleSize) {
|
||||||
|
RedissonConfig.connectionMinimumIdleSize = connectionMinimumIdleSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getConnectionPoolSize() {
|
||||||
|
return connectionPoolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectionPoolSize(int connectionPoolSize) {
|
||||||
|
RedissonConfig.connectionPoolSize = connectionPoolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSubscriptionsPerConnection() {
|
||||||
|
return subscriptionsPerConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptionsPerConnection(int subscriptionsPerConnection) {
|
||||||
|
RedissonConfig.subscriptionsPerConnection = subscriptionsPerConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSubscriptionConnectionMinimumIdleSize() {
|
||||||
|
return subscriptionConnectionMinimumIdleSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptionConnectionMinimumIdleSize(int subscriptionConnectionMinimumIdleSize) {
|
||||||
|
RedissonConfig.subscriptionConnectionMinimumIdleSize = subscriptionConnectionMinimumIdleSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSubscriptionConnectionPoolSize() {
|
||||||
|
return subscriptionConnectionPoolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptionConnectionPoolSize(int subscriptionConnectionPoolSize) {
|
||||||
|
RedissonConfig.subscriptionConnectionPoolSize = subscriptionConnectionPoolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他配置
|
||||||
public int getDnsMonitoringInterval() {
|
public int getDnsMonitoringInterval() {
|
||||||
return dnsMonitoringInterval;
|
return dnsMonitoringInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDnsMonitoringInterval(int dnsMonitoringInterval) {
|
public void setDnsMonitoringInterval(int dnsMonitoringInterval) {
|
||||||
this.dnsMonitoringInterval = dnsMonitoringInterval;
|
RedissonConfig.dnsMonitoringInterval = dnsMonitoringInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCodec() {
|
public int getThread() {
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThread(int thread) {
|
||||||
|
RedissonConfig.thread = thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCodec() {
|
||||||
return codec;
|
return codec;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCodec(String codec) {
|
public void setCodec(String codec) {
|
||||||
this.codec = codec;
|
RedissonConfig.codec = codec;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user