在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kstyrc/embedded-redis开源软件地址:https://github.com/kstyrc/embedded-redis开源编程语言:Java 100.0%开源软件介绍:embedded-redisRedis embedded server for Java integration testing Maven dependencyMaven Central: <dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
</dependency> Previous releases (before 0.6): <repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
...
<dependency>
<groupId>redis.embedded</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.5</version>
</dependency> UsageRunning 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: // 1) given explicit file (os-independence broken!)
RedisServer redisServer = new RedisServer("/path/to/your/redis", 6379);
// 2) given os-independent matrix
RedisExecProvider customProvider = 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/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(customProvider, 6379); You can also use fluent API to create RedisServer: RedisServer redisServer = RedisServer.builder()
.redisExecProvider(customRedisProvider)
.port(6379)
.slaveOf("locahost", 6378)
.configFile("/path/to/your/redis.conf")
.build(); Or even create simple redis.conf file from scratch: RedisServer redisServer = RedisServer.builder()
.redisExecProvider(customRedisProvider)
.port(6379)
.slaveOf("locahost", 6378)
.setting("daemonize no")
.setting("appendonly no")
.setting("maxheap 128M")
.build(); Setting up a clusterOur Embedded Redis has support for HA Redis clusters with Sentinels and master-slave replication Using ephemeral portsA simple redis integration test with Redis cluster on ephemeral ports, with setup similar to that from production would look like this: public class SomeIntegrationTestThatRequiresRedis {
private RedisCluster cluster;
private Set<String> jedisSentinelHosts;
@Before
public void setup() throws Exception {
//creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
cluster = RedisCluster.builder().ephemeral().sentinelCount(3).quorumSize(2)
.replicationGroup("master1", 1)
.replicationGroup("master2", 1)
.replicationGroup("master3", 1)
.build();
cluster.start();
//retrieve ports on which sentinels have been started, using a simple Jedis utility class
jedisSentinelHosts = JedisUtil.sentinelHosts(cluster);
}
@Test
public void test() throws Exception {
// testing code that requires redis running
JedisSentinelPool pool = new JedisSentinelPool("master1", jedisSentinelHosts);
}
@After
public void tearDown() throws Exception {
cluster.stop();
}
} Retrieving portsThe above example starts Redis cluster on ephemeral ports, which you can later get with Using predefined portsYou can also start Redis cluster on predefined ports and even mix both approaches: public class SomeIntegrationTestThatRequiresRedis {
private RedisCluster cluster;
@Before
public void setup() throws Exception {
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 = RedisCluster.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 Redis versionWhen not provided with the desired redis executable, RedisServer runs os-dependent executable enclosed in jar. Currently is uses:
However, you should provide RedisServer with redis executable if you need specific version. LicenseLicensed under the Apache License, Version 2.0 Contributors
Changelog0.6
0.5
0.4
0.3
0.2
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论