一、配置文件

1. db.properties配置文件
#IP地址
redis.ip = 127.0.0.1
#端口号
redis.port=
#最大连接数
redis.max.total=
#最大空闲数
redis.max.idle=
#最小空闲数
redis.min.idle=
#效验使用可用连接
redis.test.borrow=true
#效验归还可用连接
redis.test.return=false 2. pom.xml文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
 

二、java代码

public class RedisPool {
private static JedisPool pool ; //jedis连接池
private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total","")); //最大连接数
private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle","")); //最大空闲状态
private static Integer minIdle =Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle","")); //最小空闲状态 private static Boolean testOnBorrow =Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow","true")); //验证从连接池拿出的jedis实例,一定可用
private static Boolean testOnReturn =Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return","true")); //验证还回连接池的jedis实例,一定可用 private static String redisIp =PropertiesUtil.getProperty("redis.ip"); //最小空闲状态
private static Integer redisPort =Integer.parseInt(PropertiesUtil.getProperty("redis.port")); //最小空闲状态 private static void initPool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle); config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn); config.setBlockWhenExhausted(true); //连接耗尽时是否阻塞,false抛出异常;true阻塞到超时。默认true pool = new JedisPool(config,redisIp,redisPort,*);
} static{
initPool();
} public static Jedis getJedis(){
return pool.getResource();
} /**
* redis不正常不可用,将其废弃,最新版本直接将此连接销毁jedis.close();
* @param jedis
*/
public static void returnBrokenResource(Jedis jedis){
pool.returnBrokenResource(jedis); //最新版本已废弃
} public static void returnResource(Jedis jedis){
pool.returnResource(jedis); //最新版本已废弃
}
}

三。redis相关类介绍

  (1)JedisPool

    JedisPool保证资源在一个可控范围内,并且提供了线程安全,Jedis连接就是资源,JedisPool管理的就是Jedis连接。此类只有三个方法普通方法,大量的构造器,构造器根据不同的连接配置信息,生成对应的资源池。可以很好地重复利用Jedis,减少new的次数,从而提高效率。

  (2)JedisPoolConfig

    资源池的配置信息,JedisPoolConfig只有一个构造器,大部分配置信息都在继承的类GenericObjectPoolConfigBaseObjectPoolConfig中。配置最大连接数,最大/小空闲等待数,是否效验生成/归还的连接有效等等。JedisPool其中JedisPoolConfig即是JedisPool构造器所要出入的配置对象,根据JedisPoolConfig配置信息,进行资源池管理。

基本配置如下:

setBlockWhenExhausted(boolean blockWhenExhausted)
当池中的资源耗尽时是否进行阻塞,设置false直接报错,true表示会一直等待,直到有可用资源 setEvictionPolicyClassName(String evictionPolicyClassName)
设置逐出策略,默认策略为
"org.apache.commons.pool2.impl.DefaultEvictionPolicy" setFairness(boolean fairness)
当从池中获取资源或者将资源还回池中时 是否使用java.util.concurrent.locks.ReentrantLock.ReentrantLock 的公平锁机制,默认为false setJmxEnabled
设置是否启用JMX,默认true setJmxNameBase(String jmxNameBase)
设置JMX基础名 setJmxNamePrefix(String jmxNamePrefix)
设置JMX前缀名,默认值pool setLifo(boolean lifo)
设置连接对象是否后进先出,默认true setMaxIdle(int maxIdle)
设置最大空闲连接数,默认为8 setMaxTotal(int maxTotal)
设置最大连接数,默认18个 setMaxWaitMillis(long maxWaitMillis)
获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认- setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
设置连接最小的逐出间隔时间,默认1800000毫秒 setMinIdle(int minIdle)
设置无连接时池中最小的连接个数,默认连接0 setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
每次逐出检查时,逐出连接的个数 setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 setTestOnBorrow(boolean testOnBorrow)
从池中获取连接时是否测试连接的有效性,默认false setTestOnCreate(boolean testOnCreate)
在连接对象创建时测试连接对象的有效性,默认false setTestOnReturn(boolean testOnReturn)
在连接对象返回时,是否测试对象的有效性,默认false setTestWhileIdle(boolean testWhileIdle)
在连接池空闲时是否测试连接对象的有效性,默认false setTimeBetweenEvictionRunsMillis(
long timeBetweenEvictionRunsMillis)
设置连接对象有效性扫描间隔,设置为-,则不运行逐出线程

  (3)Jedis

  Jedis是Redis官方推荐的Java连接开发工具。要在Java开发中使用好Redis中间件。Jedis有大量的方法,都是进行redis数据库的crud操作。Jedis是有JedisPool连接池创建的。

import com.mmall.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis; /**
* 封装单机redis常用API方法
*/
@Slf4j
public class RedisPoolUtil { /**
* 设置对应key的有效期
* @param key
* @param exTime 有效期,单位秒
* @return
*/
public static Long expire(String key, int exTime){
Jedis jedis = null;
Long result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.expire(key,exTime);
}catch (Exception e){
log.error("set key:{} exTime:{} value:{} error",key,exTime,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* string 添加,存在有效期exTime
* @param key 键
* @param value 值
* @param exTime 有效期,单位秒
* @return
*/
public static String setEx(String key, String value, int exTime){
Jedis jedis = null;
String result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.setex(key,exTime,value);
}catch (Exception e){
log.error("set key:{} exTime:{} value:{} error",key,exTime,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* string 添加
* @param key
* @param value
* @return
*/
public static String set(String key, String value){
Jedis jedis = null;
String result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.set(key,value);
}catch (Exception e){
log.error("set key:{} value:{} error",key,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* string 获取
* @param key
* @return
*/
public static String get(String key){
Jedis jedis = null;
String result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.get(key);
}catch (Exception e){
log.error("get key:{} error",key,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* stirng 删除
* @param key
* @return
*/
public static Long del(String key){
Jedis jedis = null;
Long result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.del(key);
}catch (Exception e){
log.error("get key:{} error",key,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
}

redis单机连接池的更多相关文章

  1. redis运用连接池报错解决

    redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...

  2. nodejs + redis/mysql 连接池问题

    nodejs + redis/mysql 连接池问题 需不需要连接池 连接池的作用主要是较少每次临时建立连接所带来的开销.初步一看,nodejs运行单线程上,它不能同时使用多个连接,乍一看是不需要连接 ...

  3. Redis Java连接池调研

    Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...

  4. python redis之连接池的原理

    python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...

  5. Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题。(转)

    环境 springmvc jdk1.8 maven redis.properties配置文件 #redis setting redis.host=localhost redis.port=6379 r ...

  6. Redis缓存连接池管理

    import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.Assert;import ...

  7. java客户端Jedis操作Redis Sentinel 连接池

    pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

  8. redis mysql 连接池 之 golang 实现

    1 mysql 连接池代码 package lib import ( "database/sql" "fmt" "strconv" &quo ...

  9. python操作Redis安装、支持存储类型、普通连接、连接池

    一.python操作redis安装和支持存储类型 安装redis模块 pip3 install redis 二.Python操作Redis之普通连接 redis-py提供两个类Redis和Strict ...

随机推荐

  1. java 内部类简单总结

    在java中,一个类可以放在另一个类的内部,称之为内部类,相对而言,包含它的类称之为外部类.不过对于Java虚拟机而言,它是不知道内部类这回事的, 每个内部类最后都会被编译为一个独立的类,生成一个独立 ...

  2. 更改用户id 和组id

    转自 http://blog.csdn.net/todd911/article/details/16370577 在unix系统中,特权是基于用户和组ID的,当程序需要增加特权,或需要访问当前并不允许 ...

  3. python 安装 pyHook

    下载网站:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook 查看python版本 C:\Users\k\Desktop\file_trans> ...

  4. Spring Cloud Gateway(一):认识Spring Cloud Gateway

    1.Spring Cloud Gateway 简介 Spring Cloud Gateway 系列目录 Spring Cloud Gateway(一):认识Spring Cloud Gateway S ...

  5. java跨域配置

    一.问题 使用前后端分离模式开发项目时,往往会遇到这样一个问题 -- 无法跨域获取服务端数据 这是由于浏览器的同源策略导致的,目的是为了安全.在前后端分离开发模式备受青睐的今天,前端和后台项目往往会在 ...

  6. 【SVN】彻底 svn 服务器上的 删除某一个文件或文件夹

    参考: CSDN1:https://blog.csdn.net/u011729865/article/details/78764523 CSDN2:https://blog.csdn.net/wyyo ...

  7. KVM——以桥接的方式搭建虚拟机网络配置

    以桥接的方式搭建虚拟机网络,其优势是可以将网络中的虚拟机看作是与主机同等地位的服务器. 在原本的局域网中有两台主机,一台是win7(IP: 192.168.0.236),一台是CentOS7(IP: ...

  8. jQuery源码解读----part 1

    来源:慕课网 https://www.imooc.com/video/4392 jQuery整体架构 jQuery按我的理解分为五大块,选择器.DOM操作.事件.AJAX与动画, 那么为什么有13个模 ...

  9. kotlin嵌套类

    就是类中定义类 package loaderman.demo class Outer { var name: String = "name" inner class inner { ...

  10. 19.网络插件calico

    19.网络插件calico 官网: https://docs.projectcalico.org/v3.8/introduction/ calico默认工作在192.168.0.0/16 的网络 ca ...