• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java客户端Jedis操作Redis Sentinel实现连接池的代码分享

java 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

下面小编就为大家带来一篇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> ?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.ge<i>本文来源gaodai$ma#com搞$$代**码)网@</i>tProperty("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实现连接池的代码分享的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java客户端Jedis操作Redis Sentinel实现连接池的代码分享
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址