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

释放双眼,带上耳机,听听看~!

前言

本章主要介绍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文件内。


1
2
3
4
5
6
1        <dependency>
2            <groupId>redis.clients</groupId>
3            <artifactId>jedis</artifactId>
4            <version>2.9.0</version>
5        </dependency>
6
  • 获取Jedis对象


1
2
3
4
5
6
7
8
1public static Jedis getJedis(){
2        String redisUrl = "192.168.100.xx";
3        Jedis jedis = new Jedis(redisUrl);
4        // 如果有设置密码的话
5        jedis.auth("admin");
6        return jedis;
7    }
8
  • 获取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
1    // Jedis attribute
2    private static String address = "192.168.100.xx";
3    private static int port = 6379;
4    private static int timeout = 10000;
5    private static String auth = "admin";
6
7    // JedisConfig attribute
8    private static int max_active = 1024;
9    private static int max_idle = 200;
10    private static int max_wait = 10000;
11    private static boolean test_on_borrow = true;
12    private static JedisPool jedisPool = null;
13
14    static {
15        try{
16            JedisPoolConfig config = new JedisPoolConfig();
17            config.setMaxIdle(max_idle);
18            config.setMaxWaitMillis(max_wait);
19            config.setTestOnBorrow(test_on_borrow);
20            config.setMaxTotal(max_active);
21            jedisPool = new JedisPool(config,address,port,timeout,auth);
22        }catch(Exception e){
23            e.printStackTrace();
24        }
25    }
26
  • SentineJedislPool


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
1    private static int timeout = 10000;
2    private static String auth = "admin";
3    private static String redisList = "192.168.100.xx1:26379,192.168.100.xx2:26379";
4    private static String[] arrRedisList = redisList.split(",");
5    private static String masterName = "mymaster";
6
7
8    // JedisConfig attribute
9    private static int max_active = 1024;
10    private static int max_idle = 200;
11    private static int max_wait = 10000;
12    private static boolean test_on_borrow = true;
13    private static JedisSentinelPool jedisSentinelPool = null;
14
15    static {
16         JedisPoolConfig config = new JedisPoolConfig();
17         config.setMaxIdle(max_idle);
18         config.setMaxWaitMillis(max_wait);
19         config.setTestOnBorrow(test_on_borrow);
20         config.setMaxTotal(max_active);
21         // 定义集群连接接
22         Set<String> sentinels = new HashSet<String>();
23         if(arrRedisList != null){
24             int redisClusterSize = arrRedisList.length;
25             for(int i = 0; i< redisClusterSize; i++){
26                 sentinels.add(arrRedisList[i]);
27             }
28         jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config,timeout,auth);
29    // jedisSentinelPool.set();
30         }
31    }
32
  • RedisClusterPool


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
1private static final String default_hashtag = "{default}";
2
3    private static int max_active = 1024;
4    private static int max_idle = 200;
5    private static int max_wait = 10000;
6    private static boolean test_on_borrow = true;
7    private static JedisCluster jedisCluster = null;
8    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";
9
10    private static String auth = "admin";
11    private static String connectionTimeout = "10000";
12    private static String soTimeout = "10000";
13    private static String maxAttempts = "3";
14
15    static {
16        Set<String> redisSets = Stream.of(redisListStr.split(",")).collect(Collectors.toSet());
17         Set<HostAndPort> nodes = new HashSet<>();
18         for(String str:redisSets){
19             // 切分Redis对象 分成 Host&Port类型
20             String[] nodeInfo = str.split(":");
21             nodes.add(new HostAndPort(nodeInfo[0], Integer.parseInt(nodeInfo[1])));
22
23         }
24
25        // set redis pool config
26        JedisPoolConfig config = new JedisPoolConfig();
27        config.setMaxIdle(max_idle);
28        config.setMaxWaitMillis(max_wait);
29        config.setTestOnBorrow(test_on_borrow);
30        config.setMaxTotal(max_active);
31        // 定义集群连接接        
32        if(null != auth && !auth.equals("")) {
33            jedisCluster = new JedisCluster(nodes,Integer.parseInt(connectionTimeout),Integer.parseInt(soTimeout),Integer.parseInt(maxAttempts),auth,config);
34        }else {
35            jedisCluster = new JedisCluster(nodes,Integer.parseInt(connectionTimeout),Integer.parseInt(soTimeout),Integer.parseInt(maxAttempts),config);
36        }
37
38        // jedisCluster.get();
39        // need close or not
40//      jedisCluster.close();
41    }
42

Reference

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

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索