tarossi/embedded-redis


Redis embedded server for Java integration testing

Download


Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}
	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.tarossi:embedded-redis:1.5.2'
	}
	<dependency>
	    <groupId>com.github.tarossi</groupId>
	    <artifactId>embedded-redis</artifactId>
	    <version>1.5.2</version>
	</dependency>

                            
    libraryDependencies += "com.github.tarossi" % "embedded-redis" % "1.5.2"
        
        

                            
    :dependencies [[com.github.tarossi/embedded-redis "1.5.2"]]
        
        

Readme


embedded-redis

Redis embedded server for Scala Integration Testing.

This is a fork of https://github.com/jenchenUA/embedded-redis, which in turn is a fork of https://github.com/fmonniot/embedded-redis

Features:

  • Embeds 3.2.1 executables in the JAR.

Usage

Running RedisServer is as simple as:

RedisServer redisServer = new RedisServer(6379);
redisServer.start();
// do some work
redisServer.stop();

You can also provide RedisServer with your own executable:

// Give an OS-independent matrix
RedisExecProvider provider = RedisExecProvider.defaultProvider()
  .override(OS.UNIX, "/path/to/unix/redis")
  .override(OS.WINDOWS, Architecture.x86, "/path/to/windows/redis")
  .override(OS.WINDOWS, Architecture.x86_64, "/path/to/windows/redis64")
  .override(OS.MAC_OS_X, Architecture.x86, "/path/to/macosx/redis")
  .override(OS.MAC_OS_X, Architecture.x86_64, "/path/to/macosx/redis64")
  
RedisServer redisServer = new RedisServer(provider, 6379);

You can also use fluent API to create RedisServer:

RedisServer redisServer = new RedisServer.Builder()
  .redisExecProvider(customRedisProvider)
  .port(6379)
  .slaveOf("locahost", 6378)
  .configFile("/path/to/your/redis.conf")
  .build();

Or even create the redis.conf file from the builder:

RedisServer redisServer = new RedisServer.Builder()
  .redisExecProvider(customRedisProvider)
  .port(6379)
  .slaveOf("locahost", 6378)
  .setting("daemonize no")
  .setting("appendonly no")
  .build();

The JedisUtil class contains utility methods to get the list of port in a Jedis friendly format.

Setting up a cluster

Embedded Redis has support for Redis Cluster.

Redis cluster = new RedisCluster.Builder()
        .withServerBuilder(myOwnRedisServerBuilder)
        .serverPorts(Arrays.asList(42000,42001,42002,42003,42004,42005))
        .numOfReplicates(1)
        .numOfRetries(42)
        .build()

cluster.start()
cluster.stop()

Setting up a sentinel cluster

Embedded Redis has support for HA Redis clusters with Sentinels and master-slave replication

Using ephemeral ports

A simple Redis cluster on ephemeral ports, with setup similar to that from production, would look like this:

//creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
cluster = new SentinelCluster.Builder()
                .ephemeral()
                .sentinelCount(3)
                .quorumSize(2)
                .replicationGroup("master1", 1)
                .replicationGroup("master2", 1)
                .replicationGroup("master3", 1)
                .build();
cluster.start();
cluster.stop();

The above example starts Redis cluster on ephemeral ports, which you can later get with cluster.ports(), which will return a list of all ports of the cluster. You can also get ports of sentinels with cluster.sentinelPorts() or servers with cluster.serverPorts().

This library also includes an utility for helping you integrating it with Jedis

//retrieve ports on which sentinels have been started, using a simple Jedis utility class
Set<String> jedisSentinelHosts = JedisUtil.sentinelHosts(cluster);
// testing code that requires redis running
JedisSentinelPool pool = new JedisSentinelPool("master1", jedisSentinelHosts);

Using predefined ports

You can also start Redis cluster on predefined ports and even mix both approaches:

final List<Integer> sentinels = Arrays.asList(26739, 26912);
final List<Integer> group1 = Arrays.asList(6667, 6668);
final List<Integer> group2 = Arrays.asList(6387, 6379);

//creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
cluster = new SentinelCluster.Builder()
                .sentinelPorts(sentinels)
                .quorumSize(2)
                .serverPorts(group1).replicationGroup("master1", 1)
                .serverPorts(group2).replicationGroup("master2", 1)
                .ephemeralServers().replicationGroup("master3", 1)
                .build();
cluster.start();

The above will create and start a cluster with sentinels on ports 26739, 26912, first replication group on 6667, 6668, second replication group on 6387, 6379 and third replication group on ephemeral ports.

License

Licensed under the Apache License, Version 2.0

Contributors