首页  

Jedis的几种使用方式     所属分类 redis 浏览量 981
redis.clients:jedis:2.9.0


1 单节点简单连接 

public static Jedis createJedis(String host, int port, String passwrod) {
    Jedis jedis = new Jedis(host, port);
    if (StringUtils.isNotBlank(passwrod)){
        jedis.auth(passwrod);
    }
    return jedis;
}
  
2 创建连接池

JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(100);
config.setMaxWait(1000);
config.setMaxIdle(10);
JedisPool pool = new JedisPool(config, "127.0.0.1", 6379);

Jedis jedis = pool.getResource();
...
pool.returnResource(jedis);


3 客户端分片

Hash算法 MurmurHash2.0

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo("127.0.0.1", 6379);
si.setPassword("xxx");
shards.add(si);

si = new JedisShardInfo("127.0.0.1", 6389);
si.setPassword("xxx");
shards.add(si);

ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);



4 Jedis Cluster

Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
// Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 6379));
JedisCluster jc = new JedisCluster(jedisClusterNodes);
jc.set("foo", "bar");
String value = jc.get("foo");


上一篇     下一篇
springboot自动配置原理和实例

ConcurrentHashMap使用注意点

SpringCloud VS Dubbo

zookeeper节点属性中的三个zxid

Springboot注入ApplicationContext的几种方式

zookeeper四字命令conf结果说明