redis cluster 基本的redis操作示例:
JedisCluster jc = null;
@Before
public void before(){
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000));
jc = new JedisCluster(jedisClusterNodes);
}
@Test
public void test_incr(){
String key = "page_view";
jc.del(key);
jc.incr(key);
String result = jc.get(key);
System.out.println(result);
Assertions.assertThat(result).isEqualTo(1+"");
}
@Test
public void test_setAndGetStringVal(){
String key = "foo";
String value = "bar";
jc.set(key, value);
String result = jc.get(key);
System.out.println(result);
Assertions.assertThat(result).isEqualTo(value);
}
@Test
public void test_setAndGetStringVal_and_set_expire() throws InterruptedException{
String key = "hello";
String value = "world";
int seconds = 3;
jc.setex(key, seconds , value);
String result = jc.get(key);
System.out.println(result);
Assertions.assertThat(result).isEqualTo(value);
Thread.sleep(seconds*1000);
result = jc.get(key);
System.out.println(result);
Assertions.assertThat(result).isNull();
}
@Test
public void test_setAndGetHashVal(){
String key = "website";
String field= "google";
String value = "google.com";
jc.del(key);
jc.hset(key, field, value);
String result = jc.hget(key, field);
System.out.println(result);
Assertions.assertThat(result).isEqualTo(value);
}
@Test
public void test_setAndGetListVal(){
String key = "mylist";
jc.del(key);
String[] vals = {"a","b","c"};
jc.rpush(key, vals);
List<String> result = jc.lrange(key, 0, -1);
System.out.println(result);
Assertions.assertThat(result).containsExactly(vals);
}
@Test
public void test_setAndGetSetVal(){
String key = "language";
jc.del(key);
String[] members = {"java", "ruby", "python"};
jc.sadd(key, members);
Set<String> result = jc.smembers(key);
System.out.println(result);
Assertions.assertThat(result).containsOnly(members);
}
演示主从切换时的jedis操作情景:
JedisCluster jc = null;
@Before
public void before(){
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000));
jc = new JedisCluster(jedisClusterNodes);
}
/**
* 在一个无限循环中不停的读写
* @throws InterruptedException
*/
@Test
public void setAndWriteStringValueRepeatedly() throws InterruptedException{
String key = "test_oper_during_failover";
jc.del(key);
long failureTime = 0;
long recoveryTime = 0;
while(true){
try {
String result = jc.get(key);
if(failureTime != 0 && recoveryTime==0){
recoveryTime = System.currentTimeMillis();
System.out.println("Cluster is recovered! Downtime lasted "+(recoveryTime-failureTime)+" ms");
}
System.out.println(result);
jc.set(key, System.currentTimeMillis()+"");
} catch (Exception e) {
if(failureTime==0)
failureTime=System.currentTimeMillis();
e.printStackTrace();
}
Thread.sleep(1000);
}
}
输出:
null
1430663109857
1430663110860
......
redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:37)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:70)
...
...
redis.clients.jedis.exceptions.JedisClusterException: CLUSTERDOWN The cluster is down
at redis.clients.jedis.Protocol.processError(Protocol.java:111)
at redis.clients.jedis.Protocol.process(Protocol.java:138)
...
Cluster is recovered! Downtime lasted 7089 ms
1430663137897
1430663146602
...
pom.xml:
<properties>
<junit.version>4.11</junit.version>
<assertj.version>1.7.0</assertj.version>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- assertj -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
参考:
https://github.com/xetorthio/jedis
问题:
hostandport 只需要指定一个 host ip和端口?其他的都直接由rediscluster去获取是吧?
摘自http://redis.io/topics/cluster-tutorial
此段话是针对ruby client的, 但是我想应该也适用于jedis。
3 startup_nodes = [
4 {:host => "127.0.0.1", :port => 7000},
5 {:host => "127.0.0.1", :port => 7001}
6 ]
The startup nodes don't need to be all the nodes of the cluster. The important thing is that at least one node is reachable. Also note that redis-rb-cluster updates this list of startup nodes as soon as it is able to connect with the first node. You should expect such a behavior with any other serious client.
来源:oschina
链接:https://my.oschina.net/u/1175066/blog/411077