java客户端Jedis操作Redis Sentinel 连接池的实现方法
作者:jingxian 时间:2023-08-19 10:55:19
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.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();
}
}
标签:jedis,sentinel,客户端,redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用RestClient调用Web API
2022-05-30 04:13:09
SpringCloud搭建netflix-eureka微服务集群的过程详解
2023-09-02 18:11:52
![](https://img.aspxhome.com/file/2023/3/58293_0s.jpg)
java实现上传图片尺寸修改和质量压缩
2023-04-04 03:42:55
重新启动IDEA时maven项目SSM框架文件变色所有@注解失效
2021-12-08 06:42:23
![](https://img.aspxhome.com/file/2023/5/94745_0s.jpg)
Java利用递归算法实现查询斐波那契数
2023-08-04 00:02:29
![](https://img.aspxhome.com/file/2023/4/119064_0s.png)
Android仿qq顶部消息栏效果
2021-10-28 13:52:57
![](https://img.aspxhome.com/file/2023/1/103901_0s.jpg)
java微信公众号企业付款开发
2023-04-07 00:21:08
![](https://img.aspxhome.com/file/2023/1/88041_0s.jpg)
Java函数式编程(十二):监控文件修改
2022-08-11 10:21:01
j2ee之AJAX二级联动效果
2021-09-13 10:06:58
C#实现冒泡排序和插入排序算法
2021-07-18 17:01:53
C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题示例
2021-10-05 16:28:14
![](https://img.aspxhome.com/file/2023/2/89842_0s.png)
Java实现解出世界最难九宫格问题
2022-06-14 19:47:10
C#判断访问来源是否为搜索引擎链接的方法
2021-11-01 09:15:53
C#中实现AES算法加密解读
2022-09-17 16:49:36
![](https://img.aspxhome.com/file/2023/7/80107_0s.png)
Eclipse快速添加get、set方法的操作技巧
2022-11-20 04:11:59
![](https://img.aspxhome.com/file/2023/7/78937_0s.jpg)
OpenCV图像旋转Rotate的详细介绍
2023-07-01 08:22:27
![](https://img.aspxhome.com/file/2023/2/95522_0s.jpg)
Android利用二阶贝塞尔曲线实现添加购物车动画详解
2022-03-01 09:49:49
![](https://img.aspxhome.com/file/2023/0/138540_0s.gif)
C# List实现行转列的通用方案
2022-03-28 02:53:04
![](https://img.aspxhome.com/file/2023/4/75614_0s.png)
SpringCloud Gateway使用详解
2023-11-27 02:54:36
基于JDK动态代理原理解析
2022-07-24 19:43:33