Search in sources :

Example 11 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project olive by ClareTung.

the class RedisConfig method secondaryRedisConfig.

/**
 * 配置第二个数据源
 */
@Bean("secondaryRedisClusterConfig")
public RedisClusterConfiguration secondaryRedisConfig() {
    Map<String, Object> source = new HashMap<>(8);
    source.put("spring.redis.cluster.nodes", environment.getProperty("spring.secondaryRedis.cluster.nodes"));
    RedisClusterConfiguration redisClusterConfiguration;
    redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
    redisClusterConfiguration.setPassword(environment.getProperty("spring.redis.password"));
    return redisClusterConfiguration;
}
Also used : HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Bean(org.springframework.context.annotation.Bean)

Example 12 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project JAVA_LOVE_GO by zhonghuasheng.

the class RedisFactoryConfig method lettuceConnectionFactory.

@Bean
public RedisConnectionFactory lettuceConnectionFactory() {
    Map<String, Object> source = new HashMap<String, Object>();
    source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));
    source.put("spring.redis.cluster.max-redirects", environment.getProperty("spring.redis.cluster.max-redirects"));
    source.put("spring.redis.timeout", environment.getProperty("spring.redis.timeout"));
    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
    return new LettuceConnectionFactory(redisClusterConfiguration);
}
Also used : HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Bean(org.springframework.context.annotation.Bean)

Example 13 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project cas by apereo.

the class RedisObjectFactory method getClusterConfig.

private static RedisClusterConfiguration getClusterConfig(final BaseRedisProperties redis) {
    val redisConfiguration = new RedisClusterConfiguration();
    val cluster = redis.getCluster();
    cluster.getNodes().stream().filter(nodeConfig -> StringUtils.hasText(nodeConfig.getHost()) && nodeConfig.getPort() > 0 && StringUtils.hasText(nodeConfig.getType())).forEach(nodeConfig -> {
        LOGGER.trace("Building redis cluster node for [{}]", nodeConfig);
        val nodeBuilder = new RedisNode.RedisNodeBuilder().listeningAt(nodeConfig.getHost(), nodeConfig.getPort()).promotedAs(RedisNode.NodeType.valueOf(nodeConfig.getType().toUpperCase()));
        if (StringUtils.hasText(nodeConfig.getReplicaOf())) {
            nodeBuilder.replicaOf(nodeConfig.getReplicaOf());
        }
        if (StringUtils.hasText(nodeConfig.getId())) {
            nodeBuilder.withId(nodeConfig.getId());
        }
        if (StringUtils.hasText(nodeConfig.getName())) {
            nodeBuilder.withName(nodeConfig.getName());
        }
        redisConfiguration.clusterNode(nodeBuilder.build());
    });
    if (StringUtils.hasText(cluster.getPassword())) {
        redisConfiguration.setPassword(cluster.getPassword());
    }
    if (cluster.getMaxRedirects() > 0) {
        redisConfiguration.setMaxRedirects(cluster.getMaxRedirects());
    }
    return redisConfiguration;
}
Also used : lombok.val(lombok.val) SslOptions(io.lettuce.core.SslOptions) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) BaseRedisProperties(org.apereo.cas.configuration.model.support.redis.BaseRedisProperties) SocketOptions(io.lettuce.core.SocketOptions) Beans(org.apereo.cas.configuration.support.Beans) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) ArrayList(java.util.ArrayList) UtilityClass(lombok.experimental.UtilityClass) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Duration(java.time.Duration) ClientOptions(io.lettuce.core.ClientOptions) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) RedisNode(org.springframework.data.redis.connection.RedisNode) lombok.val(lombok.val) RedisConfiguration(org.springframework.data.redis.connection.RedisConfiguration) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Collectors(java.util.stream.Collectors) RedisPassword(org.springframework.data.redis.connection.RedisPassword) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ReadFrom(io.lettuce.core.ReadFrom) LettucePoolingClientConfiguration(org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration) CasSSLContext(org.apereo.cas.authentication.CasSSLContext) TimeoutOptions(io.lettuce.core.TimeoutOptions) StringUtils(org.springframework.util.StringUtils) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration)

Example 14 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project spring-boot by spring-projects.

the class RedisAutoConfigurationTests method testRedisConfigurationWithCluster.

@Test
void testRedisConfigurationWithCluster() {
    List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
    this.contextRunner.withPropertyValues("spring.redis.cluster.nodes[0]:" + clusterNodes.get(0), "spring.redis.cluster.nodes[1]:" + clusterNodes.get(1)).run((context) -> {
        RedisClusterConfiguration clusterConfiguration = context.getBean(LettuceConnectionFactory.class).getClusterConfiguration();
        assertThat(clusterConfiguration.getClusterNodes()).hasSize(2);
        assertThat(clusterConfiguration.getClusterNodes()).extracting((node) -> node.getHost() + ":" + node.getPort()).containsExactlyInAnyOrder("127.0.0.1:27379", "127.0.0.1:27380");
    });
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) Arrays(java.util.Arrays) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) LettuceClientConfiguration(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) ContextConsumer(org.springframework.boot.test.context.runner.ContextConsumer) RefreshTrigger(io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Duration(java.time.Duration) ClientOptions(io.lettuce.core.ClientOptions) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) Tracing(io.lettuce.core.tracing.Tracing) EnumSet(java.util.EnumSet) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) RedisNode(org.springframework.data.redis.connection.RedisNode) RedisOperations(org.springframework.data.redis.core.RedisOperations) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Collectors(java.util.stream.Collectors) Pool(org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) AssertableApplicationContext(org.springframework.boot.test.context.assertj.AssertableApplicationContext) LettuceClientConfigurationBuilder(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder) LettucePoolingClientConfiguration(org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) Bean(org.springframework.context.annotation.Bean) StringUtils(org.springframework.util.StringUtils) Mockito.mock(org.mockito.Mockito.mock) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Test(org.junit.jupiter.api.Test)

Example 15 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project nifi by apache.

the class RedisUtils method createConnectionFactory.

public static JedisConnectionFactory createConnectionFactory(final PropertyContext context, final ComponentLog logger) {
    final String redisMode = context.getProperty(RedisUtils.REDIS_MODE).getValue();
    final String connectionString = context.getProperty(RedisUtils.CONNECTION_STRING).evaluateAttributeExpressions().getValue();
    final Integer dbIndex = context.getProperty(RedisUtils.DATABASE).evaluateAttributeExpressions().asInteger();
    final String password = context.getProperty(RedisUtils.PASSWORD).evaluateAttributeExpressions().getValue();
    final Integer timeout = context.getProperty(RedisUtils.COMMUNICATION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue();
    final JedisPoolConfig poolConfig = createJedisPoolConfig(context);
    JedisConnectionFactory connectionFactory;
    if (RedisUtils.REDIS_MODE_STANDALONE.getValue().equals(redisMode)) {
        final JedisShardInfo jedisShardInfo = createJedisShardInfo(connectionString, timeout, password);
        logger.info("Connecting to Redis in standalone mode at " + connectionString);
        connectionFactory = new JedisConnectionFactory(jedisShardInfo);
    } else if (RedisUtils.REDIS_MODE_SENTINEL.getValue().equals(redisMode)) {
        final String[] sentinels = connectionString.split("[,]");
        final String sentinelMaster = context.getProperty(RedisUtils.SENTINEL_MASTER).evaluateAttributeExpressions().getValue();
        final RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration(sentinelMaster, new HashSet<>(getTrimmedValues(sentinels)));
        final JedisShardInfo jedisShardInfo = createJedisShardInfo(sentinels[0], timeout, password);
        logger.info("Connecting to Redis in sentinel mode...");
        logger.info("Redis master = " + sentinelMaster);
        for (final String sentinel : sentinels) {
            logger.info("Redis sentinel at " + sentinel);
        }
        connectionFactory = new JedisConnectionFactory(sentinelConfiguration, poolConfig);
        connectionFactory.setShardInfo(jedisShardInfo);
    } else {
        final String[] clusterNodes = connectionString.split("[,]");
        final Integer maxRedirects = context.getProperty(RedisUtils.CLUSTER_MAX_REDIRECTS).asInteger();
        final RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(getTrimmedValues(clusterNodes));
        clusterConfiguration.setMaxRedirects(maxRedirects);
        logger.info("Connecting to Redis in clustered mode...");
        for (final String clusterNode : clusterNodes) {
            logger.info("Redis cluster node at " + clusterNode);
        }
        connectionFactory = new JedisConnectionFactory(clusterConfiguration, poolConfig);
    }
    connectionFactory.setUsePool(true);
    connectionFactory.setPoolConfig(poolConfig);
    connectionFactory.setDatabase(dbIndex);
    connectionFactory.setTimeout(timeout);
    if (!StringUtils.isBlank(password)) {
        connectionFactory.setPassword(password);
    }
    // need to call this to initialize the pool/connections
    connectionFactory.afterPropertiesSet();
    return connectionFactory;
}
Also used : RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) JedisShardInfo(redis.clients.jedis.JedisShardInfo) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) HashSet(java.util.HashSet)

Aggregations

RedisClusterConfiguration (org.springframework.data.redis.connection.RedisClusterConfiguration)18 Bean (org.springframework.context.annotation.Bean)10 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)8 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)7 RedisSentinelConfiguration (org.springframework.data.redis.connection.RedisSentinelConfiguration)6 RedisNode (org.springframework.data.redis.connection.RedisNode)5 JedisConnectionFactory (org.springframework.data.redis.connection.jedis.JedisConnectionFactory)5 HashMap (java.util.HashMap)4 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 MapPropertySource (org.springframework.core.env.MapPropertySource)4 ClientOptions (io.lettuce.core.ClientOptions)3 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)3 ClusterTopologyRefreshOptions (io.lettuce.core.cluster.ClusterTopologyRefreshOptions)3 SocketOptions (io.lettuce.core.SocketOptions)2 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)2 Duration (java.time.Duration)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.jupiter.api.Test)2