当前位置: 移动技术网 > IT编程>开发语言>Java > java客户端Jedis操作Redis Sentinel 连接池的实现方法

java客户端Jedis操作Redis Sentinel 连接池的实现方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

pom.xml配置

<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; 
  } 
 
  /** 
   * 创建连接池 
   * 
   */
  private static void createjedispool() { 
    // 建立连接池配置参数 
    jedispoolconfig config = new jedispoolconfig(); 
    properties prop = getjedisproperties(); 
    // 设置最大连接数 
    config.setmaxtotal(stringutil.nulltointeger(prop.getproperty("max_active"))); 
    // 设置最大阻塞时间,记住是毫秒数milliseconds 
    config.setmaxwaitmillis(stringutil.nulltointeger(prop.getproperty("max_wait"))); 
    // 设置空间连接 
    config.setmaxidle(stringutil.nulltointeger(prop.getproperty("max_idle"))); 
    // jedis实例是否可用 
    boolean borrow = prop.getproperty("test_on_borrow") == "false" ? false : true; 
    config.settestonborrow(borrow); 
    // 创建连接池 
//   pool = new jedispool(config, prop.getproperty("addr"), stringutil.nulltointeger(prop.getproperty("port")), stringutil.nulltointeger(prop.getproperty("timeout")));// 线程数量限制,ip地址,端口,超时时间 
    //获取redis密码 
    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); 
  } 
 
  /** 
   * 在多线程环境同步初始化 
   */
  private static synchronized void poolinit() { 
    if (pool == null) 
      createjedispool(); 
  } 
 
  /** 
   * 获取一个jedis 对象 
   * 
   * @return 
   */
  public static jedis getjedis() { 
    if (pool == null) 
      poolinit(); 
    return pool.getresource(); 
  } 
 
  /** 
   * 释放一个连接 
   * 
   * @param jedis 
   */
  public static void returnres(jedis jedis) { 
    pool.returnresource(jedis); 
  } 
 
  /** 
   * 销毁一个连接 
   * 
   * @param jedis 
   */
  public static void returnbrokenres(jedis jedis) { 
    pool.returnbrokenresource(jedis); 
  } 
   
   
  public static void main(string[] args){ 
    jedis jedis=getjedis(); 
     
  } 
 
} 

以上这篇java客户端jedis操作redis sentinel 连接池的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网