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中实体类转Json的2种方法

    java中实体类转Json的2种方法

    本篇文章中主要介绍了java中实体类转Json的2种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
    2017-01-01
  • SSM整合中的Log4j日志的配置详情

    SSM整合中的Log4j日志的配置详情

    这篇文章主要介绍了SSM整合中的Log4j的配置详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Java设计模式之工厂模式(Factory模式)介绍

    Java设计模式之工厂模式(Factory模式)介绍

    这篇文章主要介绍了Java设计模式之工厂模式(Factory模式)介绍,本文讲解了为何使用工厂模式、工厂方法、抽象工厂、Java工厂模式举例等内容,需要的朋友可以参考下
    2015-03-03
  • Spring Boot 中的自动配置autoconfigure详解

    Spring Boot 中的自动配置autoconfigure详解

    这篇文章主要介绍了Spring Boot 中的自动配置autoconfigure详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • 浅谈用java实现事件驱动机制

    浅谈用java实现事件驱动机制

    这篇文章主要介绍了浅谈用java实现事件驱动机制,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • TOMCAT内存溢出及大小调整的实现方法

    TOMCAT内存溢出及大小调整的实现方法

    下面小编就为大家带来一篇TOMCAT内存溢出及大小调整的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • 基于@Table注解无法使用及报红的解决

    基于@Table注解无法使用及报红的解决

    这篇文章主要介绍了基于@Table注解无法使用及报红的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • SpringBoot配置类中@Configuration和@Bean的作用

    SpringBoot配置类中@Configuration和@Bean的作用

    这篇文章主要介绍了SpringBoot配置类中@Configuration和@Bean的作用,@Configuration 指明当前类是一个配置类来替代之前的Spring配置文件,Spring boot的配置类,相当于Spring的配置文件,需要的朋友可以参考下
    2023-11-11
  • 详解mybatis-plus的 mapper.xml 路径配置的坑

    详解mybatis-plus的 mapper.xml 路径配置的坑

    这篇文章主要介绍了详解mybatis-plus的 mapper.xml 路径配置的坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • java正则实现各种日期格式化

    java正则实现各种日期格式化

    本文给大家分享的是使用java结合正则表达式来实现各种日期的格式化功能,代码非常的简单,有需要的小伙伴可以参考下。
    2015-05-05

最新评论