i'd like to obtain a redis cluster env. what should i do by using test-container?
JustryDeng opened this issue · 5 comments
JustryDeng commented
i'd like to obtain a redis cluster env. what should i do by using test-container?
JustryDeng commented
i resolved it. just like this
/**
* redis-cluster
*
* @author {@link JustryDeng}
* @since 2020/11/13 16:14:18
*/
@Slf4j
@Testcontainers
@SuppressWarnings("rawtypes")
@Import(ClientResources4Test.class)
@ContextConfiguration(initializers = RedisClusterEnvSupport.Initializer.class)
public class RedisClusterEnvSupport implements RedisEnvSupport {
private static final String DOCKER_IMAGE_NAME = "grokzen/redis-cluster:6.0.7";
@Container
@SuppressWarnings("deprecation")
public static GenericContainer redisContainer = new FixedHostPortGenericContainer(DOCKER_IMAGE_NAME)
.withFixedExposedPort(7000, 7000)
.withFixedExposedPort(7001, 7001)
.withFixedExposedPort(7002, 7002)
.withFixedExposedPort(7003, 7003)
.withFixedExposedPort(7004, 7004)
.withFixedExposedPort(7005, 7005);
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
String redisIpAddress = redisContainer.getContainerIpAddress();
String nodesInfo = redisIpAddress + ":" + 7000
+ "," + redisIpAddress + ":" + 7001
+ "," + redisIpAddress + ":" + 7002
+ "," + redisIpAddress + ":" + 7003
+ "," + redisIpAddress + ":" + 7004
+ "," + redisIpAddress + ":" + 7005;
log.info("spring.redis.cluster.nodes is [{}]", nodesInfo);
System.setProperty("spring.redis.cluster.nodes", nodesInfo);
}
}
}
@Primary
public class ClientResources4Test extends DefaultClientResources {
protected ClientResources4Test() {
super(DefaultClientResources.builder());
}
@Override
public SocketAddressResolver socketAddressResolver() {
return new SocketAddressResolver4Test(DnsResolvers.UNRESOLVED);
}
public static class SocketAddressResolver4Test extends SocketAddressResolver {
protected SocketAddressResolver4Test(DnsResolver dnsResolver) {
super(dnsResolver);
}
@Override
public SocketAddress resolve(RedisURI redisURI) {
return InetSocketAddress.createUnresolved("localhost", redisURI.getPort());
}
}
}
sbervoets commented
Hi @JustryDeng! Do you have any repo where I could check what you did? Thanks
JustryDeng commented
Hi @JustryDeng! Do you have any repo where I could check what you did? Thanks
this is a simple demo. you can @see
Hixon10 commented
Maybe it may be useful for someone. I've added one more example for it - https://github.com/Hixon10/spring-redis-cluster-testcontainers