public HostAndPort convert() { return new HostAndPort(host, port); }
@Override public Jedis getResource() { while (true) { Jedis jedis = super.getResource(); jedis.setDataSource(this); // get a reference because it can change concurrently final HostAndPort master = currentHostMaster; final HostAndPort connection = new HostAndPort(jedis.getClient().getHost(), jedis.getClient() .getPort()); if (master.equals(connection)) { // connected to the correct master return jedis; } else { returnBrokenResource(jedis); } } }
public HostAndPort getHostAndPortFromNodeLine(String[] nodeInfoPartArray, HostAndPort current) { String stringHostAndPort = nodeInfoPartArray[HOST_AND_PORT_INDEX]; String[] arrayHostAndPort = stringHostAndPort.split(":"); return new HostAndPort(arrayHostAndPort[0].isEmpty() ? current.getHost() : arrayHostAndPort[0], arrayHostAndPort[1].isEmpty() ? current.getPort() : Integer.valueOf(arrayHostAndPort[1])); }
private void initPool(HostAndPort master) { if (!master.equals(currentHostMaster)) { currentHostMaster = master; if (factory == null) { factory = new JedisFactory(master.getHost(), master.getPort(), connectionTimeout, soTimeout, password, database, clientName); initPool(poolConfig, factory); } else { factory.setHostAndPort(currentHostMaster); // although we clear the pool, we still have to check the // returned object // in getResource, this call only clears idle instances, not // borrowed instances internalPool.clear(); } log.info("Created JedisPool to master at " + master); } }
/** * Creates {@link JedisSentinelPool}. * * @return * @since 1.4 */ protected Pool<Jedis> createRedisSentinelPool() { Set<String> hostAndPorts = redisServers .stream() .map(redisNode -> new HostAndPort(redisNode.host, redisNode.port).toString()) .collect(Collectors.toSet()); return new JedisSentinelPool(sentinelMaster, hostAndPorts, poolConfig, getShardInfo().getSoTimeout(), getShardInfo().getPassword()); }
sentinels.add(new HostAndPort("192.168.19.131", 26379).toString()); sentinels.add("192.168.19.131:26380"); try { host = jedisSentinelPool.getCurrentHostMaster().getHost(); port = jedisSentinelPool.getCurrentHostMaster().getPort(); System.out.println(host + ":" + port); host = jedisSentinelPool.getCurrentHostMaster().getHost(); port = jedisSentinelPool.getCurrentHostMaster().getPort(); System.out.println(host + ":" + port);
private List<JedisShardInfo> makeShardInfoList(List<HostAndPort> masters) { List<JedisShardInfo> shardMasters = new ArrayList<JedisShardInfo>(); for (HostAndPort master : masters) { String masterName = masterInfoMap.get(master.toString()); if (StringUtil.isBlank(masterName)) { throw new RuntimeException("没有找到" + master.toString() + "对应的主备masterName"); } ShardSentinelConfig shardSentinelConfig = totalMasterConfig .get(masterName); //读出主服务器对应的配置项 JedisConfig jedisConfig = getJedisConfig(shardSentinelConfig, master.getHost(), master.getPort()); //根据主服务器的配置项和主从所有服务器列表创建 TinyJedisShardInfo jedisShardInfo = new TinyJedisShardInfo( jedisConfig.getHost(), jedisConfig.getPort(), jedisConfig.getTimeout(), shardSentinelConfig.getJedisConfigList()); //设置主服务器密码 jedisShardInfo.setPassword(jedisConfig.getPassword()); shardMasters.add(jedisShardInfo); } return shardMasters; }
final HostAndPort hap = HostAndPort.parseString(sentinel); final HostAndPort hap = HostAndPort.parseString(sentinel); MasterListener masterListener = new MasterListener(masterName, hap.getHost(), hap.getPort());
private boolean isAddressChange(HostAndPort currentMasterAddress) { if (previousMasterAddress == null) { return true; } return !previousMasterAddress.equals(currentMasterAddress); }
/** * redis 主从测试 */ @Test public void sentinelTest() { Set<String> sentinels = new HashSet<String>(); sentinels.add(new HostAndPort("192.168.46.137", 26376).toString()); sentinels.add(new HostAndPort("192.168.46.137", 26377).toString()); sentinels.add(new HostAndPort("192.168.46.137", 26378).toString()); sentinels.add(new HostAndPort("192.168.46.137", 26379).toString()); JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", sentinels, "123456"); System.out.println("Current master: " + sentinelPool.getCurrentHostMaster().toString()); Jedis master = sentinelPool.getResource(); master.set("username","jager"); System.out.println(master.get("username")); sentinelPool.close(); sentinelPool.destroy(); }
private void initPool(HostAndPort master) { synchronized(initPoolLock){ if (!master.equals(currentHostMaster)) { currentHostMaster = master; if (factory == null) { factory = new JedisFactory(master.getHost(), master.getPort(), connectionTimeout, soTimeout, password, database, clientName); initPool(poolConfig, factory); } else { factory.setHostAndPort(currentHostMaster); // although we clear the pool, we still have to check the // returned object // in getResource, this call only clears idle instances, not // borrowed instances internalPool.clear(); } log.info("Created JedisPool to master at " + master); } } }
Jedis jedis = new Jedis(hap.getHost(), hap.getPort()); master = masterMap.get(masterName); if (master == null) { masterMap.put(masterName, master); masterInfoMap .put(master.toString(), masterName); fetched = true; jedis.disconnect(); .split(":"))); MasterListener masterListener = new MasterListener(masters, hap.getHost(), hap.getPort()); masterListeners.add(masterListener); masterListener.start();
private boolean equals(List<HostAndPort> currentShardMasters, List<HostAndPort> shardMasters) { if (currentShardMasters != null && shardMasters != null) { if (currentShardMasters.size() == shardMasters.size()) { for (int i = 0; i < currentShardMasters.size(); i++) { if (!currentShardMasters.get(i).equals(shardMasters.get(i))) return false; } return true; } } return false; }
private void initPool(List<HostAndPort> masters) { if (!equals(currentHostMasters, masters)) { StringBuffer sb = new StringBuffer(); for (HostAndPort master : masters) { sb.append(master.toString()); sb.append(" "); } log.info("Created ShardedJedisPool to master at [" + sb.toString() + "]"); List<JedisShardInfo> shardMasters = makeShardInfoList(masters); initPool(poolConfig, new ShardedJedisFactory(shardMasters, Hashing.MURMUR_HASH, null)); currentHostMasters = masters; } }
public JedisFactory(final String host, final int port, final int connectionTimeout, final int soTimeout, final String password, final int database, final String clientName) { this.hostAndPort.set(new HostAndPort(host, port)); this.connectionTimeout = connectionTimeout; this.soTimeout = soTimeout; this.password = password; this.database = database; this.clientName = clientName; }
public void setNodeIfNotExist(HostAndPort node) { w.lock(); try { String nodeKey = getNodeKey(node); if (nodes.containsKey(nodeKey)) return; JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), connectionTimeout, soTimeout, null, 0, null); nodes.put(nodeKey, nodePool); } finally { w.unlock(); } }
@Override public Jedis getResource() { while (true) { Jedis jedis = super.getResource(); // get a reference because it can change concurrently final HostAndPort master = currentHostMaster; final HostAndPort connection = new HostAndPort(jedis.getClient() .getHost(), jedis.getClient().getPort()); if (master.equals(connection)) { // connected to the correct master return jedis; } else { invalidateResource(jedis); } } }
/** * Creates {@link JedisSentinelPool}. * * @return * @since 1.4 */ protected Pool<Jedis> createRedisSentinelPool() { Set<String> hostAndPorts = redisServers .stream() .map(redisNode -> new HostAndPort(redisNode.host, redisNode.port).toString()) .collect(Collectors.toSet()); return new JedisSentinelPool(sentinelMaster, hostAndPorts, poolConfig, getShardInfo().getSoTimeout(), getShardInfo().getPassword()); }