|
|
@@ -7,10 +7,7 @@ package com.coffee.framework.config;
|
|
|
|
|
|
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.redisson.config.*;
|
|
|
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
|
|
|
import org.redisson.spring.starter.RedissonProperties;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -24,21 +21,16 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
-import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.RedisOperations;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.lang.reflect.Method;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Configuration
|
|
|
@ConditionalOnClass({Redisson.class, RedisOperations.class})
|
|
|
@@ -51,8 +43,7 @@ public class RedissonClientAutoConfiguration {
|
|
|
required = false
|
|
|
)
|
|
|
private List<RedissonAutoConfigurationCustomizer> redissonAutoConfigurationCustomizers;
|
|
|
- @Autowired
|
|
|
- private RedissonProperties redissonProperties;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RedisProperties redisProperties;
|
|
|
@Autowired
|
|
|
@@ -65,110 +56,38 @@ public class RedissonClientAutoConfiguration {
|
|
|
destroyMethod = "shutdown"
|
|
|
)
|
|
|
@ConditionalOnMissingBean({RedissonClient.class})
|
|
|
- public RedissonClient redisson() throws IOException {
|
|
|
- Config config = null;
|
|
|
- Method clusterMethod = ReflectionUtils.findMethod(RedisProperties.class, "getCluster");
|
|
|
- Method timeoutMethod = ReflectionUtils.findMethod(RedisProperties.class, "getTimeout");
|
|
|
- Object timeoutValue = ReflectionUtils.invokeMethod(timeoutMethod, this.redisProperties);
|
|
|
- int timeout;
|
|
|
- Method nodesMethod;
|
|
|
- if (null == timeoutValue) {
|
|
|
- timeout = 10000;
|
|
|
- } else if (!(timeoutValue instanceof Integer)) {
|
|
|
- nodesMethod = ReflectionUtils.findMethod(timeoutValue.getClass(), "toMillis");
|
|
|
- timeout = ((Long) ReflectionUtils.invokeMethod(nodesMethod, timeoutValue)).intValue();
|
|
|
- } else {
|
|
|
- timeout = (Integer)timeoutValue;
|
|
|
- }
|
|
|
-
|
|
|
- if (this.redissonProperties.getConfig() != null) {
|
|
|
- try {
|
|
|
- config = Config.fromYAML(this.redissonProperties.getConfig());
|
|
|
- } catch (IOException var13) {
|
|
|
- try {
|
|
|
- config = Config.fromJSON(this.redissonProperties.getConfig());
|
|
|
- } catch (IOException var12) {
|
|
|
- throw new IllegalArgumentException("Can't parse config", var12);
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (this.redissonProperties.getFile() != null) {
|
|
|
- try {
|
|
|
- InputStream is = this.getConfigStream();
|
|
|
- config = Config.fromYAML(is);
|
|
|
- } catch (IOException var11) {
|
|
|
- try {
|
|
|
- InputStream is = this.getConfigStream();
|
|
|
- config = Config.fromJSON(is);
|
|
|
- } catch (IOException var10) {
|
|
|
- throw new IllegalArgumentException("Can't parse config", var10);
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (this.redisProperties.getSentinel() != null) {
|
|
|
- nodesMethod = ReflectionUtils.findMethod(Sentinel.class, "getNodes");
|
|
|
- Object nodesValue = ReflectionUtils.invokeMethod(nodesMethod, this.redisProperties.getSentinel());
|
|
|
- String[] nodes;
|
|
|
- if (nodesValue instanceof String) {
|
|
|
- nodes = this.convert(Arrays.asList(((String)nodesValue).split(",")));
|
|
|
- } else {
|
|
|
- nodes = this.convert((List)nodesValue);
|
|
|
- }
|
|
|
-
|
|
|
- config = new Config();
|
|
|
- ((SentinelServersConfig)config.useSentinelServers().setMasterName(this.redisProperties.getSentinel().getMaster()).addSentinelAddress(nodes).setDatabase(this.redisProperties.getDatabase()).setConnectTimeout(timeout)).setPassword(this.redisProperties.getPassword());
|
|
|
- } else {
|
|
|
- Method method;
|
|
|
- if (clusterMethod != null && ReflectionUtils.invokeMethod(clusterMethod, this.redisProperties) != null) {
|
|
|
- Object clusterObject = ReflectionUtils.invokeMethod(clusterMethod, this.redisProperties);
|
|
|
- method = ReflectionUtils.findMethod(clusterObject.getClass(), "getNodes");
|
|
|
- List<String> nodesObject = (List) ReflectionUtils.invokeMethod(method, clusterObject);
|
|
|
- String[] nodes = this.convert(nodesObject);
|
|
|
- config = new Config();
|
|
|
- ((ClusterServersConfig)config.useClusterServers().addNodeAddress(nodes).setConnectTimeout(timeout)).setPassword(this.redisProperties.getPassword());
|
|
|
- } else {
|
|
|
- config = new Config();
|
|
|
- String prefix = "redis://";
|
|
|
- method = ReflectionUtils.findMethod(RedisProperties.class, "isSsl");
|
|
|
- if (method != null && (Boolean) ReflectionUtils.invokeMethod(method, this.redisProperties)) {
|
|
|
- prefix = "rediss://";
|
|
|
- }
|
|
|
-
|
|
|
- ((SingleServerConfig)config.useSingleServer().setAddress(prefix + this.redisProperties.getHost() + ":" + this.redisProperties.getPort()).setConnectTimeout(timeout)).setDatabase(this.redisProperties.getDatabase()).setPassword(this.redisProperties.getPassword());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (this.redissonAutoConfigurationCustomizers != null) {
|
|
|
- Iterator var19 = this.redissonAutoConfigurationCustomizers.iterator();
|
|
|
-
|
|
|
- while(var19.hasNext()) {
|
|
|
- RedissonAutoConfigurationCustomizer customizer = (RedissonAutoConfigurationCustomizer)var19.next();
|
|
|
- customizer.customize(config);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ public RedissonClient redisson() {
|
|
|
+ Config config = new Config();
|
|
|
+ SingleServerConfig singleServerConfig= config.useSingleServer();
|
|
|
+ singleServerConfig.setDatabase(redisProperties.getDatabase());
|
|
|
+ singleServerConfig.setPassword(redisProperties.getPassword());
|
|
|
+ singleServerConfig.setAddress("redis://"+redisProperties.getHost()+":"+redisProperties.getPort());
|
|
|
+ singleServerConfig.setPingConnectionInterval(0);
|
|
|
+ // 如果当前连接池里的连接数量超过了最小空闲连接数,而同时有连接空闲时间超过了该数值,那么这些连接将会自动被关闭,并从连接池里去掉。时间单位是毫秒。默认10000
|
|
|
+ singleServerConfig.setIdleConnectionTimeout(30000);
|
|
|
+ //建立连接时的等待超时。时间单位是毫秒。默认10000
|
|
|
+ singleServerConfig.setConnectTimeout(Long.valueOf(Optional.ofNullable(redisProperties.getConnectTimeout()).orElse(Duration.ofSeconds(3)).toMillis()).intValue());
|
|
|
+ //等待节点回复命令的时间。该时间从命令发送成功时开始计时。默认3000
|
|
|
+ singleServerConfig.setTimeout(Long.valueOf(Optional.ofNullable(redisProperties.getTimeout()).orElse(Duration.ofSeconds(3)).toMillis()).intValue());
|
|
|
+ //命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误,如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
|
|
|
+ singleServerConfig.setRetryAttempts(3);
|
|
|
+ //在一条命令发送失败以后,等待重试发送的时间间隔。时间单位是毫秒。默认1500
|
|
|
+ singleServerConfig.setRetryInterval(1500);
|
|
|
+ //每个连接的最大订阅数量。默认5
|
|
|
+ singleServerConfig.setSubscriptionsPerConnection(5000);
|
|
|
+ //在Redis节点里显示的客户端名称。默认null
|
|
|
+ singleServerConfig.setClientName("nb");
|
|
|
+ //用于发布和订阅连接的最小保持连接数(长连接)。Redisson内部经常通过发布和订阅来实现许多功能,长期保持一定数量的发布订阅连接是必须的。默认1
|
|
|
+ singleServerConfig.setSubscriptionConnectionMinimumIdleSize(1);
|
|
|
+ //多从节点的环境里,每个从服务节点里用于发布和订阅连接的连接池最大容量。连接池的连接数量自动弹性伸缩。默认50
|
|
|
+ singleServerConfig.setSubscriptionConnectionPoolSize(500);
|
|
|
+ //是否保持连接
|
|
|
+ singleServerConfig.setKeepAlive(false);
|
|
|
+ //这个线程池数量被所有RTopic对象监听器,RRemoteService调用者和RExecutorService任务共同共享。默认当前处理核数量 * 2
|
|
|
+ config.setThreads(Runtime.getRuntime().availableProcessors()*2);
|
|
|
+ // 这个线程池数量是在一个Redisson实例内,被其创建的所有分布式数据类型和服务,以及底层客户端所一同共享的线程池里保存的线程数量。
|
|
|
+ config.setThreads(Runtime.getRuntime().availableProcessors()*2);
|
|
|
+ config.setTransportMode(TransportMode.NIO);
|
|
|
return Redisson.create(config);
|
|
|
}
|
|
|
-
|
|
|
- private String[] convert(List<String> nodesObject) {
|
|
|
- List<String> nodes = new ArrayList(nodesObject.size());
|
|
|
- Iterator var3 = nodesObject.iterator();
|
|
|
-
|
|
|
- while(true) {
|
|
|
- while(var3.hasNext()) {
|
|
|
- String node = (String)var3.next();
|
|
|
- if (!node.startsWith("redis://") && !node.startsWith("rediss://")) {
|
|
|
- nodes.add("redis://" + node);
|
|
|
- } else {
|
|
|
- nodes.add(node);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return (String[])nodes.toArray(new String[nodes.size()]);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private InputStream getConfigStream() throws IOException {
|
|
|
- Resource resource = this.ctx.getResource(this.redissonProperties.getFile());
|
|
|
- InputStream is = resource.getInputStream();
|
|
|
- return is;
|
|
|
- }
|
|
|
}
|