基本上最Popular的嵌入式redis是以下这个项目
https://github.com/kstyrc/embedded-redis
但这个项目也存在维护不力的问题。
1) 大量缺陷未修复。在笔者写下此文时,这个项目上未关闭的缺陷issues 有33个。 并且CI 显示目前这个版本在windows上的构建是失败的。
- 缺少更新。这个项目的最近一次更新在2016年6月。
经过试用之后,笔者发现,以下这个案例能够顺利通过。
package redis.embedded;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import static org.junit.Assert.assertEquals;
public class RedisServerClusterTest {
private RedisServer redisServer1;
private RedisServer redisServer2;
@Before
public void setUp() throws Exception {
redisServer1 = RedisServer.builder()
.port(6300)
.build();
redisServer2 = RedisServer.builder()
.port(6301)
.slaveOf("localhost", 6300)
.build();
redisServer1.start();
redisServer2.start();
}
@Test
public void testSimpleOperationsAfterRun() throws Exception {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = new JedisPool("localhost", 6300);
jedis = pool.getResource();
jedis.mset("abc", "1", "def", "2");
assertEquals("1", jedis.mget("abc").get(0));
assertEquals("2", jedis.mget("def").get(0));
assertEquals(null, jedis.mget("xyz").get(0));
} finally {
if (jedis != null)
pool.returnResource(jedis);
}
}
@After
public void tearDown() throws Exception {
redisServer1.stop();
redisServer2.stop();
}
}
但是在用例执行完之后, 程序似乎留下了一些未关闭的进程,可能会导致资源泄露和可能的端口冲突。 因此,可能需要去优化redisServer的stop方法,或者干脆通过在@After方法中手工去杀进程的方式将运行环境清理干净。
redis_firewall.JPG
redis_process_detail.JPG
redis_process_list.JPG