redis.clients.jedis.HostAndPort Java Examples
The following examples show how to use
redis.clients.jedis.HostAndPort.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JedisClusterFactory.java From code with Apache License 2.0 | 6 votes |
public void init() { //这里可以设置相关参数 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); //从配置文件中读取ip:port的参数放进Set中 Set<HostAndPort> nodeSet = new HashSet<>(); for (String hostPort : hostPortList) { String[] arr = hostPort.split(":"); if (arr.length != 2) { continue; } nodeSet.add(new HostAndPort(arr[0], Integer.parseInt(arr[1]))); } try { jedisCluster = new JedisCluster(nodeSet, timeout, jedisPoolConfig); } catch (Exception e) { // logger e.printStackTrace(); } }
Example #2
Source File: RedisConfiguration.java From seed with Apache License 2.0 | 6 votes |
@Bean public JedisCluster jedisCluster(){ Set<HostAndPort> nodes = new HashSet<>(); for(String node : this.getNodes()){ try{ String[] parts = StringUtils.split(node, ":"); Assert.state(parts.length==2, "redis node shoule be defined as 'host:port', not '" + Arrays.toString(parts) + "'"); nodes.add(new HostAndPort(parts[0], Integer.parseInt(parts[1]))); }catch(RuntimeException e){ throw new IllegalStateException("Invalid redis cluster nodes property '" + node + "'", e); } } if(nodes.isEmpty()){ return null; } GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(this.getMaxTotal()); config.setMaxIdle(this.getMaxIdle()); config.setMinIdle(this.getMinIdle()); return new JedisCluster(nodes, this.getConnectionTimeout(), this.getSoTimeout(), this.getMaxRedirections(), this.getPassword(), config); }
Example #3
Source File: RedisSentinelTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void testSentinel() { JedisSentinelPool sentinelPool = ClientBuilder.redisSentinel(appId) .setConnectionTimeout(2000) .setSoTimeout(1000) .build(); HostAndPort currentHostMaster = sentinelPool.getCurrentHostMaster(); logger.info("current master: {}", currentHostMaster.toString()); Jedis jedis = sentinelPool.getResource(); for (int i = 0; i < 10; i++) { jedis.lpush("mylist", "list-" + i); } jedis.close(); sentinelPool.destroy(); }
Example #4
Source File: DefaultSentinel.java From redis-rdb-cli with Apache License 2.0 | 6 votes |
protected void pulse() { for (HostAndPort sentinel : hosts) { try (final Jedis jedis = new Jedis(sentinel)) { List<String> list = jedis.sentinelGetMasterAddrByName(masterName); if (list == null || list.size() != 2) { throw new JedisException("host: " + list); } String host = list.get(0); int port = Integer.parseInt(list.get(1)); doSwitchListener(new HostAndPort(host, port)); logger.info("subscribe sentinel {}", sentinel); jedis.subscribe(new PubSub(), this.channel); } catch (Throwable cause) { logger.warn("suspend sentinel {}, cause: {}", sentinel, cause); } } }
Example #5
Source File: RedisService.java From redis-manager with Apache License 2.0 | 6 votes |
@Override public Map<String, String> getClusterInfo(Cluster cluster) { RedisClient redisClient = null; try { String redisPassword = cluster.getRedisPassword(); Set<HostAndPort> hostAndPortSet = nodesToHostAndPortSet(cluster.getNodes()); RedisURI redisURI = new RedisURI(hostAndPortSet, redisPassword); redisClient = RedisClientFactory.buildRedisClient(redisURI); return redisClient.getClusterInfo(); } catch (Exception e) { logger.error("Get cluster info failed, cluster name = " + cluster.getClusterName(), e); return null; } finally { close(redisClient); } }
Example #6
Source File: RedisService.java From redis-manager with Apache License 2.0 | 6 votes |
@Override public boolean monitorMaster(SentinelMaster sentinelMaster) { Cluster cluster = clusterService.getClusterById(sentinelMaster.getClusterId()); if (cluster == null) { return false; } boolean result = false; Set<HostAndPort> hostAndPorts = nodesToHostAndPortSet(cluster.getNodes()); for (HostAndPort hostAndPort : hostAndPorts) { RedisClient redisClient = null; try { redisClient = RedisClientFactory.buildRedisClient(hostAndPort); result = redisClient.monitorMaster(sentinelMaster.getName(), sentinelMaster.getHost(), sentinelMaster.getPort(), sentinelMaster.getQuorum()) && sentinelSet(redisClient, sentinelMaster); } catch (Exception e) { logger.error("Monitor master failed, master name: " + sentinelMaster.getName(), e); result = false; } finally { close(redisClient); } } return result; }
Example #7
Source File: JedisClusterConstructorWithListHostAndPortArgInterceptorTest.java From skywalking with Apache License 2.0 | 6 votes |
@Test public void onHugeClusterConstruct() throws Exception { hostAndPortSet = new LinkedHashSet<HostAndPort>(); for (int i = 0; i < 100; i++) { hostAndPortSet.add(new HostAndPort("localhost", i)); } enhancedInstance = new EnhancedInstance() { private Object v; @Override public Object getSkyWalkingDynamicField() { return v; } @Override public void setSkyWalkingDynamicField(Object value) { this.v = value; } }; interceptor.onConstruct(enhancedInstance, new Object[] {hostAndPortSet}); Assert.assertTrue(enhancedInstance.getSkyWalkingDynamicField().toString().length() == 200); }
Example #8
Source File: RedisQParserPlugin.java From solr-redis with Apache License 2.0 | 6 votes |
@Override public void init(final NamedList args) { final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(getInt(args, MAX_CONNECTIONS_FIELD, DEFAULT_MAX_CONNECTIONS)); final String host = getString(args, HOST_FIELD, HostAndPort.LOCALHOST_STR); final int timeout = getInt(args, TIMEOUT_FIELD, Protocol.DEFAULT_TIMEOUT); final String password = getString(args, PASSWORD_FIELD, null); final int database = getInt(args, DATABASE_FIELD, Protocol.DEFAULT_DATABASE); final int retries = getInt(args, RETRIES_FIELD, DEFAULT_RETRIES); final String[] hostAndPort = host.split(":"); final JedisPool jedisConnectionPool = createPool(poolConfig, hostAndPort[0], hostAndPort.length == 2 ? Integer.parseInt(hostAndPort[1]) : Protocol.DEFAULT_PORT, timeout, password, database); connectionHandler = createCommandHandler(jedisConnectionPool, retries); log.info("Initialized RedisQParserPlugin with host: " + host); }
Example #9
Source File: JedisClusterNodeInformationParserTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void testParseSlotBeingMigrated() { String nodeInfo = "5f4a2236d00008fba7ac0dd24b95762b446767bd :7379 myself,master - 0 0 1 connected 0-5459 [5460->-5f4a2236d00008fba7ac0dd24b95762b446767bd] [5461-<-5f4a2236d00008fba7ac0dd24b95762b446767bd]"; HostAndPort current = new HostAndPort("localhost", 7379); ClusterNodeInformation clusterNodeInfo = parser.parse(nodeInfo, current); assertEquals(clusterNodeInfo.getNode(), current); for (int slot = 0; slot <= 5459; slot++) { assertTrue(clusterNodeInfo.getAvailableSlots().contains(slot)); } assertEquals(1, clusterNodeInfo.getSlotsBeingMigrated().size()); assertTrue(clusterNodeInfo.getSlotsBeingMigrated().contains(5460)); assertEquals(1, clusterNodeInfo.getSlotsBeingImported().size()); assertTrue(clusterNodeInfo.getSlotsBeingImported().contains(5461)); }
Example #10
Source File: AppDeployCenterImpl.java From cachecloud with Apache License 2.0 | 6 votes |
private void startMigrateSlot(final InstanceReshardProcess instanceReshardProcess) { final long appId = instanceReshardProcess.getAppId(); final long appAuditId = instanceReshardProcess.getAuditId(); final InstanceInfo targetInstanceInfo = instanceReshardProcess.getTargetInstanceInfo(); processThreadPool.execute(new Runnable() { @Override public void run() { //所有节点用户clustersetslot Set<HostAndPort> clusterHosts = getEffectiveInstanceList(appId); RedisClusterReshard clusterReshard = new RedisClusterReshard(clusterHosts, redisCenter, instanceReshardProcessDao); //添加进度 boolean joinCluster = clusterReshard.migrateSlot(instanceReshardProcess); if (joinCluster) { // 改变审核状态 appAuditDao.updateAppAudit(appAuditId, AppCheckEnum.APP_ALLOCATE_RESOURCE.value()); if (targetInstanceInfo != null && targetInstanceInfo.getStatus() != InstanceStatusEnum.GOOD_STATUS.getStatus()) { targetInstanceInfo.setStatus(InstanceStatusEnum.GOOD_STATUS.getStatus()); instanceDao.update(targetInstanceInfo); } } } }); }
Example #11
Source File: NodeManageController.java From redis-manager with Apache License 2.0 | 6 votes |
@RequestMapping(value = "/getNodeInfo", method = RequestMethod.POST) @ResponseBody public Result getNodeInfo(@RequestBody RedisNode redisNode) { Integer clusterId = redisNode.getClusterId(); Cluster cluster = clusterService.getClusterById(clusterId); HostAndPort hostAndPort = new HostAndPort(redisNode.getHost(), redisNode.getPort()); Map<String, String> infoMap = redisService.getNodeInfo(hostAndPort, cluster.getRedisPassword()); if (infoMap == null) { return Result.failResult(); } List<JSONObject> infoList = new ArrayList<>(); infoMap.forEach((key, value) -> { JSONObject jsonObject = new JSONObject(); jsonObject.put("key", key); jsonObject.put("value", value); jsonObject.put("description", RedisNodeInfoUtil.getNodeInfoItemDesc(key)); infoList.add(jsonObject); }); return Result.successResult(infoList); }
Example #12
Source File: JedisSentinelPoolTest.java From cachecloud with Apache License 2.0 | 6 votes |
private void forceFailover(JedisSentinelPool pool) throws InterruptedException { HostAndPort oldMaster = pool.getCurrentHostMaster(); // jedis connection should be master Jedis beforeFailoverJedis = pool.getResource(); assertEquals("PONG", beforeFailoverJedis.ping()); waitForFailover(pool, oldMaster); Jedis afterFailoverJedis = pool.getResource(); assertEquals("PONG", afterFailoverJedis.ping()); assertEquals("foobared", afterFailoverJedis.configGet("requirepass").get(1)); assertEquals(2, afterFailoverJedis.getDB()); // returning both connections to the pool should not throw beforeFailoverJedis.close(); afterFailoverJedis.close(); }
Example #13
Source File: BootStrap.java From MyBlog with Apache License 2.0 | 5 votes |
/*********************************************************************************************************/ //redisCluster设置 @Bean(destroyMethod = "close") public JedisCluster getJedisCluster() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 最大空闲数 jedisPoolConfig.setMaxIdle(10); // 连接池的最大数据库连接数 jedisPoolConfig.setMaxTotal(30); // 最大建立连接等待时间 jedisPoolConfig.setMaxWaitMillis(1500); // 逐出连接的最小空闲时间 默认1800000毫秒(30分钟) jedisPoolConfig.setMinEvictableIdleTimeMillis(1800000); // 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3 jedisPoolConfig.setNumTestsPerEvictionRun(3); // 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1 jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000); // 连接空闲多久后释放,当空闲时间大于该值且空闲连接大于最大空闲连接数时释放 jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000); // 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 jedisPoolConfig.setTestOnBorrow(true); // 在空闲时检查有效性, 默认false jedisPoolConfig.setTestWhileIdle(true); // 连接耗尽时是否阻塞,false报异常,true阻塞直到超时,默认true jedisPoolConfig.setBlockWhenExhausted(false); Set<HostAndPort> nodeSet = Sets.newHashSet(); nodeSet.add(new HostAndPort("127.0.0.1", 6381)); nodeSet.add(new HostAndPort("127.0.0.1", 6382)); nodeSet.add(new HostAndPort("127.0.0.1", 6383)); nodeSet.add(new HostAndPort("127.0.0.1", 6384)); nodeSet.add(new HostAndPort("127.0.0.1", 6385)); nodeSet.add(new HostAndPort("127.0.0.1", 6386)); return new JedisCluster(nodeSet, 2000, 100, jedisPoolConfig); }
Example #14
Source File: ClusterNodeInformationParser.java From cachecloud with Apache License 2.0 | 5 votes |
public ClusterNodeInformation parse(String nodeInfo, HostAndPort current) { String[] nodeInfoPartArray = nodeInfo.split(" "); HostAndPort node = getHostAndPortFromNodeLine(nodeInfoPartArray, current); ClusterNodeInformation info = new ClusterNodeInformation(node); if (nodeInfoPartArray.length >= SLOT_INFORMATIONS_START_INDEX) { String[] slotInfoPartArray = extractSlotParts(nodeInfoPartArray); fillSlotInformation(slotInfoPartArray, info); } return info; }
Example #15
Source File: JedisClusterFactory.java From redis-game-transaction with Apache License 2.0 | 5 votes |
@Override public void afterPropertiesSet() throws Exception { if (jedisClusterNodes == null || jedisClusterNodes.size() == 0) { throw new NullPointerException("jedisClusterNodes is null."); } Set<HostAndPort> haps = new HashSet<HostAndPort>(); for (String node : jedisClusterNodes) { String[] arr = node.split(":"); if (arr.length != 2) { throw new ParseException("node address error !",node.length()-1); } haps.add(new HostAndPort(arr[0], Integer.valueOf(arr[1]))); } jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, genericObjectPoolConfig); }
Example #16
Source File: JedisConfig.java From paas with Apache License 2.0 | 5 votes |
@Bean public JedisCluster jedisCluster() { Set<HostAndPort> set = new HashSet<>(); set.add(new HostAndPort(cluster01Host, cluster01Port)); set.add(new HostAndPort(cluster02Host, cluster02Port)); set.add(new HostAndPort(cluster03Host, cluster03Port)); set.add(new HostAndPort(cluster04Host, cluster04Port)); set.add(new HostAndPort(cluster05Host, cluster05Port)); set.add(new HostAndPort(cluster06Host, cluster06Port)); return new JedisCluster(set); }
Example #17
Source File: RedisUtil.java From redis-manager with Apache License 2.0 | 5 votes |
public static Set<HostAndPort> nodesToHostAndPortSet(String nodes) { String[] nodeList = SignUtil.splitByCommas(nodes); int length = nodeList.length; Set<HostAndPort> hostAndPortSet = new HashSet<>(length); if (length > 0) { for (String node : nodeList) { String[] ipAndPort = SignUtil.splitByColon(node); HostAndPort hostAndPort = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1])); hostAndPortSet.add(hostAndPort); } } return hostAndPortSet; }
Example #18
Source File: RedisURI.java From redis-manager with Apache License 2.0 | 5 votes |
public RedisURI(String token, Set<HostAndPort> hostAndPortSet, String requirePass, String clientName, String sentinelMasterId, int database) { this.token = token; this.hostAndPortSet = hostAndPortSet; this.requirePass = requirePass; this.clientName = clientName; this.sentinelMasterId = sentinelMasterId; this.database = database; }
Example #19
Source File: TracingJedisCluster.java From java-redis-client with Apache License 2.0 | 5 votes |
public TracingJedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, GenericObjectPoolConfig poolConfig, TracingConfiguration tracingConfiguration) { super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, poolConfig); this.helper = new TracingHelper(tracingConfiguration); }
Example #20
Source File: RedisClientPool.java From nano-framework with Apache License 2.0 | 5 votes |
private JedisCluster createJedisClusterPool(final RedisConfig config) { Assert.notNull(config); try { final String[] hostAndports = config.getHostNames().split(";"); final List<String> redisHosts = Lists.newArrayList(); final List<Integer> redisPorts = Lists.newArrayList(); for (int i = 0; i < hostAndports.length; i++) { final String[] hostPort = hostAndports[i].split(":"); redisHosts.add(hostPort[0]); redisPorts.add(Integer.valueOf(hostPort[1])); } final Set<HostAndPort> nodes = Sets.newLinkedHashSet(); for (int i = 0; i < redisHosts.size(); i++) { final String host = (String) redisHosts.get(i); final int port = (Integer) redisPorts.get(i); nodes.add(new HostAndPort(host, port)); } Integer timeout = config.getTimeOut(); if (timeout == null || timeout < 0) { timeout = DEFAULT_TIMEOUT; } Integer maxRedirections = config.getMaxRedirections(); if (maxRedirections == null || maxRedirections < 0) { maxRedirections = DEFAULT_MAX_REDIRECTIONS; } return new JedisCluster(nodes, timeout, maxRedirections, getJedisPoolConfig(config)); } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } }
Example #21
Source File: RedisSlowLog.java From redis-manager with Apache License 2.0 | 5 votes |
public RedisSlowLog(HostAndPort hostAndPort, Slowlog slowlog) { this.node = hostAndPort.toString(); this.dateTime = new Timestamp(slowlog.getTimeStamp() * 1000); this.executionTime = slowlog.getExecutionTime(); List<String> args = slowlog.getArgs(); this.type = args.get(0); List<String> commands = args.subList(1, args.size()); this.command = Joiner.on(" ").skipNulls().join(commands); }
Example #22
Source File: TestRedisQParserPlugin.java From solr-redis with Apache License 2.0 | 5 votes |
@Test public void shouldConfigurePoolWithCustomMaxConnections() { final NamedList<String> list = new NamedList<>(); list.add("maxConnections", "100"); parserPlugin.init(list); verify(parserPlugin).createPool(poolConfigArgument.capture(), eq(HostAndPort.LOCALHOST_STR), eq(Protocol.DEFAULT_PORT), eq(Protocol.DEFAULT_TIMEOUT), passwordArgument.capture(), eq(Protocol.DEFAULT_DATABASE)); verify(parserPlugin).createCommandHandler(poolArgument.capture(), eq(1)); assertNull(passwordArgument.getValue()); assertEquals(100, poolConfigArgument.getValue().getMaxTotal()); }
Example #23
Source File: FlinkJedisClusterConfig.java From bahir-flink with Apache License 2.0 | 5 votes |
/** * Returns nodes. * * @return list of node information */ public Set<HostAndPort> getNodes() { Set<HostAndPort> ret = new HashSet<>(); for (InetSocketAddress node : nodes) { ret.add(new HostAndPort(node.getHostName(), node.getPort())); } return ret; }
Example #24
Source File: NodeInfoCollectionAbstract.java From redis-manager with Apache License 2.0 | 5 votes |
private Set<HostAndPort> getHostAndPortSet(Cluster cluster) { List<RedisNode> redisNodeList = redisService.getRealRedisNodeList(cluster); Set<HostAndPort> hostAndPortSet = new HashSet<>(); for (RedisNode redisNode : redisNodeList) { hostAndPortSet.add(new HostAndPort(redisNode.getHost(), redisNode.getPort())); } return hostAndPortSet; }
Example #25
Source File: AppServiceImpl.java From cachecloud with Apache License 2.0 | 5 votes |
@Override public List<InstanceInfo> getAppInstanceInfo(Long appId) { AppDesc appDesc = appDao.getAppDescById(appId); String password = appDesc.getPassword(); List<InstanceInfo> resultList = instanceDao.getInstListByAppId(appId); if (resultList != null && resultList.size() > 0) { for (InstanceInfo instanceInfo : resultList) { int type = instanceInfo.getType(); if(instanceInfo.getStatus() != InstanceStatusEnum.GOOD_STATUS.getStatus()){ continue; } if (TypeUtil.isRedisType(type)) { if (TypeUtil.isRedisSentinel(type)) { continue; } String host = instanceInfo.getIp(); int port = instanceInfo.getPort(); Boolean isMaster = redisCenter.isMaster(appId, host, port); instanceInfo.setRoleDesc(isMaster); if(isMaster != null && !isMaster){ HostAndPort hap = redisCenter.getMaster(host, port, password); if (hap != null) { instanceInfo.setMasterHost(hap.getHost()); instanceInfo.setMasterPort(hap.getPort()); for (InstanceInfo innerInfo : resultList) { if (innerInfo.getIp().equals(hap.getHost()) && innerInfo.getPort() == hap.getPort()) { instanceInfo.setMasterInstanceId(innerInfo.getId()); break; } } } } } } } return resultList; }
Example #26
Source File: NodeManageController.java From redis-manager with Apache License 2.0 | 5 votes |
@RequestMapping(value = "/standaloneReplicateOf", method = RequestMethod.POST) @ResponseBody @OperationLog(type = OperationType.REPLICATE_OF, objType = OperationObjectType.NODE) public Result standaloneReplicateOf(@RequestBody List<RedisNode> redisNodeList) { Result result = clusterOperate(redisNodeList, (cluster, redisNode) -> { String masterNode = redisNode.getMasterId(); HostAndPort hostAndPort = RedisUtil.nodesToHostAndPort(masterNode); RedisNode masterRedisNode = RedisNode.masterRedisNode(hostAndPort); String resultMessage = redisService.standaloneReplicaOf(cluster, masterRedisNode, redisNode); return Strings.isNullOrEmpty(resultMessage); }); return result; }
Example #27
Source File: JedisSessionRepositoryFactory.java From HttpSessionReplacer with MIT License | 5 votes |
/** * Extracts jedis host/port configuration * @param config */ static Set<HostAndPort> jedisHostsAndPorts(RedisConfiguration config) { Set<HostAndPort> hostsAndPorts = new HashSet<>(); for (RedisConfiguration.HostAndPort hp : config.hostsAndPorts()) { hostsAndPorts.add(new HostAndPort(hp.host, hp.port)); } return hostsAndPorts; }
Example #28
Source File: RedisClusterTest.java From mpush with Apache License 2.0 | 5 votes |
@Before public void init() { jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005)); cluster = new JedisCluster(jedisClusterNodes, new GenericObjectPoolConfig()); }
Example #29
Source File: RedisSentinelReplicator.java From redis-rdb-cli with Apache License 2.0 | 5 votes |
@Override public void onSwitch(Sentinel sentinel, HostAndPort next) { if (prev == null || !prev.equals(next)) { logger.info("Sentinel switch master to [{}]", next); Replicators.closeQuietly(replicator); executors.submit(() -> { Reflections.setField(replicator, "host", next.getHost()); Reflections.setField(replicator, "port", next.getPort()); Replicators.openQuietly(replicator); }); } prev = next; }
Example #30
Source File: RedisSentinelReplicator.java From redis-rdb-cli with Apache License 2.0 | 5 votes |
private void initialize(List<HostAndPort> hosts, String name, Configuration configuration) { Objects.requireNonNull(hosts); Objects.requireNonNull(configuration); this.replicator = new RedisSocketReplicator("", 1, configuration); this.sentinel = new DefaultSentinel(hosts, name, configuration); this.sentinel.addSentinelListener(this); }