您好,登錄后才能下訂單哦!
這篇文章給大家介紹java客戶端中如何使用Jedis實(shí)現(xiàn)操作Redis Sentinel 連接池,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
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; } /** * 創(chuàng)建連接池 * */ private static void createJedisPool() { // 建立連接池配置參數(shù) JedisPoolConfig config = new JedisPoolConfig(); Properties prop = getJedisProperties(); // 設(shè)置最大連接數(shù) config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE"))); // 設(shè)置最大阻塞時(shí)間,記住是毫秒數(shù)milliseconds config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT"))); // 設(shè)置空間連接 config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE"))); // jedis實(shí)例是否可用 boolean borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? false : true; config.setTestOnBorrow(borrow); // 創(chuàng)建連接池 // pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 線程數(shù)量限制,IP地址,端口,超時(shí)時(shí)間 //獲取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); } /** * 在多線程環(huán)境同步初始化 */ private static synchronized void poolInit() { if (pool == null) createJedisPool(); } /** * 獲取一個(gè)jedis 對(duì)象 * * @return */ public static Jedis getJedis() { if (pool == null) poolInit(); return pool.getResource(); } /** * 釋放一個(gè)連接 * * @param jedis */ public static void returnRes(Jedis jedis) { pool.returnResource(jedis); } /** * 銷毀一個(gè)連接 * * @param jedis */ public static void returnBrokenRes(Jedis jedis) { pool.returnBrokenResource(jedis); } public static void main(String[] args){ Jedis jedis=getJedis(); } }
關(guān)于java客戶端中如何使用Jedis實(shí)現(xiàn)操作Redis Sentinel 連接池就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。