Jedis配置

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客户端软件
  1. Redis Desktop Manager

    支持: Windows 7+, Mac OS X 10.10+, Ubuntu 14+
    特点: C++ 编写,响应迅速,性能好。但不支持数据库备份与恢复。
    项目地址: https://github.com/uglide/RedisDesktopManager

  2. Redis Client

    项目简介: 使用Java编写,功能丰富,缺点是性能稍差,网络不好时,会不时断线。
    项目地址: https://github.com/caoxinyu/RedisClient

  3. Redis Studio

    项目简介: 又一个C++编写的redis管理工具,仅支持windows平台,支持xp操作系统。
    项目地址: https://github.com/cinience/RedisStudio

  4. AnotherRedisDesktopManager

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值