-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedisClusterCacheConfiguration.java
173 lines (149 loc) · 6.02 KB
/
RedisClusterCacheConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import io.lettuce.core.ClientOptions.DisconnectedBehavior;
import io.lettuce.core.SocketOptions;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.embedded.RedisCluster;
import redis.embedded.RedisClusterBuilder;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
@Slf4j
@Data
@Configuration
@EnableConfigurationProperties
@ConditionalOnProperty(prefix = "data.rediscluster", name = {"nodes", "ttl", "redirects"})
@ConfigurationProperties(prefix = "data.rediscluster")
public class RedisClusterCacheConfiguration implements CacheBackendConfiguration {
private List<String> nodes;
private int redirects;
private int ttl;
private boolean embeded;
private String password;
/**
* Create an embedded redis cluster
*/
public class EmbeddedRedisCluster implements InitializingBean, DisposableBean {
private RedisCluster redisCluster;
private final static int SLAVES_PER_MASTER = 0;
@Override
public void afterPropertiesSet() {
if (embeded) {
RedisClusterBuilder builder = RedisCluster.builder();
IntStream.range(0, nodes.size()).forEach(i -> {
String[] nodeArray = nodes.get(i).split(":");
builder.serverPorts(Arrays.asList(Integer.valueOf(nodeArray[1])))
.replicationGroup(String.format("master%s", i), SLAVES_PER_MASTER);
});
redisCluster = builder.build();
redisCluster.start();
}
}
@Override
public void destroy() {
if (redisCluster != null) {
redisCluster.stop();
}
}
}
@Bean
public EmbeddedRedisCluster embeddedRedisCluster() {
return new EmbeddedRedisCluster();
}
@Bean
public LettuceClientConfiguration lettuceClientConfiguration() {
SocketOptions socketOptions = SocketOptions.builder()
.connectTimeout(Duration.ofSeconds(5))
.build();
ClusterTopologyRefreshOptions refreshOptions = ClusterTopologyRefreshOptions.builder()
.enableAllAdaptiveRefreshTriggers()
.enablePeriodicRefresh()
.build();
ClusterClientOptions clientOptions = ClusterClientOptions.builder()
.socketOptions(socketOptions)
.topologyRefreshOptions(refreshOptions)
.validateClusterNodeMembership(false)
.build();
return LettuceClientConfiguration.builder()
.commandTimeout(Duration.ofSeconds(10))
.clientOptions(clientOptions)
.build();
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
if (embeded) {
return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration());
} else {
return new LettuceConnectionFactory(redisClusterConfiguration(), lettuceClientConfiguration());
}
}
@DependsOn({"embeddedRedisCluster"})
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
String[] nodeArray = nodes.get(0).split(":");
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(nodeArray[0]);
redisStandaloneConfiguration.setPort(Integer.valueOf(nodeArray[1]));
redisStandaloneConfiguration.setPassword(getRedisPassword());
return redisStandaloneConfiguration;
}
@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
for (String node : nodes) {
String[] nodeArray = node.split(":");
RedisClusterNode redisNode = new RedisClusterNode(nodeArray[0], Integer.parseInt(nodeArray[1]));
redisClusterConfiguration.addClusterNode(redisNode);
}
redisClusterConfiguration.setMaxRedirects(redirects);
redisClusterConfiguration.setPassword(getRedisPassword());
return redisClusterConfiguration;
}
@Bean
public StringRedisSerializer stringRedisSerializer() {
return new StringRedisSerializer();
}
@Bean
public SnappyRedisSerializer genericSnappyRedisSerializer() {
return new SnappyRedisSerializer<>(new KryoRedisSerializer<>());
}
@Bean
public RedisCacheWriter redisCacheWriter() {
return RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory());
}
@Bean
public RedisCacheConfiguration defaultRedisCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.disableKeyPrefix()
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext
.SerializationPair.fromSerializer(stringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext
.SerializationPair.fromSerializer(genericSnappyRedisSerializer()))
.entryTtl(Duration.ofSeconds(ttl));
}
private RedisPassword getRedisPassword() {
return StringUtils.isNotEmpty(password) ? RedisPassword.of(password) : RedisPassword.none();
}
}