温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java客户端中如何使用Jedis实现操作Redis Sentinel 连接池

发布时间:2020-11-12 16:02:12 来源:亿速云 阅读:361 作者:Leah 栏目:编程语言

这篇文章给大家介绍java客户端中如何使用Jedis实现操作Redis Sentinel 连接池,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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> 
&#63;
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" &#63; 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 连接池就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI