Jedis的几种使用方式
所属分类 redis
浏览量 1147
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 shards = new ArrayList();
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 jedisClusterNodes = new HashSet();
// 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");
5 jedis4 连接哨兵
import redis.clients.jedis.JedisSentinelPool ;
import java.util.HashSet ;
import java.util.Set ;
static final String Sentinels = "127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381";
static final String MasterName = "mymaster";
static final String Password = "my sentinel password";
JedisSentinelPool pool = new JedisSentinelPool(MasterName,
new HashSet<>(Arrays.asList (Sentinels.split (","))),Password);
Jedis jedis = pool.getResource ();
System.out.println ("Value: " + jedis.get ("key"));
上一篇
下一篇
springboot自动配置原理和实例
ConcurrentHashMap使用注意点
SpringCloud VS Dubbo
zookeeper节点属性中的三个zxid
Springboot注入ApplicationContext的几种方式
zookeeper四字命令conf结果说明