Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ with
spring.redis.host: value
```

For configuring single redis node, please use next properties:

```yaml
spring:
redis:
host: host
timeout: value
port: value
```



It is possible to override the default YAML configuration by supplying a custom configuration. See example scenario(s) below.

Expand All @@ -98,9 +109,14 @@ It is possible to override the default YAML configuration by supplying a custom
Redis cluster settings
_application-default.yml:_
```yaml
spring.redis.host: redis_host_1:port,redis_host_2:port,redis_host_3:port
spring.redis.is_cluster: true
spring.redis.timeout: 300
spring:
redis:
cluster:
nodes:
- host_1:port_1
- host_2:port_2
- host_3:port_3
timeout: 300
```
Aerospike cluster settings
_application-default.yml:_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import lombok.Singular;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -26,27 +25,26 @@
@NoArgsConstructor
@AllArgsConstructor
@Configuration
@ConditionalOnProperty(prefix = "spring.redis", name = {"host"})
@ConditionalOnProperty(prefix = "spring.redis", name = {"timeout"})
@ConfigurationProperties(prefix = "spring.redis")
public class RedisPropertyConfiguration {

private String host;
private long timeout;
private String password;
private Boolean isCluster;
private int port;
private Cluster cluster;

private RedisURI createRedisURI(String hosts) {
requireNonNull(hosts);
String[] hostParams = hosts.split(":");
@Data
public static class Cluster {

if (hostParams.length < 2) {
throw new IllegalArgumentException("Illegal host URL format, host and port should be specified "
+ "as host:port");
}
String hostname = requireNonNull(hostParams[0]);
int port = Integer.parseInt(hostParams[1]);
@Singular
List<String> nodes;
}

final RedisURI.Builder builder = RedisURI.Builder.redis(hostname, port)
private RedisURI createRedisURI(String host, int port) {
requireNonNull(host);
final RedisURI.Builder builder = RedisURI.Builder.redis(host, port)
.withTimeout(Duration.ofMillis(timeout));
if (password != null) {
builder.withPassword(password);
Expand All @@ -56,39 +54,40 @@ private RedisURI createRedisURI(String hosts) {
}

private List<RedisURI> createRedisClusterURIs() {
requireNonNull(host);
return Arrays.stream(host.split(","))
.map(this::createRedisURI)

return cluster.getNodes().stream()
.map(node -> node.split(":"))
.map(host -> createRedisURI(host[0], Integer.parseInt(host[1])))
.collect(Collectors.toList());
}

@Bean(destroyMethod = "shutdown")
@ConditionalOnExpression("'${spring.redis.is_cluster}' == 'false'")
@ConditionalOnProperty(prefix = "spring.redis", name = "host")
RedisClient client() {
return RedisClient.create(createRedisURI(getHost()));
return RedisClient.create(createRedisURI(getHost(), getPort()));
}

@Bean(destroyMethod = "close")
@ConditionalOnExpression("'${spring.redis.is_cluster}' == 'false'")
@ConditionalOnProperty(prefix = "spring.redis", name = "host")
StatefulRedisConnection<String, String> connection() {
return client().connect();
}

@Bean(destroyMethod = "shutdown")
@ConditionalOnExpression("'${spring.redis.is_cluster}' == 'true'")
@ConditionalOnProperty(prefix = "spring.redis", name = "host", matchIfMissing = true, havingValue = "null")
RedisClusterClient clusterClient() {
return RedisClusterClient.create(createRedisClusterURIs());
}

@Bean(destroyMethod = "close")
@ConditionalOnExpression("'${spring.redis.is_cluster}' == 'true'")
@ConditionalOnProperty(prefix = "spring.redis", name = "host", matchIfMissing = true, havingValue = "null")
StatefulRedisClusterConnection<String, String> clusterConnection() {
return clusterClient().connect();
}

@Bean
RedisStringReactiveCommands<String, String> reactiveCommands() {
if (getIsCluster()) {
if (getHost() == null) {
return clusterConnection().reactive();
} else {
return connection().reactive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@Repository
@Slf4j
@ConditionalOnProperty(prefix = "spring.redis", name = {"host"})
@ConditionalOnProperty(prefix = "spring.redis", name = {"timeout"})
@RequiredArgsConstructor
public class RedisRepositoryImpl implements ReactiveRepository<PayloadWrapper, String> {
private final RedisStringReactiveCommands<String, String> reactiveCommands;
Expand Down