java client Jedis operates the Redis Sentinel connection pool

  • 2020-06-15 09:03:46
  • OfStack

pom. xml configuration


<dependency> 
  <groupId>org.springframework.data</groupId> 
  <artifactId>spring-data-redis</artifactId> 
  <version>1.0.2.RELEASE</version> 
</dependency> 
<dependency> 
  <groupId>redis.clients</groupId> 
  <artifactId>jedis</artifactId> 
  <version>2.7.0</version> 
  <type>jar</type> 
  <scope>compile</scope> 
</dependency> 
?
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 public class JedisPoolUtil { 
   
  private static JedisSentinelPool pool = null; 
 
  public static Properties getJedisProperties() { 
 
    Properties config = new Properties(); 
    InputStream is = null; 
    try { 
      is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("cacheConfig.properties"); 
      config.load(is); 
    } catch (IOException e) { 
      logger.error("", e); 
    } finally { 
      if (is != null) { 
        try { 
          is.close(); 
        } catch (IOException e) { 
          logger.error("", e); 
        } 
      } 
    } 
    return config; 
  } 
 
  /** 
   *  Create a connection pool  
   * 
   */
  private static void createJedisPool() { 
    //  Establish connection pool configuration parameters  
    JedisPoolConfig config = new JedisPoolConfig(); 
    Properties prop = getJedisProperties(); 
    //  Set the maximum number of connections  
    config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE"))); 
    //  Set the maximum block time, in milliseconds milliseconds 
    config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT"))); 
    //  Set up space connection  
    config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE"))); 
    // jedis Whether an instance is available  
    boolean borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? false : true; 
    config.setTestOnBorrow(borrow); 
    //  Create a connection pool  
//   pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));//  Limit the number of threads IP Address, port, timeout  
    // To obtain redis password  
    String password = StringUtil.nullToString(prop.getProperty("PASSWORD")); 
 
     String masterName = "mymaster"; 
    Set<String> sentinels = new HashSet<String>(); 
    sentinels.add("192.168.137.128:26379"); 
    sentinels.add("192.168.137.128:26380"); 
    sentinels.add("192.168.137.128:26381"); 
    pool = new JedisSentinelPool(masterName, sentinels, config); 
  } 
 
  /** 
   *  Synchronization initialization in a multithreaded environment  
   */
  private static synchronized void poolInit() { 
    if (pool == null) 
      createJedisPool(); 
  } 
 
  /** 
   *  To obtain 1 a jedis  object  
   * 
   * @return 
   */
  public static Jedis getJedis() { 
    if (pool == null) 
      poolInit(); 
    return pool.getResource(); 
  } 
 
  /** 
   *  The release of 1 A connection  
   * 
   * @param jedis 
   */
  public static void returnRes(Jedis jedis) { 
    pool.returnResource(jedis); 
  } 
 
  /** 
   *  The destruction 1 A connection  
   * 
   * @param jedis 
   */
  public static void returnBrokenRes(Jedis jedis) { 
    pool.returnBrokenResource(jedis); 
  } 
   
   
  public static void main(String[] args){ 
    Jedis jedis=getJedis(); 
     
  } 
 
} 

Related articles: