Jedis介紹
Jedis源碼。
Jedis是redis 的java客戶端。
Jedis的連接池是common-pool2。
使用
構建JedisPool對象
jedispool的構造函數有很多,最終都調用了他的父類redis.clients.util.Pool的構造函數:
public Pool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
this.initPool(poolConfig, factory);
}
public void initPool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
if (this.internalPool != null) {
try {
this.closeInternalPool();
} catch (Exception var4) {
}
}
this.internalPool = new GenericObjectPool(factory, poolConfig);
}
可以看到,最終裝載的是common-pool2包下的GenericObjectPool類對象。其中要傳入兩個參數GenericObjectPoolConfig 、PooledObjectFactory:
GenericObjectPoolConfig
是jedis默認配置類,該類在common-pool2包下。使用這個類是用來配置連接池的一些屬性。部分常用屬性如下:
public class GenericObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
private int maxTotal = 8;
private int maxIdle = 8;
private int minIdle = 0;
public GenericObjectPoolConfig() {
}
...
}
public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Cloneable {
...
private long maxWaitMillis = -1L;
private boolean lifo = true;
private boolean blockWhenExhausted = true;
private boolean testOnCreate = false;
private boolean testOnBorrow=false;
private boolean testOnReturn = false;
private boolean testWhileIdle = false;
...
}
- maxTotal :連接池中最大連接數,默認爲 8。
- maxIdle:連接池中最大空閒連接數,默認爲 8。
- minIdle:連接池中最小空閒連接數,默認爲 0。
- maxWaitMillis:等待空閒連接的最大時間,默認爲 -1,超過最大時間直接報連接異常。
- lifo (last in,first out):後進先出,默認爲true
- blockWhenExhausted :對象池耗盡之後是否阻塞,默認爲 true。
- testOnCreate :創建對象時驗證,默認爲false。
- testOnBorrow:出借對象時驗證,默認爲false。
- testOnReturn :回收對象時驗證,默認爲false。
- testWhileIdle :空閒驗證,默認爲false。
PooledObjectFactory
該類在common-pool2包下,JedisPool初始化的時候傳入是PooledObjectFactory的子類JedisFactory。
該類是用來創建池內對象的,一些成員屬性是用來配置連接的。
class JedisFactory implements PooledObjectFactory<Jedis> {
private final AtomicReference<HostAndPort> hostAndPort = new AtomicReference();
private final int connectionTimeout;
private final int soTimeout;
private final String password;
private final int database;
private final String clientName;
private final boolean ssl;
private final SSLSocketFactory sslSocketFactory;
private SSLParameters sslParameters;
private HostnameVerifier hostnameVerifier;
...
public void activateObject(PooledObject<Jedis> pooledJedis) throws Exception {
...
}
public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {
...
}
public PooledObject<Jedis> makeObject() throws Exception {
...
}
public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception {
}
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
...
}
}
- hostAndPort :redis服務器的域名和端口號
- connectionTimeout:redis服務器的連接超時時間
- soTimeout(socket TimeOut):讀取數據超時
- password:redis服務器密碼
- database:數據庫
- clientName:連接對象名稱
- ssl:是否添加ssl驗證,默認 false。
- sslSocketFactory:ssl驗證
- sslParameters:ssl驗證參數
- hostnameVerifier:主機名驗證
- activateObject:激活對象
- destroyObject:銷燬對象
- makeObject:創建對象
- passivateObject:鈍化對象
- validateObject:驗證對象
案例:
redis:
pool:
host: localhost
port: 6379
max-idle: 100
min-idle: 5
maxTotal: 650
testOnBorrow: true
blockWhenExhausted: false
testOnReturn: true
max-wait: -1
@Component
@ConfigurationProperties(prefix = "redis.pool")
@Configuration
@Data
public class JedisConfig {
private String host;
private int port;
private Integer maxTotal;
private Integer maxIdle;
private Integer minIdle;
private Long maxWait;
private boolean blockWhenExhausted;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean testWhileIdle;
@Bean
public JedisPool getJedisPool() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
config.setMinIdle(minIdle);
config.setBlockWhenExhausted(blockWhenExhausted);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
config.setTestWhileIdle(testWhileIdle);
JedisPool jedisPool = new JedisPool(config, host, port);
return jedisPool;
}
}
@Component
public class RedisManger {
@Autowired
private JedisPool jedisPool;
private RedisManger() {
}
public String get(String key) {
try {
return jedisPool.getResource().get(key);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String set(String key, String value) {
try {
return jedisPool.getResource().set(key, value);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
幾款redis客戶端軟件
-
Redis Desktop Manager
支持: Windows 7+, Mac OS X 10.10+, Ubuntu 14+
特點: C++ 編寫,響應迅速,性能好。但不支持數據庫備份與恢復。
項目地址: https://github.com/uglide/RedisDesktopManager -
Redis Client
項目簡介: 使用Java編寫,功能豐富,缺點是性能稍差,網絡不好時,會不時斷線。
項目地址: https://github.com/caoxinyu/RedisClient -
Redis Studio
項目簡介: 又一個C++編寫的redis管理工具,僅支持windows平臺,支持xp操作系統。
項目地址: https://github.com/cinience/RedisStudio