java客户端Jedis操作Redis Sentinel 连接池的实现方法
作者:jingxian 发布时间:2023-08-19 10:55:19
标签:jedis,sentinel,客户端,redis
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();
}
}
0
投稿
猜你喜欢
- AsyncTask什么是AsyncTaskAsyncTask是一个轻量级的异步任务类,它可以在线程池中执行后台任务,然后把执行的进度和结果传
- Jackson解析嵌套类(MismatchedInputException)具体报错如下问题描述:Jackson解析嵌套类问题 调
- 1.多数据源配置类整体项目结构1).pom.xml 项目依赖<?xml version="1.0" encodin
- 这篇文章主要介绍了Java import导入及访问控制权限修饰符过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 无论哪种界面框架输入文本框都是非常重要的控件, 但是发现flutter中的输入框TextField介绍的虽然多,但是各个属性怎么组合满足需要
- 一、写在前面数据结构中的队列应该是比较熟悉的了,就是先进先出,因为有序故得名队列,就如同排队嘛,在对尾插入新的节点,在对首删除节点.jdk集
- 一. Window 分类应用 Window(ApplicationWindow: 对应一个 Acitivity)
- 在并发多线程的情况下,为了保证数据安全性,一般我们会对数据进行加锁,通常使用Synchronized或者ReentrantLock同步锁。S
- 前言Kotlin一个强大之处就在于它的扩展函数,巧妙的运用这些扩展函数可以让你写出的代码更加优雅,阅读起来更加流畅,下面总结了在开发中经常用
- 这篇会深化View拖拽实例,利用Flutter Animation、插值器以及AnimatedBuilder教大家实现带动画的抽屉效果。先来
- 在Java中如果一个类同时继承接口A与B,并且这两个接口中具有同名方法,会怎么样?动手做实验:interface A{ void
- 1)首先启动hadoop2个进程,进入hadoop/sbin目录下,依次启动如下命令[root@node02 sbin]# pwd/usr/
- 一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: << string name=”OK”&g
- 上篇介绍了几种图表的公共组件X、Y轴、背景Board的绘制。这章节介绍柱状图表的绘制,相对其它图表而言简单一些,这里主要介绍图表主体的绘制,
- 前言虽然从学java的第一个程序——helloworld至今,已经有好几个年头了。当时自己找资料,看视频,学习了java的输入输出流,多线程
- 合并有序数组的实现java版本:实例代码public class Merge {//合并有序数组 public static v
- 记录一下微信第三方实现登录的方法。还是比较简单。一、必要的准备工作1.首先需要注册并被审核通过的微信开放平台帐号,然后创建一个移动应用,也需
- 详解java.lang.reflect.Modifier.isInterface()方法java.lang.reflect.Modifier
- 开发环境win10Android Studio效果用于多级菜单展示,或选择。如 每个省,市,县;如 树木的病虫害;关键代码 @overrid
- 在实际应用中,我们往往有需要比较两个自定义对象大小的地方。而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我