总结redis的连接方式
来源:互联网 发布:java实用教程第4版答案 编辑:程序博客网 时间:2024/06/06 14:19
jedis客户端操作redis主要三种模式:单台模式、分片模式(ShardedJedis)、含有哨兵的分片模式(JedisSentinel)、集群模式(BinaryJedisCluster),分片模式是一种轻量级集群。
1、单台模式:直接通过jedis获取实例就可以
2、分片模式:
JedisPoolConfig config =new JedisPoolConfig();config.setMinIdle(minIdel);config.setMaxIdle(intMaxIdel);config.setMaxTotal(maxSize);config.setMaxWaitMillis(readTimeOut);config.setMaxIdle(intMaxIdel);config.setTestWhileIdle(checkAlive);ShardedJedisPool sharedPool = new ShardedJedisPool(config,jdsInfoList);‘sharedPool.getResource();3、含有哨兵的分片模式
由于程序连接配置了主服务器的连接,但是此时主服务器挂调,哨兵会将从服务器变成主服务器,但程序是不知道的,出现无法连接异常。Jedis提供了对jedis sentinel pool的封装,所以发生主从切换的时候,web server都不需要重新配置和deploy,对客户端完全透明。高可用性的极佳体现啊。
import org.junit.Test;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.JedisSentinelPool;public class TestSentinel { @Test public void test1() { JedisPoolConfig poolConfig = new JedisPoolConfig(); String masterName = "mymaster"; Set<String> sentinels = new HashSet<String>(); sentinels.add("192.168.1.97:26379"); sentinels.add("192.168.1.96:26379"); JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, poolConfig); HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster(); System.out.println(currentHostMaster.getHost()+"--"+currentHostMaster.getPort());//获取主节点的信息 Jedis resource = jedisSentinelPool.getResource(); String value = resource.get("a"); System.out.println(value);//获得键a对应的value值 resource.close(); }}4、集群模式
import java.util.HashSet;import java.util.Set;import org.junit.Test;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPoolConfig;public class TestCluster { @Test public void test1() throws Exception { JedisPoolConfig poolConfig = new JedisPoolConfig(); Set<HostAndPort> nodes = new HashSet<HostAndPort>(); HostAndPort hostAndPort = new HostAndPort("192.168.1.99", 7000);//只给集群里一个实例就可以 HostAndPort hostAndPort1 = new HostAndPort("192.168.1.99", 7001); HostAndPort hostAndPort2 = new HostAndPort("192.168.1.99", 7002); HostAndPort hostAndPort3 = new HostAndPort("192.168.1.99", 7003); HostAndPort hostAndPort4 = new HostAndPort("192.168.1.99", 7004); HostAndPort hostAndPort5 = new HostAndPort("192.168.1.99", 7005); nodes.add(hostAndPort); nodes.add(hostAndPort1); nodes.add(hostAndPort2); nodes.add(hostAndPort3); nodes.add(hostAndPort4); nodes.add(hostAndPort5); JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);//JedisCluster中默认分装好了连接池. //redis内部会创建连接池,从连接池中获取连接使用,然后再把连接返回给连接池 String string = jedisCluster.get("a"); System.out.println(string); }}初始化代码:
private void initializeSlotsCache(Set<HostAndPort> startNodes, GenericObjectPoolConfig poolConfig) { //遍历所有从配置文件读取的节点 for (HostAndPort hostAndPort : startNodes) { Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort()); try { cache.discoverClusterNodesAndSlots(jedis); //这个break是个重点,虽然使用的for循环遍历,但是此处的break让此循环立刻跳出 break; } catch (JedisConnectionException e) { // try next nodes } finally { if (jedis != null) { jedis.close(); } } } for (HostAndPort node : startNodes) { cache.setNodeIfNotExist(node); } }
注意上面代码段for循环里面的break:
它让for循环立刻跳出,导致for循环实际上只执行了一次,也就是说只使用了配置文件中所有节点配置的第一个,如此说来,集群配置文件中节点配置一个和配置多个效果一致。
同时还要注意到:通过获取的第一个节点配置实例化了一个Jedis jedis,如果配置文件中第一个节点指向的服务挂机或无法连接,将导致程序无法使用整个集群,虽然redis集群中其它节点是可用的。
0 0
- 总结redis的连接方式
- redis三种连接方式
- java连接主流数据库的方式总结
- JAVA 连接常见数据库的方式总结
- jedis针对三种redis工作模式的连接方式
- redis系列-redis的连接
- redis系列-redis的连接
- Redis的连接
- 连接redis的方法
- Spring(一) 关于连接池的方式的总结
- Redis的pipeline方式
- redis 的启动方式
- Redis的备份方式
- Oracle表连接的几种方式总结
- Java连接数各种据库的方式总结
- atitit.客户端连接oracle数据库的方式总结
- python字符串连接的N种方式总结
- 树莓派远程连接的三种方式总结
- selenium2java造数据例子
- 昨天,请等一等
- React Native指定设备运行
- Unity HTC vive 物体的拾取
- 雷神课程
- 总结redis的连接方式
- Codeforces Round #411(Div. 2)C. Find Amir【思维】
- mysql常用函数整理
- 查询每个年龄的顾客消费金额数的SQL语句
- 求关系的传递闭包
- 蓝桥杯练习系统 合并石子
- StarCitizen星际公民添加好友一起玩教程
- ceph format2格式image
- 提示框的一些需要注意的小细节