Search in sources :

Example 1 with RedisClusterConfiguration

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

the class RedisConnectionConfiguration method getClusterConfiguration.

/**
 * Create a {@link RedisClusterConfiguration} if necessary.
 * @return {@literal null} if no cluster settings are set.
 */
protected final RedisClusterConfiguration getClusterConfiguration() {
    if (this.clusterConfiguration != null) {
        return this.clusterConfiguration;
    }
    if (this.properties.getCluster() == null) {
        return null;
    }
    RedisProperties.Cluster clusterProperties = this.properties.getCluster();
    RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes());
    if (clusterProperties.getMaxRedirects() != null) {
        config.setMaxRedirects(clusterProperties.getMaxRedirects());
    }
    config.setUsername(this.properties.getUsername());
    if (this.properties.getPassword() != null) {
        config.setPassword(RedisPassword.of(this.properties.getPassword()));
    }
    return config;
}
Also used : RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration)

Example 2 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project pancm_project by xuwujing.

the class RedisConfig method redisClusterConfiguration.

/**
 * Redis集群的配置
 *
 * @return the redis cluster configuration
 */
@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
    // Set<RedisNode> clusterNodes
    String[] serverArray = clusterNodes.split(",");
    Set<RedisNode> nodes = new HashSet<RedisNode>();
    for (String ipPort : serverArray) {
        String[] ipAndPort = ipPort.split(":");
        nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1])));
    }
    redisClusterConfiguration.setClusterNodes(nodes);
    redisClusterConfiguration.setMaxRedirects(mmaxRedirectsac);
    return redisClusterConfiguration;
}
Also used : RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) RedisNode(org.springframework.data.redis.connection.RedisNode) HashSet(java.util.HashSet) Bean(org.springframework.context.annotation.Bean)

Example 3 with RedisClusterConfiguration

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

the class TBRedisClusterConfiguration method loadFactory.

public JedisConnectionFactory loadFactory() {
    RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
    clusterConfiguration.setClusterNodes(getNodes(clusterNodes));
    clusterConfiguration.setMaxRedirects(maxRedirects);
    clusterConfiguration.setPassword(password);
    if (useDefaultPoolConfig) {
        return new JedisConnectionFactory(clusterConfiguration);
    } else {
        return new JedisConnectionFactory(clusterConfiguration, buildPoolConfig());
    }
}
Also used : JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration)

Example 4 with RedisClusterConfiguration

use of org.springframework.data.redis.connection.RedisClusterConfiguration in project JAQU-CAZ-Payments-API by InformedSolutions.

the class RedisConfiguration method lettuceConnectionFactory.

/**
 * Customised lettuce connection factory builder.
 *
 * @return A lettuce connection factory instance.
 */
@Bean
@Profile("!integration-tests")
public LettuceConnectionFactory lettuceConnectionFactory() {
    log.info("Creating redis-connection for a cluster");
    LettuceClientConfiguration clientConfiguration;
    if (redisUseSsl) {
        log.info("Configuring redis connection using SSL");
        clientConfiguration = LettuceClientConfiguration.builder().useSsl().disablePeerVerification().and().commandTimeout(Duration.ofSeconds(commandTimeout)).build();
    } else {
        clientConfiguration = LettuceClientConfiguration.builder().commandTimeout(Duration.ofSeconds(commandTimeout)).build();
    }
    RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
    clusterConfiguration.clusterNode(redisClusterEndpoint, redisClusterPort);
    return new LettuceConnectionFactory(clusterConfiguration, clientConfiguration);
}
Also used : LettuceClientConfiguration(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) Profile(org.springframework.context.annotation.Profile) Bean(org.springframework.context.annotation.Bean)

Example 5 with RedisClusterConfiguration

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

the class SpringRedisClientInfoCollector method attachment.

/**
 * 业务流量的信息采集
 *
 * @param advice
 */
protected void attachment(Advice advice) {
    try {
        if (Pradar.isClusterTest()) {
            return;
        }
        LettuceConnectionFactory biz = (LettuceConnectionFactory) advice.getTarget();
        Object standaloneConfiguration = biz.getStandaloneConfiguration();
        Object clusterConfiguration = biz.getClusterConfiguration();
        Object sentinelConfiguration = biz.getSentinelConfiguration();
        if (clusterConfiguration != null) {
            Set<RedisNode> nodeSet = ((RedisClusterConfiguration) clusterConfiguration).getClusterNodes();
            StringBuilder builder = new StringBuilder();
            for (RedisNode node : nodeSet) {
                String host = node.getHost();
                String port = String.valueOf(node.getPort());
                builder.append(host.concat(":").concat(port)).append(",");
            }
            String nodes = builder.deleteCharAt(builder.length() - 1).toString();
            String password = null;
            try {
                char[] passwdbyte = ((RedisClusterConfiguration) clusterConfiguration).getPassword().get();
                password = new String(passwdbyte);
            } catch (NoSuchElementException e) {
            // 
            }
            ResourceManager.set(new Attachment(Arrays.asList(nodes.split(",")), "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceClusterTemplate().setNodes(nodes).setPassword(password)));
        } 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> nodeSet = redisSentinelConfiguration.getSentinels();
            StringBuilder builder = new StringBuilder();
            for (RedisNode node : nodeSet) {
                String host = node.getHost();
                String port = String.valueOf(node.getPort());
                builder.append(host.concat(":").concat(port)).append(",");
            }
            String nodes = builder.deleteCharAt(builder.length() - 1).toString();
            List<String> sentinelRemoteAddress = new ArrayList<String>();
            try {
                AbstractRedisClient client = Reflect.on(biz).get("client");
                ChannelGroup channelGroup = Reflect.on(client).get("channels");
                Iterator iterator = channelGroup.iterator();
                while (iterator.hasNext()) {
                    Object o = iterator.next();
                    if (o instanceof NioSocketChannel) {
                        NioSocketChannel channel = (NioSocketChannel) o;
                        InetSocketAddress inetSocketAddress = channel.remoteAddress();
                        String host = inetSocketAddress.getAddress().getHostName();
                        String port = String.valueOf(inetSocketAddress.getPort());
                        sentinelRemoteAddress.add(host.concat(":").concat(port));
                    }
                }
            } catch (Throwable t) {
                logger.error(Throwables.getStackTraceAsString(t));
            }
            sentinelRemoteAddress.addAll(Arrays.asList(nodes.split(",")));
            ResourceManager.set(new Attachment(sentinelRemoteAddress, "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceSentinelTemplate().setNodes(nodes).setPassword(password).setMaster(masterName).setDatabase(database)));
        } else /**
         *standalone肯定不为空 放后面 因为可能为localhost
         */
        if (standaloneConfiguration != null && !("localhost".equals(((RedisStandaloneConfiguration) standaloneConfiguration).getHostName()))) {
            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) {
            // 
            }
            String node = host.concat(":").concat(String.valueOf(port));
            ResourceManager.set(new Attachment(Arrays.asList(node), "redis-lettuce", new String[] { "redis" }, new RedisTemplate.LettuceSingleTemplate().setNodes(node).setPassword(password).setDatabase(db)));
        }
    } catch (Throwable t) {
        logger.error("[redis-lettuce] collector spring biz info error , {}", Throwables.getStackTraceAsString(t));
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) RedisStandaloneConfiguration(org.springframework.data.redis.connection.RedisStandaloneConfiguration) RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) RedisTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate) AbstractRedisClient(io.lettuce.core.AbstractRedisClient) RedisNode(org.springframework.data.redis.connection.RedisNode) RedisClusterConfiguration(org.springframework.data.redis.connection.RedisClusterConfiguration) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelGroup(io.netty.channel.group.ChannelGroup)

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