Search in sources :

Example 1 with HostAndPort

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;
}
Also used : HostAndPort(io.lettuce.core.internal.HostAndPort) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration)

Example 2 with HostAndPort

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;
}
Also used : HostAndPort(io.lettuce.core.internal.HostAndPort) RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList)

Example 3 with HostAndPort

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;
}
Also used : HostAndPort(io.lettuce.core.internal.HostAndPort)

Example 4 with HostAndPort

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());
    }
}
Also used : HostAndPort(io.lettuce.core.internal.HostAndPort) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress)

Example 5 with HostAndPort

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);
}
Also used : HostAndPort(io.lettuce.core.internal.HostAndPort) Test(org.junit.jupiter.api.Test)

Aggregations

HostAndPort (io.lettuce.core.internal.HostAndPort)11 Test (org.junit.jupiter.api.Test)4 RedisURI (io.lettuce.core.RedisURI)3 ArrayList (java.util.ArrayList)2 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)1 Intent (io.lettuce.core.cluster.ClusterConnectionProvider.Intent)1 AskRedirectionEvent (io.lettuce.core.cluster.event.AskRedirectionEvent)1 MovedRedirectionEvent (io.lettuce.core.cluster.event.MovedRedirectionEvent)1 DefaultEndpoint (io.lettuce.core.protocol.DefaultEndpoint)1 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1 RedisSentinelConfiguration (org.springframework.data.redis.connection.RedisSentinelConfiguration)1 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)1 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)1