Redis embedded server for Java integration testing
Clone or download
Pull request Compare This branch is 89 commits ahead, 2 commits behind kstyrc:master.
fmonniot Deploy to bintray via JDK8 instead of 7
This may fix a problem with the bintrayUpload task
Latest commit 029fb86 May 23, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper
modules
samples
src
.gitignore
.travis.yml
CHANGELOG.md
LICENSE.txt
README.md
build.gradle
deploy.sh
gradlew
gradlew.bat
settings.gradle

README.md

embedded-redis Build Status Download

Redis embedded server for Java integration testing

This is a fork of https://github.com/kstyrc/embedded-redis and remove support for several features:

  • Embedded redis binaries, you MUST provide them yourself
  • Java 6 support
  • Spring data support (should work but not tested anymore)

It also add support for some other features:

  • Support for Redis Cluster
  • Cleanup the API
  • Let user access Redis instances logs
  • More consistent use of PortProvider interface

Maven dependency

This library is available on jCenter:

<dependency>
  <groupId>eu.monniot.redis</groupId>
  <artifactId>embedded-redis</artifactId>
  <version>1.6.0</version>
</dependency>

Usage

Redis Server

Running RedisServer is as simple as:

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

This works as long as you have a redis-server-3.0.7-* executables in your path (see redis/embedded/RedisExecProvider.java for the complete list of default binaries paths).

You can also provide RedisServer with your own executable:

// Give an OS-independent matrix
RedisExecProvider provider = RedisExecProvider.build()
  .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/redis")
  .override(OS.MAC_OS_X, Architecture.x86, "/path/to/macosx/redis")
  .override(OS.MAC_OS_X, Architecture.x86_64, "/path/to/macosx/redis")
  
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.

Redis version

Since this library doesn't provides binary by default, and thanks to @xhoong, we can now further customize redis executables and the pattern used to detect when they became available. This is fairly useful when using a version of redis not supported by this library:

RedisExecProvider provider = RedisExecProvider.build()
  .override(OS.UNIX, Architecture.x86_64, RedisExecutable.build("redis-4.2-bin", "Ready to accept connections"))

License

Licensed under the Apache License, Version 2.0

Contributors