zj
2024-06-03 287ac389edd047696d956afafdb855a93830bc0c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.nq.utils.redis;
 
 
import com.google.common.collect.Lists;
import com.nq.utils.PropertiesUtil;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
 
 
import java.util.List;
 
import static redis.clients.jedis.util.Hashing.MURMUR_HASH;
import static redis.clients.jedis.util.Sharded.DEFAULT_KEY_TAG_PATTERN;
 
 
public class RedisShardedPool {
 
    private static ShardedJedisPool pool;
 
    private static Integer maxTotal = Integer.valueOf(
 
            Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20")));
 
 
    private static Integer maxIdle = Integer.valueOf(
 
            Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "10")));
 
 
    private static Integer minIdle = Integer.valueOf(
 
            Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle", "2")));
    private static Integer setMaxWait = Integer.valueOf(
 
            Integer.parseInt(PropertiesUtil.getProperty("redis.max.wait.millis", "10000")));
 
    private static Boolean testOnBorrow = Boolean.valueOf(
 
            Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow", "true")));
 
 
    private static Boolean testOnReturn = Boolean.valueOf(
 
            Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return", "true")));
 
 
    private static String redisIp1 = PropertiesUtil.getProperty("redis1.ip");
 
    private static Integer redisPort1 = Integer.valueOf(
 
            Integer.parseInt(PropertiesUtil.getProperty("redis1.port")));
 
 
 
    private static void initPool() {
 
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
 
 
        config.setMaxTotal(maxTotal.intValue());
 
        config.setMaxIdle(maxIdle.intValue());
 
        config.setMinIdle(minIdle.intValue());
//        config.setMaxWaitMillis(setMaxWait.intValue());
 
        config.setTestOnBorrow(testOnBorrow.booleanValue());
 
        config.setTestOnReturn(testOnReturn.booleanValue());
 
 
        config.setBlockWhenExhausted(true);
 
 
        List<JedisShardInfo> jedisShardInfos = Lists.newArrayList();
 
        JedisShardInfo info1 = new JedisShardInfo(redisIp1, redisPort1.intValue(), 20000);
 
 
        info1.setPassword(PropertiesUtil.getProperty("redis1.pwd"));
 
        jedisShardInfos.add(info1);
 
 
        pool = new ShardedJedisPool(config, jedisShardInfos, MURMUR_HASH, DEFAULT_KEY_TAG_PATTERN);
 
    }
 
 
    static {
 
        initPool();
 
    }
 
 
    public static ShardedJedis getJedis() {
        return pool.getResource();
    }
 
 
    public static void returnResouce(ShardedJedis jedis) {
        pool.returnResource(jedis);
    }
 
 
    public static void returnBrokenResouce(ShardedJedis jedis) {
        pool.returnBrokenResource(jedis);
    }
 
 
    public static void main(String[] args) {
    }
 
}