|
|
|
@ -1,12 +1,12 @@
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2013-2020 Nikita Koksharov
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
* <p>
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
@ -15,13 +15,6 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.redisson.spring.starter;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import org.redisson.Redisson;
|
|
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
|
|
import org.redisson.config.Config;
|
|
|
|
@ -44,11 +37,16 @@ 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.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Nikita Koksharov
|
|
|
|
|
* @author Nikos Kakavas (https://github.com/nikakis)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
@ConditionalOnClass({Redisson.class, RedisOperations.class})
|
|
|
|
@ -56,18 +54,21 @@ import org.springframework.util.ReflectionUtils;
|
|
|
|
|
@EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class})
|
|
|
|
|
public class RedissonAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
private static final String REDIS_PROTOCOL_PREFIX = "redis://";
|
|
|
|
|
private static final String REDISS_PROTOCOL_PREFIX = "rediss://";
|
|
|
|
|
|
|
|
|
|
@Autowired(required = false)
|
|
|
|
|
private List<RedissonAutoConfigurationCustomizer> redissonAutoConfigurationCustomizers;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedissonProperties redissonProperties;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisProperties redisProperties;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ApplicationContext ctx;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean(name = "redisTemplate")
|
|
|
|
|
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
|
|
|
@ -89,33 +90,42 @@ public class RedissonAutoConfiguration {
|
|
|
|
|
public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
|
|
|
|
|
return new RedissonConnectionFactory(redisson);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean(destroyMethod = "shutdown")
|
|
|
|
|
@ConditionalOnMissingBean(RedissonClient.class)
|
|
|
|
|
public RedissonClient redisson() throws IOException {
|
|
|
|
|
Config config = null;
|
|
|
|
|
Config config;
|
|
|
|
|
Method clusterMethod = ReflectionUtils.findMethod(RedisProperties.class, "getCluster");
|
|
|
|
|
Method timeoutMethod = ReflectionUtils.findMethod(RedisProperties.class, "getTimeout");
|
|
|
|
|
Object timeoutValue = ReflectionUtils.invokeMethod(timeoutMethod, redisProperties);
|
|
|
|
|
int timeout;
|
|
|
|
|
if(null == timeoutValue){
|
|
|
|
|
if (null == timeoutValue) {
|
|
|
|
|
timeout = 10000;
|
|
|
|
|
}else if (!(timeoutValue instanceof Integer)) {
|
|
|
|
|
} else if (!(timeoutValue instanceof Integer)) {
|
|
|
|
|
Method millisMethod = ReflectionUtils.findMethod(timeoutValue.getClass(), "toMillis");
|
|
|
|
|
timeout = ((Long) ReflectionUtils.invokeMethod(millisMethod, timeoutValue)).intValue();
|
|
|
|
|
} else {
|
|
|
|
|
timeout = (Integer)timeoutValue;
|
|
|
|
|
timeout = (Integer) timeoutValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (redissonProperties.getConfig() != null) {
|
|
|
|
|
if (redissonProperties.getConfigBlock() != null) {
|
|
|
|
|
try {
|
|
|
|
|
config = Config.fromYAML(redissonProperties.getConfigBlock());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
try {
|
|
|
|
|
config = Config.fromJSON(redissonProperties.getConfigBlock());
|
|
|
|
|
} catch (IOException e1) {
|
|
|
|
|
throw new IllegalArgumentException("Can't parse config", e1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (redissonProperties.getConfig() != null) {
|
|
|
|
|
try {
|
|
|
|
|
InputStream is = getConfigStream();
|
|
|
|
|
config = Config.fromJSON(is);
|
|
|
|
|
config = Config.fromYAML(is);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// trying next format
|
|
|
|
|
try {
|
|
|
|
|
InputStream is = getConfigStream();
|
|
|
|
|
config = Config.fromYAML(is);
|
|
|
|
|
config = Config.fromJSON(is);
|
|
|
|
|
} catch (IOException e1) {
|
|
|
|
|
throw new IllegalArgumentException("Can't parse config", e1);
|
|
|
|
|
}
|
|
|
|
@ -123,46 +133,46 @@ public class RedissonAutoConfiguration {
|
|
|
|
|
} else if (redisProperties.getSentinel() != null) {
|
|
|
|
|
Method nodesMethod = ReflectionUtils.findMethod(Sentinel.class, "getNodes");
|
|
|
|
|
Object nodesValue = ReflectionUtils.invokeMethod(nodesMethod, redisProperties.getSentinel());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String[] nodes;
|
|
|
|
|
if (nodesValue instanceof String) {
|
|
|
|
|
nodes = convert(Arrays.asList(((String)nodesValue).split(",")));
|
|
|
|
|
nodes = convert(Arrays.asList(((String) nodesValue).split(",")));
|
|
|
|
|
} else {
|
|
|
|
|
nodes = convert((List<String>)nodesValue);
|
|
|
|
|
nodes = convert((List<String>) nodesValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config = new Config();
|
|
|
|
|
config.useSentinelServers()
|
|
|
|
|
.setMasterName(redisProperties.getSentinel().getMaster())
|
|
|
|
|
.addSentinelAddress(nodes)
|
|
|
|
|
.setDatabase(redisProperties.getDatabase())
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.setPassword(redisProperties.getPassword());
|
|
|
|
|
.setMasterName(redisProperties.getSentinel().getMaster())
|
|
|
|
|
.addSentinelAddress(nodes)
|
|
|
|
|
.setDatabase(redisProperties.getDatabase())
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.setPassword(redisProperties.getPassword());
|
|
|
|
|
} else if (clusterMethod != null && ReflectionUtils.invokeMethod(clusterMethod, redisProperties) != null) {
|
|
|
|
|
Object clusterObject = ReflectionUtils.invokeMethod(clusterMethod, redisProperties);
|
|
|
|
|
Method nodesMethod = ReflectionUtils.findMethod(clusterObject.getClass(), "getNodes");
|
|
|
|
|
List<String> nodesObject = (List) ReflectionUtils.invokeMethod(nodesMethod, clusterObject);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String[] nodes = convert(nodesObject);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config = new Config();
|
|
|
|
|
config.useClusterServers()
|
|
|
|
|
.addNodeAddress(nodes)
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.setPassword(redisProperties.getPassword());
|
|
|
|
|
.addNodeAddress(nodes)
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.setPassword(redisProperties.getPassword());
|
|
|
|
|
} else {
|
|
|
|
|
config = new Config();
|
|
|
|
|
String prefix = "redis://";
|
|
|
|
|
String prefix = REDIS_PROTOCOL_PREFIX;
|
|
|
|
|
Method method = ReflectionUtils.findMethod(RedisProperties.class, "isSsl");
|
|
|
|
|
if (method != null && (Boolean)ReflectionUtils.invokeMethod(method, redisProperties)) {
|
|
|
|
|
prefix = "rediss://";
|
|
|
|
|
if (method != null && (Boolean) ReflectionUtils.invokeMethod(method, redisProperties)) {
|
|
|
|
|
prefix = REDISS_PROTOCOL_PREFIX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.useSingleServer()
|
|
|
|
|
.setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.setDatabase(redisProperties.getDatabase())
|
|
|
|
|
.setPassword(redisProperties.getPassword());
|
|
|
|
|
.setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
|
|
|
|
|
.setConnectTimeout(timeout)
|
|
|
|
|
.setDatabase(redisProperties.getDatabase())
|
|
|
|
|
.setPassword(redisProperties.getPassword());
|
|
|
|
|
}
|
|
|
|
|
if (redissonAutoConfigurationCustomizers != null) {
|
|
|
|
|
for (RedissonAutoConfigurationCustomizer customizer : redissonAutoConfigurationCustomizers) {
|
|
|
|
@ -175,20 +185,19 @@ public class RedissonAutoConfiguration {
|
|
|
|
|
private String[] convert(List<String> nodesObject) {
|
|
|
|
|
List<String> nodes = new ArrayList<String>(nodesObject.size());
|
|
|
|
|
for (String node : nodesObject) {
|
|
|
|
|
if (!node.startsWith("redis://") && !node.startsWith("rediss://")) {
|
|
|
|
|
nodes.add("redis://" + node);
|
|
|
|
|
if (!node.startsWith(REDIS_PROTOCOL_PREFIX) && !node.startsWith(REDISS_PROTOCOL_PREFIX)) {
|
|
|
|
|
nodes.add(REDIS_PROTOCOL_PREFIX + node);
|
|
|
|
|
} else {
|
|
|
|
|
nodes.add(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nodes.toArray(new String[nodes.size()]);
|
|
|
|
|
return nodes.toArray(new String[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private InputStream getConfigStream() throws IOException {
|
|
|
|
|
Resource resource = ctx.getResource(redissonProperties.getConfig());
|
|
|
|
|
InputStream is = resource.getInputStream();
|
|
|
|
|
return is;
|
|
|
|
|
return resource.getInputStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|