java客戶端Jedis操作Redis Sentinel 連接池的實(shí)現(xiàn)方法
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(); } }
以上這篇java客戶端Jedis操作Redis Sentinel 連接池的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何獲取src/main/resource路徑下的文件
這篇文章主要介紹了SpringBoot如何獲取src/main/resource路徑下的文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Spring使用注解進(jìn)行對(duì)象注入的示例詳解
獲取?Bean?對(duì)象也叫做對(duì)象裝配,就是把對(duì)象取出來(lái)放到某個(gè)類中,有時(shí)候也叫對(duì)象注入,常見(jiàn)有關(guān)對(duì)象注入的注解有兩個(gè),一個(gè)是@Autowired,另外一個(gè)是@Resource,下面就來(lái)講講它們的具體使用吧2023-07-07java?Export大量數(shù)據(jù)導(dǎo)出和打包
這篇文章主要為大家介紹了java?Export大量數(shù)據(jù)的導(dǎo)出和打包實(shí)現(xiàn)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06解決grails服務(wù)端口沖突的辦法(grails修改端口號(hào))
grails中默認(rèn)的服務(wù)端口為8080,當(dāng)本機(jī)中需要同時(shí)啟動(dòng)兩個(gè)不同的項(xiàng)目時(shí),就會(huì)造成端口沖突,下面給出解決方法2013-12-12解決RestTemplate反序列化嵌套對(duì)象的問(wèn)題
這篇文章主要介紹了解決RestTemplate反序列化嵌套對(duì)象的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java實(shí)現(xiàn)讀取鍵盤輸入保存到txt文件,再統(tǒng)計(jì)并輸出每個(gè)單詞出現(xiàn)次數(shù)的方法
這篇文章主要介紹了Java實(shí)現(xiàn)讀取鍵盤輸入保存到txt文件,再統(tǒng)計(jì)并輸出每個(gè)單詞出現(xiàn)次數(shù)的方法,涉及java文件I/O操作及字符串遍歷、運(yùn)算實(shí)現(xiàn)統(tǒng)計(jì)功能相關(guān)技巧,需要的朋友可以參考下2017-07-07java中的Io(input與output)操作總結(jié)(三)
這一節(jié)我們來(lái)講Scanner類和PrintWriter類的用法,感興趣的朋友可以了解下2013-01-01