欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二十三、Redis Sentinel客户端

程序员文章站 2022-06-06 09:50:19
...

                               Redis Sentinel客户端

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
package com.yang;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

public class RedisSentinelFailOver {

    public static void main(String[] args) {
        String masterName="mymaster";
        Set<String> sentinels=new HashSet<String>();
        sentinels.add("192.168.1.107:26379");
        sentinels.add("192.168.1.107:26380");
        sentinels.add("192.168.1.107:26381");
        JedisSentinelPool pools=new JedisSentinelPool(masterName, sentinels);

        int counter=0;
        while(true) {
            counter++;
            Jedis jedis=null;
            try {
                jedis=pools.getResource();
                int index=new Random().nextInt(10000);
                String key="k-"+index;
                String value="v-"+index;
                jedis.set(key, value);
                TimeUnit.MICROSECONDS.sleep(10);
            } catch (Exception e) {
                e.getMessage();
                e.printStackTrace();
            }finally {
                if(jedis!=null) {
                    jedis.close();
                }
            }

        }


    }
}
相关标签: redis 客户端