-
Notifications
You must be signed in to change notification settings - Fork 2
/
RedisConfig.java
67 lines (55 loc) · 2.77 KB
/
RedisConfig.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
package ru.hixon.springredisclustertestcontainers;
import io.lettuce.core.RedisURI;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.SocketAddressResolver;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import java.net.SocketAddress;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
@TestConfiguration
public class RedisConfig {
public static final Set<Integer> redisClusterPorts = Set.of(7000, 7001, 7002, 7003, 7004, 7005);
private static final GenericContainer<?> redisClusterContainer = new GenericContainer<>("grokzen/redis-cluster:6.0.7")
.withExposedPorts(redisClusterPorts.toArray(new Integer[0]));
private static ConcurrentMap<Integer, Integer> redisClusterNatPortMapping = new ConcurrentHashMap<>();
private static ConcurrentMap<Integer, SocketAddress> redisClusterSocketAddresses = new ConcurrentHashMap<>();
static {
redisClusterContainer.start();
final String redisClusterNodes = redisClusterPorts.stream()
.map(port -> {
Integer mappedPort = redisClusterContainer.getMappedPort(port);
redisClusterNatPortMapping.put(port, mappedPort);
return redisClusterContainer.getContainerIpAddress() + ":" + mappedPort;
})
.collect(Collectors.joining(","));
System.setProperty("spring.redis.cluster.nodes", redisClusterNodes);
}
@Bean(destroyMethod = "shutdown")
public ClientResources lettuceClientResources() {
final SocketAddressResolver socketAddressResolver = new SocketAddressResolver() {
@Override
public SocketAddress resolve(RedisURI redisURI) {
Integer mappedPort = redisClusterNatPortMapping.get(redisURI.getPort());
if (mappedPort != null) {
SocketAddress socketAddress = redisClusterSocketAddresses.get(mappedPort);
if (socketAddress != null) {
return socketAddress;
}
redisURI.setPort(mappedPort);
}
redisURI.setHost(DockerClientFactory.instance().dockerHostIpAddress());
SocketAddress socketAddress = super.resolve(redisURI);
redisClusterSocketAddresses.putIfAbsent(redisURI.getPort(), socketAddress);
return socketAddress;
}
};
return ClientResources.builder()
.socketAddressResolver(socketAddressResolver)
.build();
}
}