use of io.lettuce.core.internal.HostAndPort in project gravitee-api-management by gravitee-io.
the class RedisConnectionFactory method getObject.
@Override
public org.springframework.data.redis.connection.RedisConnectionFactory getObject() throws Exception {
final LettuceConnectionFactory lettuceConnectionFactory;
if (isSentinelEnabled()) {
// Sentinels + Redis master / replicas
logger.debug("Redis repository configured to use Sentinel connection");
List<HostAndPort> sentinelNodes = getSentinelNodes();
String redisMaster = readPropertyValue(propertyPrefix + SENTINEL_PARAMETER_PREFIX + "master", String.class);
if (StringUtils.isBlank(redisMaster)) {
throw new IllegalStateException("Incorrect Sentinel configuration : parameter '" + SENTINEL_PARAMETER_PREFIX + "master' is mandatory !");
}
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
sentinelConfiguration.master(redisMaster);
// Parsing and registering nodes
sentinelNodes.forEach(hostAndPort -> sentinelConfiguration.sentinel(hostAndPort.getHostText(), hostAndPort.getPort()));
// Sentinel Password
sentinelConfiguration.setSentinelPassword(readPropertyValue(propertyPrefix + SENTINEL_PARAMETER_PREFIX + "password", String.class));
// Redis Password
sentinelConfiguration.setPassword(readPropertyValue(propertyPrefix + "password", String.class));
lettuceConnectionFactory = new LettuceConnectionFactory(sentinelConfiguration, buildLettuceClientConfiguration());
} else {
// Standalone Redis
logger.debug("Redis repository configured to use standalone connection");
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(readPropertyValue(propertyPrefix + "host", String.class, "localhost"));
standaloneConfiguration.setPort(readPropertyValue(propertyPrefix + "port", int.class, 6379));
standaloneConfiguration.setPassword(readPropertyValue(propertyPrefix + "password", String.class));
lettuceConnectionFactory = new LettuceConnectionFactory(standaloneConfiguration, buildLettuceClientConfiguration());
}
lettuceConnectionFactory.afterPropertiesSet();
return lettuceConnectionFactory;
}
use of io.lettuce.core.internal.HostAndPort in project chunjun by DTStack.
the class RedisAsyncClient method buildClusterURIs.
private List<RedisURI> buildClusterURIs(String url) {
String password = redisConf.getPassword();
int database = redisConf.getDatabase();
String[] addresses = StringUtils.split(url, ",");
List<RedisURI> redisURIs = new ArrayList<>(addresses.length);
for (String addr : addresses) {
HostAndPort hostAndPort = HostAndPort.parse(addr);
RedisURI redisURI = RedisURI.create(hostAndPort.hostText, hostAndPort.port);
if (StringUtils.isNotEmpty(password)) {
redisURI.setPassword(password);
}
redisURI.setDatabase(database);
redisURIs.add(redisURI);
}
return redisURIs;
}
use of io.lettuce.core.internal.HostAndPort in project lettuce-core by lettuce-io.
the class RedisURI method configureSentinel.
private static RedisURI.Builder configureSentinel(URI uri) {
String masterId = uri.getFragment();
RedisURI.Builder builder = null;
if (isNotEmpty(uri.getHost())) {
if (uri.getPort() != -1) {
builder = RedisURI.Builder.sentinel(uri.getHost(), uri.getPort());
} else {
builder = RedisURI.Builder.sentinel(uri.getHost());
}
}
if (builder == null && isNotEmpty(uri.getAuthority())) {
String authority = uri.getAuthority();
if (authority.indexOf('@') > -1) {
authority = authority.substring(authority.indexOf('@') + 1);
}
String[] hosts = authority.split(",");
for (String host : hosts) {
HostAndPort hostAndPort = HostAndPort.parse(host);
if (builder == null) {
if (hostAndPort.hasPort()) {
builder = RedisURI.Builder.sentinel(hostAndPort.getHostText(), hostAndPort.getPort());
} else {
builder = RedisURI.Builder.sentinel(hostAndPort.getHostText());
}
} else {
if (hostAndPort.hasPort()) {
builder.withSentinel(hostAndPort.getHostText(), hostAndPort.getPort());
} else {
builder.withSentinel(hostAndPort.getHostText());
}
}
}
}
LettuceAssert.notNull(builder, "Invalid URI, cannot get host part");
if (isNotEmpty(masterId)) {
builder.withSentinelMasterId(masterId);
}
if (uri.getScheme().equals(URI_SCHEME_REDIS_SENTINEL_SECURE)) {
builder.withSsl(true);
}
return builder;
}
use of io.lettuce.core.internal.HostAndPort in project lettuce-core by lettuce-io.
the class MappingSocketAddressResolver method resolve.
@Override
public SocketAddress resolve(RedisURI redisURI) {
if (redisURI.getSocket() != null) {
return getDomainSocketAddress(redisURI);
}
HostAndPort hostAndPort = HostAndPort.of(redisURI.getHost(), redisURI.getPort());
HostAndPort mapped = mappingFunction.apply(hostAndPort);
if (mapped == null) {
throw new IllegalStateException("Mapping function must not return null for HostAndPort");
}
try {
return doResolve(mapped);
} catch (UnknownHostException e) {
return new InetSocketAddress(redisURI.getHost(), redisURI.getPort());
}
}
use of io.lettuce.core.internal.HostAndPort in project lettuce-core by lettuce-io.
the class ClusterDistributionChannelWriterUnitTests method shouldParseIPv6AskTargetCorrectly.
@Test
void shouldParseIPv6AskTargetCorrectly() {
HostAndPort askTarget = ClusterDistributionChannelWriter.getAskTarget("ASK 1234-2020 1:2:3:4::6:6381");
assertThat(askTarget.getHostText()).isEqualTo("1:2:3:4::6");
assertThat(askTarget.getPort()).isEqualTo(6381);
}
Aggregations