Search in sources :

Example 6 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project LinkAgent by shulieTech.

the class Key method create.

LettuceConnectionFactory create() {
    RedisServerMatchStrategy matcher = null;
    if (biz == null) {
        return null;
    }
    Object standaloneConfiguration = biz.getStandaloneConfiguration();
    Object clusterConfiguration = biz.getClusterConfiguration();
    Object sentinelConfiguration = biz.getSentinelConfiguration();
    if (clusterConfiguration != null) {
        matcher = CLUSTER_MODE_MATCHER;
        Set<RedisNode> nodes = ((RedisClusterConfiguration) clusterConfiguration).getClusterNodes();
        String password = null;
        try {
            char[] passwdbyte = ((RedisClusterConfiguration) clusterConfiguration).getPassword().get();
            password = new String(passwdbyte);
        } catch (NoSuchElementException t) {
        // ignore
        }
        List<Key> keys = new ArrayList<Key>();
        Iterator<RedisNode> iterator = nodes.iterator();
        while (iterator.hasNext()) {
            RedisNode node = iterator.next();
            keys.add(new Key(node.getHost(), node.getPort(), 0, password));
        }
        ShadowRedisConfig shadowRedisConfig = matcher.getConfig(keys);
        String shadowPassword = shadowRedisConfig.getPassword();
        /**
         * 创建影子配置
         */
        RedisClusterConfiguration shadowRedisClusterConfiguration = new RedisClusterConfiguration();
        /**
         * 填充密码
         */
        if (!StringUtil.isEmpty(shadowPassword)) {
            shadowRedisClusterConfiguration.setPassword(shadowPassword);
        }
        /**
         * 填充连接地址
         */
        for (String configNode : shadowRedisConfig.getNodes().split(",")) {
            shadowRedisClusterConfiguration.addClusterNode(RedisNode.newRedisNode().listeningAt(configNode.split(":")[0], Integer.parseInt(configNode.split(":")[1])).build());
        }
        /**
         * 构建连接
         */
        LettuceConnectionFactory shadowConnectionFactory = new LettuceConnectionFactory(shadowRedisClusterConfiguration);
        /**
         * 初始化连接
         */
        shadowConnectionFactory.afterPropertiesSet();
        return shadowConnectionFactory;
    } else if (sentinelConfiguration != null) {
        RedisSentinelConfiguration redisSentinelConfiguration = (RedisSentinelConfiguration) sentinelConfiguration;
        String masterName = redisSentinelConfiguration.getMaster().getName();
        String password = null;
        try {
            char[] passwdbyte = redisSentinelConfiguration.getPassword().get();
            password = new String(passwdbyte);
        } catch (NoSuchElementException e) {
        // 
        }
        Integer database = Integer.parseInt(String.valueOf(redisSentinelConfiguration.getDatabase()));
        Set<RedisNode> nodes = redisSentinelConfiguration.getSentinels();
    } else /**
     *standalone肯定不为空 放后面 因为可能为localhost
     */
    if (standaloneConfiguration != null && !("localhost".equals(((RedisStandaloneConfiguration) standaloneConfiguration).getHostName()))) {
        matcher = SINGLE_MODE_MATCHER;
        RedisStandaloneConfiguration configuration = (RedisStandaloneConfiguration) standaloneConfiguration;
        String host = configuration.getHostName();
        Integer port = configuration.getPort();
        Integer db = configuration.getDatabase();
        String password = null;
        try {
            char[] passwdbyte = configuration.getPassword().get();
            password = new String(passwdbyte);
        } catch (NoSuchElementException e) {
        // 
        }
        ShadowRedisConfig shadowRedisConfig = matcher.getConfig(new Key(host, port, db, password));
        if (shadowRedisConfig == null) {
            return null;
        } else {
            String[] spilter = shadowRedisConfig.getNodes().split(":");
            String shadowHost = spilter[0];
            Integer shadowPort = Integer.valueOf(spilter[1]);
            Integer shadowDb = shadowRedisConfig.getDatabase();
            String shadowPassword = shadowRedisConfig.getPassword();
            LettuceConnectionFactory shadowConnectionFactory = new LettuceConnectionFactory();
            shadowConnectionFactory.setHostName(shadowHost);
            shadowConnectionFactory.setPort(shadowPort);
            /**
             * 填充密码
             */
            if (shadowPassword != null) {
                shadowConnectionFactory.setPassword(shadowPassword);
            }
            /**
             * 填充db
             */
            if (shadowDb != null) {
                shadowConnectionFactory.setDatabase(shadowDb);
            } else {
                shadowConnectionFactory.setDatabase(biz.getDatabase());
            }
            /**
             * 填充额外属性
             */
            extraProperties(biz, shadowConnectionFactory);
            // 初始化
            shadowConnectionFactory.afterPropertiesSet();
            /*    DefaultListableBeanFactory defaultListableBeanFactory = PradarSpringUtil.getBeanFactory();*/
            return shadowConnectionFactory;
        }
    }
    return null;
}
Also used : RedisNode(org.springframework.data.redis.connection.RedisNode) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) RedisServerMatchStrategy(com.pamirs.attach.plugin.common.datasource.redisserver.RedisServerMatchStrategy) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig)

Example 7 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project incubator-shenyu by apache.

the class RateLimiterPluginDataHandlerTest method redisRedisClusterConfigurationPropertiesTest.

/**
 * redisStandaloneConfiguration property test case.
 */
@Test
public void redisRedisClusterConfigurationPropertiesTest() {
    RedisClusterConfiguration configuration = ReflectionTestUtils.invokeMethod(rateLimiterPluginDataHandler, "redisClusterConfiguration", generateRateLimiterConfig("localhost:2181;localhost:2182"));
    assertNotNull(configuration);
    assertEquals(RedisPassword.of(PASSWORD_TEST_VALUE), configuration.getPassword());
    assertEquals(Collections.unmodifiableSet(Sets.newHashSet(generateRedisNode(PORT_TEST_VALUE_1), generateRedisNode(PORT_TEST_VALUE_2))), configuration.getClusterNodes());
}
Also used : RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Test(org.junit.jupiter.api.Test)

Example 8 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project incubator-shenyu by apache.

the class RateLimiterPluginDataHandler method redisClusterConfiguration.

private RedisClusterConfiguration redisClusterConfiguration(final RateLimiterConfig rateLimiterConfig) {
    RedisClusterConfiguration config = new RedisClusterConfiguration();
    config.setClusterNodes(createRedisNode(rateLimiterConfig.getUrl()));
    if (rateLimiterConfig.getPassword() != null) {
        config.setPassword(RedisPassword.of(rateLimiterConfig.getPassword()));
    }
    return config;
}
Also used : RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration)

Example 9 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project Sermant by huaweicloud.

the class RedisConnCluster method getConnectionFactory.

/**
 * 往IOC容器中注册RedisConnectionFactory对象
 * <p>
 * 主要是针对redis集群的配置,暂时不实现,只实现单机版
 *
 * @return LettuceConnectionFactory
 */
@Bean(destroyMethod = "destroy")
@Conditional(value = { RedisClusterCondition.class })
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public LettuceConnectionFactory getConnectionFactory() {
    // 集群配置
    Map<String, Object> source = new HashMap<>();
    source.put("spring.redis.cluster.nodes", nodes);
    source.put("spring.redis.timeout", timeout);
    source.put("spring.redis.database", database);
    source.put("spring.redis.password", password);
    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
    redisClusterConfiguration.setMaxRedirects(redirects);
    // 连接池配置
    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMaxIdle(maxIdle);
    genericObjectPoolConfig.setMinIdle(minIdle);
    genericObjectPoolConfig.setMaxTotal(maxActive);
    genericObjectPoolConfig.setMaxWaitMillis(maxWait);
    genericObjectPoolConfig.setMaxWaitMillis(MAX_WAIT_MS);
    genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(EVICTION_RUNS_MS);
    // 开启自适应集群拓扑刷新和周期拓扑刷新
    ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enableAllAdaptiveRefreshTriggers().adaptiveRefreshTriggersTimeout(Duration.ofSeconds(REFRESH_TRIGGERS_TIMEOUT)).enablePeriodicRefresh(Duration.ofSeconds(PERIODIC_REFRESH)).build();
    ClientResources clientResources = DefaultClientResources.builder().dnsResolver(DnsResolvers.JVM_DEFAULT).build();
    final SocketOptions socketOptions = SocketOptions.builder().connectTimeout(Duration.ofMillis(timeout)).build();
    final ClientOptions clientOptions = ClusterClientOptions.builder().socketOptions(socketOptions).autoReconnect(true).topologyRefreshOptions(clusterTopologyRefreshOptions).build();
    LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig).clientOptions(clientOptions).clientResources(clientResources).commandTimeout(Duration.ofMillis(timeout)).build();
    return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) HashMap(java.util.HashMap) ClientResources(io.lettuce.core.resource.ClientResources) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) SocketOptions(io.lettuce.core.SocketOptions) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) ClusterTopologyRefreshOptions(io.lettuce.core.cluster.ClusterTopologyRefreshOptions) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) MapPropertySource(org.springframework.core.env.MapPropertySource) LettuceClientConfiguration(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) Scope(org.springframework.context.annotation.Scope) Conditional(org.springframework.context.annotation.Conditional) Bean(org.springframework.context.annotation.Bean)

Example 10 with RedisClusterConfiguration

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

the class RedisConfig method redisClusterConfig.

/**
 * 配置第一个数据源的
 */
@Bean("redisClusterConfig")
@Primary
public RedisClusterConfiguration redisClusterConfig() {
    Map<String, Object> source = new HashMap<>(8);
    source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.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) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

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