广告
返回顶部
首页 > 资讯 > 精选 >java客户端中如何使用Jedis实现操作Redis Sentinel 连接池
  • 677
分享到

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

jedissentineljava 2023-05-31 16:05:35 677人浏览 安东尼
摘要

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

这篇文章给大家介绍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;123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 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();   }      public static Jedis getJedis() {     if (pool == null)       poolInit();     return pool.getResource();   }      public static void returnRes(Jedis jedis) {     pool.returnResource(jedis);   }      public static void returnBrokenRes(Jedis jedis) {     pool.returnBrokenResource(jedis);   }         public static void main(String[] args){     Jedis jedis=getJedis();        }  } 

--结束END--

本文标题: java客户端中如何使用Jedis实现操作Redis Sentinel 连接池

本文链接: https://www.lsjlt.com/news/227259.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作