Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i'd like to obtain a redis cluster env. what should i do by using test-container? #3467

Closed
JustryDeng opened this issue Nov 13, 2020 · 5 comments

Comments

@JustryDeng
Copy link

JustryDeng commented Nov 13, 2020

i'd like to obtain a redis cluster env. what should i do by using test-container?

@JustryDeng JustryDeng changed the title i'd like to obtain a redis cluster env. what should i do for using test-container? i'd like to obtain a redis cluster env. what should i do by using test-container? Nov 13, 2020
@JustryDeng
Copy link
Author

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());
        }
    }
}

@bsideup bsideup closed this as completed Nov 17, 2020
@sbervoets
Copy link

Hi @JustryDeng! Do you have any repo where I could check what you did? Thanks

@JustryDeng
Copy link
Author

Hi @JustryDeng! Do you have any repo where I could check what you did? Thanks

this is a simple demo. you can @see

@Hixon10
Copy link

Hixon10 commented Jun 19, 2021

Maybe it may be useful for someone. I've added one more example for it - https://github.com/Hixon10/spring-redis-cluster-testcontainers

@viglucci
Copy link

viglucci commented Jun 24, 2021

@Hixon10 thanks for linking that example. It was useful to me today as I needed to do exactly this (but outside of a Spring application), and I'm not overly familiar with Lettuce. Additionally, I got a kick out of it linking to my blog article 😆 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants