redis cluster jedis client 示例

redis cluster 基本的redis操作示例:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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操作情景:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 );
         }
          
     }

 

输出:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< 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

 

redis cluster jedis client 示例_第1张图片

 

问题:

 

  1.  hostandport 只需要指定一个 host ip和端口?其他的都直接由rediscluster去获取是吧?

     

    摘自http://redis.io/topics/cluster-tutorial

    此段话是针对ruby client的, 但是我想应该也适用于jedis。

 

?
1
2
3
4
5
6
  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 o

http://my.oschina.net/zhuguowei/blog/411077

 

你可能感兴趣的:(redis cluster jedis client 示例)