Jerdi使用和常用方法命令

三种连接Redis的方法

方法一:单机连接池连接

JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
JedisPool jedisPool=new JedisPool(jedisPoolConfig,"host","port",2000,"password");
Jedis jedis=jedisPool.getResource();
//关闭连接,将资源归还连接池
jedis.close();
jedisPool.close();

方法二:ShardedJedisPool 集群连接

//创建 poolConfig
JedisPoolConfig poolConfig=new JedisPoolConfig();
//设置 JedisShardInfo信息,host post password
JedisShardInfo jedisShardInfo1=new JedisShardInfo("host1","port1");
jedisShardInfo1.setPassword("password1");
JedisShardInfo jedisShardInfo2=new JedisShardInfo("host2","port2");
jedisShardInfo2.setPassword("password2");
JedisShardInfo jedisShardInfo3=new JedisShardInfo("host3","port3");
jedisShardInfo3.setPassword("password3");

List<JedisShardInfo> list= Arrays.asList(jedisShardInfo1,jedisShardInfo2,jedisShardInfo3);
//创建连接池 ShardedJedisPool
ShardedJedisPool jedisPool=new ShardedJedisPool(poolConfig,list);
//获取 jedis
ShardedJedis jedis=jedisPool.getResource();
//关闭连接,将资源归还连接池
jedis.close();
jedisPool.close();

方法三:JedisCluster 集群连接

//节点添加
Set<HostAndPort> nodes=new HashSet<HostAndPort>();
nodes.add(new HostAndPort("host1","port1"));
nodes.add(new HostAndPort("host2","port2"));
nodes.add(new HostAndPort("host3","port3"));
//创建 JedisCluster
JedisCluster jedis=new JedisCluster(nodes,2000,2000,5,"password",new JedisPoolConfig());
jedis.close();

JedisCluster与ShardedJedisPool的区别

https://blog.csdn.net/qq_31102103/article/details/89372143

 

posted @ 2020-05-29 10:19  hjy1995  阅读(219)  评论(0编辑  收藏  举报