`
m635674608
  • 浏览: 5069102 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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

 

 

问题:

 

  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集群java客户端,支持批量提交

    java客户端不是很好支持redis cluster,spring-date-redis和jedis批量提交还不支持,单个提交都是可以的。 为了批量解决批量提交 网上有几个方案,本示例使用了其中一种,demo里的JedisClusterPipeline类是网上找的...

    redis-java项目配置手册

    JedisCluster redisCluster = ClientBuilder.redisCluster(appId) .setJedisPoolConfig(poolConfig) .setConnectionTimeout(1000) // 设置连接超时时间 .setSoTimeout(1000) // 设置SO超时时间 ....

    java链接Redis实例

    可能意味着你在一天的学习或开发过程中,会涵盖上述的基本操作,甚至深入到Redis的数据结构(如集合、有序集合、哈希表)的使用,以及Redis的高级特性如事务、Lua脚本、主从复制、Sentinel哨兵系统或Cluster集群等。...

    redis 快速学习demo

    - 示例代码:初始化Jedis实例,连接Redis服务器,设置和获取String键值对。 ```java Jedis jedis = new Jedis("localhost", 6379); jedis.set("key", "value"); String value = jedis.get("key"); ``` 3. **...

    Redis的Jar包和工具类.rar

    Lettuce不仅支持基本的Redis命令,还支持Redis Cluster、Sentinel和模版方法,使得代码更加整洁。 在使用Lettuce时,我们需要先创建一个RedisClient,然后通过该客户端连接到Redis服务器,如下所示: ```java ...

    Redis-5.0.0集群配置

    - **Jedis (Java Cluster Client)**: Java应用程序可以通过Jedis客户端库来连接Redis集群。 - **r3c (C++ Cluster Client)**: 对于使用C++开发的应用程序,可以使用r3c作为客户端库。 #### 11. 新增节点 - **添加...

    redis和spring整合,redis集群

    3. **连接集群**: 配置 Spring 与 Redis 集群的连接时,需要使用 `JedisCluster` 或 `Lettuce` 的 `ClusterClient`。配置方法与单节点类似,但需要提供一组节点的地址而不是单一地址。 4. **注意事项**: 集群模式下...

    springboot接入cachecloud redis示例实践

    redis_cluster_suffix = /cache/client/redis/cluster/%s.json?clientVersion= redis_sentinel_suffix = /cache/client/redis/sentinel/%s.json?clientVersion= ``` 六、结论 本文主要介绍了 SpringBoot 接入 Cache...

    spring boot+redis单机与集群.zip

    使用`JedisCluster`或者Lettuce的`ClusterClient`可以与Redis集群进行交互。需要注意的是,不是所有的Redis操作在集群模式下都支持,比如某些涉及到跨节点的操作。 集成Redis集群时,我们还需要了解一些概念,如...

Global site tag (gtag.js) - Google Analytics