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

 更新时间:2017年03月25日 10:10:20   投稿:jingxian  
下面小编就为大家带来一篇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> 
?
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 连接池的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java 实现Redis存储复杂json格式数据并返回给前端

    Java 实现Redis存储复杂json格式数据并返回给前端

    这篇文章主要介绍了Java 实现Redis存储复杂json格式数据并返回给前端操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • SpringBoot中Controller参数与返回值的用法总结

    SpringBoot中Controller参数与返回值的用法总结

    这篇文章主要介绍了SpringBoot中Controller参数与返回值的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • AQS核心流程解析cancelAcquire方法

    AQS核心流程解析cancelAcquire方法

    可以清楚的看到在互斥锁和共享锁的拿锁过程中都是有调用此方法的,而cancelAcquire()方法是写在finally代码块中,并且使用failed标志位来控制cancelAcquire()方法的执行
    2023-04-04
  • SpringBoot整合Mybatis-Plus+Druid实现多数据源配置功能

    SpringBoot整合Mybatis-Plus+Druid实现多数据源配置功能

    本文主要讲解springboot +mybatisplus + druid 实现多数据源配置功能以及一些必要的准备及代码说明,具有一定的参考价值,感兴趣的小伙伴可以借鉴一下
    2023-06-06
  • Elasticsearch开发AtomicArray使用示例探究

    Elasticsearch开发AtomicArray使用示例探究

    这篇文章主要为大家介绍了Elasticsearch AtomicArray使用示例探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • java实现Rabbitmq延迟队列和惰性队列

    java实现Rabbitmq延迟队列和惰性队列

    本文主要介绍了java实现Rabbitmq延迟队列和惰性队列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12
  • Spring中@Conditional注解的用法

    Spring中@Conditional注解的用法

    这篇文章主要介绍了Spring中@Conditional注解的用法,@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean,需要的朋友可以参考下
    2024-01-01
  • 简单实现java数独游戏

    简单实现java数独游戏

    这篇文章主要教大家如何简单实现java数独游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 详解java解决分布式环境中高并发环境下数据插入重复问题

    详解java解决分布式环境中高并发环境下数据插入重复问题

    这篇文章主要介绍了java解决并发数据重复问题 ,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • 轻松掌握Java建造者模式

    轻松掌握Java建造者模式

    这篇文章主要帮助大家轻松掌握Java建造者模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10

最新评论