java客户端Jedis操作Redis Sentinel 连接池
pom配置:
<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>
java代码:
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 连接池的更多相关文章
- Redis入门和Java利用jedis操作redis
Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...
- Java中Jedis操作Redis与Spring的整合
Redis是一个key-value存储系统.它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop. ...
- Java使用Jedis操作Redis大全
Java操作Redis需要导入两个jar: commons-pool2-2.4.2.jar jedis-2.1.0.jar package com.chinasofti.test; import ja ...
- Java通过jedis操作redis缓存
package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...
- Java通过jedis操作redis(增删改查)
package sgh.main.powersite; import java.util.ArrayList; import java.util.HashMap; import java.util.I ...
- jedis操作redis的几种常见方式总结
Redis是一个著名的key-value存储系统,也是nosql中的最常见的一种,这篇文章主要给大家总结了关于在java中jedis操作redis的几种常见方式,文中给出了详细的示例代码供大家参考学习 ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- JAVA中通过Jedis操作Redis连接与插入简单库
一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...
- 使用Redis的Java客户端Jedis
转载自:http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 前一篇文章<Redis命令指南>讲解了通过命令行 ...
随机推荐
- mosquitto安装和测试
一.安装 1.windows安装 安装完毕,更新安装目录的dll文件 2.linux安装 编译保存用户数据到数据库的插件 安装 3.启动 mosquitto mosquitto mosquitto_p ...
- python Django之Form组件
python Django之Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试 ...
- flask +gevent+nginx+Gunicorn+supervisor部署flask应用
上篇 可以完美部署flask ,但是视乎在结合gevent+apscheduler 实现异步非阻塞后台和定时任务的时候视乎不是那么完美.请教了前辈,决定使用flask+gevent+nginx+g ...
- js正则表达式入门以及常见用例
学习正则表达式的最好方法是从例子开始,理解例子之后再自己对例子进行修改,实验.下面给出了不少简单的例子,并对它们作了详细的说明. 假设你在一篇英文小说里查找hi,你可以使用正则表达式hi. 这几乎是最 ...
- Batch训练的反向传播过程
Batch训练的反向传播过程 本文试图通过Softmax理解Batch训练的反向传播过程 采用的网络包含一层全连接和一层softmax,具体网络如下图所示: 交叉熵成本函数: \[L = - \fra ...
- [LeetCode] Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- MQTT客户端库-Paho GO
为了加深理解,本文是翻译文章.原文地址 Paho GO Client 语言 GO 协议 EPL AND EDL 官网地址 http://www.eclipse.org/paho/ API类型 As ...
- 页面中引入mui 地址选择,点击页面中其他input时页面回到顶部
问题:在页面中引入mui地址选择时,点击页面中的input页面会滚到顶部(谷歌浏览器中出现的bug),在手机上点击input会出现跳动.开始的时候是想修改mui.min.js里的滚动事件,但是后来找到 ...
- [Luogu 3835]【模板】可持久化平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作(对于各个以往的历史版本): 插入x数 删除x数(若有多个相同的数,因只删除一个,如果没有请忽略该操作 ...
- [BeiJing2011]元素
Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔 法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石. 一般地,矿石越多则法力 ...