Search in sources :

Example 1 with RedisSentinelConfiguration

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

the class RedisObjectFactory method potentiallyGetSentinelConfig.

private RedisSentinelConfiguration potentiallyGetSentinelConfig(final BaseRedisProperties redis) {
    if (redis.getSentinel() == null) {
        return null;
    }
    RedisSentinelConfiguration sentinelConfig = null;
    if (redis.getSentinel() != null) {
        sentinelConfig = new RedisSentinelConfiguration().master(redis.getSentinel().getMaster());
        sentinelConfig.setSentinels(createRedisNodesForProperties(redis));
    }
    return sentinelConfig;
}
Also used : RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration)

Example 2 with RedisSentinelConfiguration

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

the class RedisConnectionConfiguration method getSentinelConfig.

protected final RedisSentinelConfiguration getSentinelConfig() {
    if (this.sentinelConfiguration != null) {
        return this.sentinelConfiguration;
    }
    RedisProperties.Sentinel sentinelProperties = this.properties.getSentinel();
    if (sentinelProperties != null) {
        RedisSentinelConfiguration config = new RedisSentinelConfiguration();
        config.master(sentinelProperties.getMaster());
        config.setSentinels(createSentinels(sentinelProperties));
        config.setUsername(this.properties.getUsername());
        if (this.properties.getPassword() != null) {
            config.setPassword(RedisPassword.of(this.properties.getPassword()));
        }
        config.setSentinelUsername(sentinelProperties.getUsername());
        if (sentinelProperties.getPassword() != null) {
            config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword()));
        }
        config.setDatabase(this.properties.getDatabase());
        return config;
    }
    return null;
}
Also used : RedisSentinelConfiguration(org.springframework.data.redis.connection.RedisSentinelConfiguration)

Example 3 with RedisSentinelConfiguration

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

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

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

Aggregations

RedisSentinelConfiguration (org.springframework.data.redis.connection.RedisSentinelConfiguration)10 RedisClusterConfiguration (org.springframework.data.redis.connection.RedisClusterConfiguration)4 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)4 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)3 RedisNode (org.springframework.data.redis.connection.RedisNode)2 JedisConnectionFactory (org.springframework.data.redis.connection.jedis.JedisConnectionFactory)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 HostAndPort (io.lettuce.core.internal.HostAndPort)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 lombok.val (lombok.val)1 Test (org.junit.jupiter.api.Test)1 Bean (org.springframework.context.annotation.Bean)1 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1