Merge pull request #2983 from anjia0532/starter-use-application

feat(starter): support spring config server/appolo/nacos
pull/3097/head
Nikita Koksharov 4 years ago committed by GitHub
commit 004b941981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -43,23 +43,62 @@ Downgrade `redisson-spring-data` module if necessary to support required Spring
Common spring boot settings or Redisson settings could be used.
```properties
```yaml
# common spring boot settings
spring.redis.database=
spring.redis.host=
spring.redis.port=
spring.redis.password=
spring.redis.ssl=
spring.redis.timeout=
spring.redis.cluster.nodes=
spring.redis.sentinel.master=
spring.redis.sentinel.nodes=
spring:
redis:
database:
host:
port:
password:
ssl:
timeout:
cluster:
nodes:
sentinel:
master:
nodes:
# Redisson settings
#path to config - redisson.yaml
redisson:
file: classpath:redisson.yaml
config: |
clusterServersConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
failedSlaveReconnectionInterval: 3000
failedSlaveCheckInterval: 60000
password: null
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 24
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 24
masterConnectionPoolSize: 64
readMode: "SLAVE"
subscriptionMode: "SLAVE"
nodeAddresses:
- "redis://127.0.0.1:7004"
- "redis://127.0.0.1:7001"
- "redis://127.0.0.1:7000"
scanInterval: 1000
pingConnectionInterval: 0
keepAlive: false
tcpNoDelay: false
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.FstCodec> {}
transportMode: "NIO"
# Redisson settings
#path to config - redisson.yaml
spring.redis.redisson.config=classpath:redisson.yaml
```
### 3. Use Redisson through spring bean with `RedissonClient` interface or `RedisTemplate`/`ReactiveRedisTemplate` objects

@ -45,9 +45,10 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ReflectionUtils;
/**
*
*
* @author Nikita Koksharov
* @author Nikos Kakavas (https://github.com/nikakis)
* @author AnJia (https://anjia0532.github.io/)
*
*/
@Configuration
@ -56,18 +57,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,7 +93,7 @@ public class RedissonAutoConfiguration {
public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
return new RedissonConnectionFactory(redisson);
}
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(RedissonClient.class)
public RedissonClient redisson() throws IOException {
@ -106,16 +110,26 @@ public class RedissonAutoConfiguration {
} else {
timeout = (Integer)timeoutValue;
}
if (redissonProperties.getConfig() != null) {
try {
config = Config.fromYAML(redissonProperties.getConfig());
} catch (IOException e) {
try {
config = Config.fromJSON(redissonProperties.getConfig());
} catch (IOException e1) {
throw new IllegalArgumentException("Can't parse config", e1);
}
}
} else if (redissonProperties.getFile() != 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,14 +137,14 @@ 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(",")));
} else {
nodes = convert((List<String>)nodesValue);
}
config = new Config();
config.useSentinelServers()
.setMasterName(redisProperties.getSentinel().getMaster())
@ -142,9 +156,9 @@ public class RedissonAutoConfiguration {
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)
@ -152,12 +166,12 @@ public class RedissonAutoConfiguration {
.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://";
prefix = REDISS_PROTOCOL_PREFIX;
}
config.useSingleServer()
.setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
.setConnectTimeout(timeout)
@ -175,8 +189,8 @@ 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);
}
@ -190,5 +204,5 @@ public class RedissonAutoConfiguration {
return is;
}
}

@ -18,8 +18,9 @@ package org.redisson.spring.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
*
*
* @author Nikita Koksharov
* @author AnJia (https://anjia0532.github.io/)
*
*/
@ConfigurationProperties(prefix = "spring.redis.redisson")
@ -27,6 +28,8 @@ public class RedissonProperties {
private String config;
private String file;
public String getConfig() {
return config;
}
@ -34,5 +37,12 @@ public class RedissonProperties {
public void setConfig(String config) {
this.config = config;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}

Loading…
Cancel
Save