Redis Demo系列之(一)获取Jedis链接及Jedis连接池

版权声明:欢迎转载,转载请说明出处https://csdn.yanxml.com。大数据Github项目地址https://github.com/SeanYanxml/bigdata。 https://blog.csdn.net/u010416101/article/details/81228879

前言

本章主要介绍Redis的Java实现Jedis的获取与简单使用,本章节主要包括如下几个部分:

  • Jedis
  • JedisPool
  • RedisSentinelPool(哨兵模式)
  • RedisClusterPool(集群模式)
    注: Spring-Redis还有使用ShardRedis以及RedisTemplate进行操作Redis的,本人未深入研究,故略。

本文相关代码,可在我的Github项目https://github.com/SeanYanxml/bigdata/ 目录下可以找到。
PS: (如果觉得项目不错,可以给我一个Star。)


Jedis

使用Jedis需要导入Jedis.jar,如果你使用Maven。需将如下依赖导入你的pom.xml文件内。

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
  • 获取Jedis对象
public static Jedis getJedis(){
        String redisUrl = "192.168.100.xx";
        Jedis jedis = new Jedis(redisUrl);
        // 如果有设置密码的话
        jedis.auth("admin");
        return jedis;
    }
  • 获取Redis线程池
    // Jedis attribute
    private static String address = "192.168.100.xx";
    private static int port = 6379;
    private static int timeout = 10000;
    private static String auth = "admin";

    // JedisConfig attribute
    private static int max_active = 1024;
    private static int max_idle = 200;
    private static int max_wait = 10000;
    private static boolean test_on_borrow = true;
    private static JedisPool jedisPool = null;

    static {
        try{
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(max_idle);
            config.setMaxWaitMillis(max_wait);
            config.setTestOnBorrow(test_on_borrow);
            config.setMaxTotal(max_active);
            jedisPool = new JedisPool(config,address,port,timeout,auth);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
  • SentineJedislPool
    private static int timeout = 10000;
    private static String auth = "admin";
    private static String redisList = "192.168.100.xx1:26379,192.168.100.xx2:26379";
    private static String[] arrRedisList = redisList.split(",");
    private static String masterName = "mymaster";


    // JedisConfig attribute
    private static int max_active = 1024;
    private static int max_idle = 200;
    private static int max_wait = 10000;
    private static boolean test_on_borrow = true;
    private static JedisSentinelPool jedisSentinelPool = null;

    static {
         JedisPoolConfig config = new JedisPoolConfig();
         config.setMaxIdle(max_idle);
         config.setMaxWaitMillis(max_wait);
         config.setTestOnBorrow(test_on_borrow);
         config.setMaxTotal(max_active);
         // 定义集群连接接
         Set<String> sentinels = new HashSet<String>();
         if(arrRedisList != null){
             int redisClusterSize = arrRedisList.length;
             for(int i = 0; i< redisClusterSize; i++){
                 sentinels.add(arrRedisList[i]);
             }
         jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config,timeout,auth);
    // jedisSentinelPool.set();
         }
    }
  • RedisClusterPool
private static final String default_hashtag = "{default}";

    private static int max_active = 1024;
    private static int max_idle = 200;
    private static int max_wait = 10000;
    private static boolean test_on_borrow = true;
    private static JedisCluster jedisCluster = null;
    private static String redisListStr="192.168.xx.76:7000,192.168.xx.76:7001,192.168.xx.76:7002,192.168.xx.76:7003,192.168.xx.76:7004,192.168.xx.76:7005";

    private static String auth = "admin"; 
    private static String connectionTimeout = "10000";
    private static String soTimeout = "10000";
    private static String maxAttempts = "3";

    static {
        Set<String> redisSets = Stream.of(redisListStr.split(",")).collect(Collectors.toSet());
         Set<HostAndPort> nodes = new HashSet<>();
         for(String str:redisSets){
             // 切分Redis对象 分成 Host&Port类型
             String[] nodeInfo = str.split(":");
             nodes.add(new HostAndPort(nodeInfo[0], Integer.parseInt(nodeInfo[1])));

         }

        // set redis pool config
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(max_idle);
        config.setMaxWaitMillis(max_wait);
        config.setTestOnBorrow(test_on_borrow);
        config.setMaxTotal(max_active);
        // 定义集群连接接        
        if(null != auth && !auth.equals("")) {
            jedisCluster = new JedisCluster(nodes,Integer.parseInt(connectionTimeout),Integer.parseInt(soTimeout),Integer.parseInt(maxAttempts),auth,config);
        }else {
            jedisCluster = new JedisCluster(nodes,Integer.parseInt(connectionTimeout),Integer.parseInt(soTimeout),Integer.parseInt(maxAttempts),config);
        }

        // jedisCluster.get();
        // need close or not
//      jedisCluster.close();
    }

Reference

[1]. Java连接远程Redis
[2]. Redis学习笔记(五)jedis(JedisCluster)操作Redis集群 redis-cluster

阅读更多

扫码向博主提问

在风中的意志

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • Java
  • 大数据
去开通我的Chat快问
换一批

没有更多推荐了,返回首页