shardjedis采用一致hash算法实现key的分片,通过计算key的hash值将key分布到不同的redis服务器上,从而达到横向扩展的目的。
以下介绍shardjedis的常用操作。
- 配置shardjedispool
@Configuration
@ComponentScan(basePackages = "config")
public class ShardJedisPool {
@Autowired
RedisConfig redisConfig;
/**
*获取jedisinfo信息
* @return
*/
private List<JedisShardInfo> jedisShardInfos(){
String[] redisInfos = redisConfig.getRedisInfo().split(",");
List<JedisShardInfo> jedisShardInfos = new ArrayList<>();
for(String redisInfo :redisInfos){
String[] part = redisInfo.split(":");
jedisShardInfos.add(new JedisShardInfo(part[0],Integer.parseInt(part[1])));
}
return jedisShardInfos;
}
/**
* 设置redis的参数配置
* @return
*/
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig(){
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
genericObjectPoolConfig.setMaxTotal(redisConfig.getMaxTotal());
genericObjectPoolConfig.setMinIdle(redisConfig.getMinIdle());
genericObjectPoolConfig.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
return genericObjectPoolConfig;
}
/**
* 获取shardjedis连接池
* @param genericObjectPoolConfig
* @return
*/
@Bean
public ShardedJedisPool shardedJedisPool(GenericObjectPoolConfig genericObjectPoolConfig){
return new ShardedJedisPool(genericObjectPoolConfig,jedisShardInfos());
}
- 常用方法
@Component
public class JedisAction {
@Autowired
private ShardedJedisPool shardedJedisPool;
/**
* 获取string类型的value
* @param key
* @return
*/
public String get(String key){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()) {
return shardedJedis.get(key);
}
}
/**
* 设置string类型的值
* @param key
* @param value
*/
public void set(String key,String value){
try (ShardedJedis shardedJedis = shardedJedisPool.getResource()){
shardedJedis.set(key,value);
}
}
/**
* 删除key
* @param key
*/
public void del(String key){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()){
shardedJedis.del(key);
}
}
/**
* 判断key是否存在
* @param key
* @return
*/
public boolean exits(String key){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()){
return shardedJedis.exists(key);
}
}
/**
* 获取key的类型
* @param key
* @return
*/
public String type(String key){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()){
return shardedJedis.type(key);
}
}
/**
* 设置key的过期时间
* @param key
* @param seconds
*/
public void expire(String key,final int seconds){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()){
shardedJedis.expire(key,seconds);
}
}
/**
* 根据key的模糊类型获取所有的key
* @param pattern
* @return
*/
public List<String> keys(String pattern){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()){
Collection<Jedis> allShards = shardedJedis.getAllShards();
List<String> keyList = new ArrayList<>();
for(Jedis jedis:allShards){
Set<String> keys = jedis.keys(pattern);
keyList.addAll(keys);
}
return keyList;
}
}
/**
* 获取所有匹配模糊key的value值
* @param pattern
* @return
*/
public List<String> getValues(String pattern){
try(ShardedJedis shardedJedis = shardedJedisPool.getResource()){
Collection<Jedis> allShards = shardedJedis.getAllShards();
List<String> values = new ArrayList<>();
for(Jedis jedis:allShards){
Set<String> keys = jedis.keys(pattern);
for(String key:keys){
values.add(getValue(jedis,key));
}
}
return values;
}
}
/**
* 不区分key的类型获取key的value,可以匹配string,list,hash,set四种类型
* @param jedis
* @param key
* @return
*/
public String getValue(Jedis jedis,String key){
String type = jedis.type(key);
GetValue value = GetValue.valueOf(type.toUpperCase());
return value.getValue(jedis,key);
}
}
- 常用的key类型,用于辅助不区分key类型获取key的value值
public enum GetValue {
LIST(){
@Override
public String getValue(Jedis jedis,String key) {
List<String> list = jedis.lrange(key, 0, jedis.llen(key));
return "key="+key+"|type=list|value="+toString(list);
}
},
HASH(){
@Override
public String getValue(Jedis jedis, String key) {
Set<String> keys = jedis.hkeys(key);
return "key="+key+"|type=hash|value="+toString(keys);
}
},
SET(){
@Override
public String getValue(Jedis jedis, String key) {
Set<String> smembers = jedis.smembers(key);
return "key="+key+"|type=set|value="+toString(smembers);
}
},
STRING(){
@Override
public String getValue(Jedis jedis, String key) {
return "key="+key+"|type=string|value="+jedis.get(key);
}
};
public abstract String getValue(Jedis jedis,String key);
String toString(Collection<String> collection){
StringBuilder sb = new StringBuilder();
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()){
sb.append(iterator.next()).append(",");
}
return sb.substring(0,sb.length()-1);
}
}
- 获取redis配置类
@Component
@Data
@PropertySource("config/properties")
public class RedisConfig {
@Value("${redis.info}")
private String redisInfo;
@Value("${redis.pool.maxTotal}")
private int maxTotal;
@Value("${redis.pool.maxIdle}")
private int maxIdle;
@Value("${redis.pool.minIdle}")
private int minIdle;
@Value("${redis.pool.maxWaitMillis}")
private int maxWaitMillis;
}
- redis配置
#redis实例
redis.info=192.168.127.128:6379,192.168.127.128:6380
#redis最大连接数
redis.pool.maxTotal=50
redis.pool.maxIdle=20
redis.pool.minIdle=10
redis.pool.maxWaitMillis=1000