JedisCluster 基本代码__写给自己看的

pom.xml
<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
</dependency>
主要代码
Set<HostAndPort> jedisClusterNodes = Sets.newHashSet();
for (String hps : redisProperties.getAddressList()) {
    String[] hp = hps.split(":");
    jedisClusterNodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
}
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxIdle(redisProperties.getMaxIdle());
config.setMaxTotal(redisProperties.getMaxTotal());
config.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
config.setMinIdle(redisProperties.getMinIdle());
config.setTestWhileIdle(true);
JedisCluster jc = new JedisCluster(jedisClusterNodes, Protocol.DEFAULT_TIMEOUT, config);
return jc;

-----------------------------------用springboot@Bean注入到spring中去
 

import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.testng.collections.Sets;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Protocol;

@Configuration
public class RedisTaskConfigurations {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisTaskConfigurations.class);


    @Autowired
    private RedisProperties redisProperties;

    @Bean(name="jedisCluster")
    public JedisCluster getJedisCluster() {
        Set<HostAndPort> jedisClusterNodes = Sets.newHashSet();
        for (String hps : redisProperties.getAddressList()) {
            String[] hp = hps.split(":");
            jedisClusterNodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
        }
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxIdle(redisProperties.getMaxIdle());
        config.setMaxTotal(redisProperties.getMaxTotal());
        config.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
        config.setMinIdle(redisProperties.getMinIdle());
        config.setTestWhileIdle(true);
        JedisCluster jc = new JedisCluster(jedisClusterNodes, Protocol.DEFAULT_TIMEOUT, config);
        return jc;
    }
}

猜你喜欢

转载自blog.csdn.net/maqingbin8888/article/details/81429467