Search in sources :

Example 1 with RedisNode

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

the class RedisConnectionConfiguration method createSentinels.

private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {
    List<RedisNode> nodes = new ArrayList<>();
    for (String node : sentinel.getNodes()) {
        try {
            String[] parts = StringUtils.split(node, ":");
            Assert.state(parts.length == 2, "Must be defined as 'host:port'");
            nodes.add(new RedisNode(parts[0], Integer.parseInt(parts[1])));
        } catch (RuntimeException ex) {
            throw new IllegalStateException("Invalid redis sentinel property '" + node + "'", ex);
        }
    }
    return nodes;
}
Also used : ArrayList(java.util.ArrayList) RedisNode(org.springframework.data.redis.connection.RedisNode)

Example 2 with RedisNode

use of org.springframework.data.redis.connection.RedisNode 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 RedisNode

use of org.springframework.data.redis.connection.RedisNode 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)

Example 4 with RedisNode

use of org.springframework.data.redis.connection.RedisNode 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 5 with RedisNode

use of org.springframework.data.redis.connection.RedisNode in project jag-file-submission by bcgov.

the class CacheConfiguration method createSentinels.

private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {
    List<RedisNode> nodes = new ArrayList<>();
    for (String node : sentinel.getNodes()) {
        try {
            String[] parts = node.split(":");
            nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
        } catch (RuntimeException ex) {
            throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex);
        }
    }
    return nodes;
}
Also used : ArrayList(java.util.ArrayList) RedisNode(org.springframework.data.redis.connection.RedisNode)

Aggregations

RedisNode (org.springframework.data.redis.connection.RedisNode)8 ArrayList (java.util.ArrayList)4 RedisClusterConfiguration (org.springframework.data.redis.connection.RedisClusterConfiguration)3 RedisSentinelConfiguration (org.springframework.data.redis.connection.RedisSentinelConfiguration)2 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)2 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)2 RedisServerMatchStrategy (com.pamirs.attach.plugin.common.datasource.redisserver.RedisServerMatchStrategy)1 Attachment (com.pamirs.attach.plugin.dynamic.Attachment)1 RedisTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate)1 ShadowRedisConfig (com.pamirs.pradar.internal.config.ShadowRedisConfig)1 AbstractRedisClient (io.lettuce.core.AbstractRedisClient)1 ChannelGroup (io.netty.channel.group.ChannelGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 InetSocketAddress (java.net.InetSocketAddress)1 HashSet (java.util.HashSet)1 Bean (org.springframework.context.annotation.Bean)1