使用jedisCluster连接Redis集群(xml和代码配置)

使用jedisCluster连接Redis集群(xml和代码配置)


1.JedisCluster简介

​ 一种优雅的连接Redis集群的工具,配置连接池之后自动获取和释放连接.

2.代码配置方式

            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(100);
            jedisPoolConfig.setMaxIdle(30);
            jedisPoolConfig.setMaxWaitMillis(200);
            jedisPoolConfig.setTestOnBorrow(true);
            pool = new JedisPool(jedisPoolConfig, "IP", 7000, 2000, 10000, null, Protocol.DEFAULT_DATABASE, null);

            HostAndPort hostAndPort = new HostAndPort("IP", 7000);
            HostAndPort hostAndPort1 = new HostAndPort("IP", 7001);
            HostAndPort hostAndPort2 = new HostAndPort("IP", 7002);
            HostAndPort hostAndPort3 = new HostAndPort("IP", 7003);
            HostAndPort hostAndPort4 = new HostAndPort("IP", 7004);
            HostAndPort hostAndPort5 = new HostAndPort("IP", 7005);

            Set hostAndPortSet = new HashSet<>();
            hostAndPortSet.add(hostAndPort);
            hostAndPortSet.add(hostAndPort1);
            hostAndPortSet.add(hostAndPort2);
            hostAndPortSet.add(hostAndPort3);
            hostAndPortSet.add(hostAndPort4);
            hostAndPortSet.add(hostAndPort5);


            JedisCluster jedis = new JedisCluster(hostAndPortSet,jedisPoolConfig);
            jedis.set(k,v);
            jedis.get(k);

3.XML配置方式

​ 配置完成后可以自动注入,使用比较方便.

@Autowired
 private JedisCluster jedis;

3.1 加载配置文件


    
    

3.2 redis.properties

#JedisPoolConfig的参数
#最大连接数
redis.maxTotal=30
#最大空闲时间
redis.maxIdle=100
redis.maxWait=2000
redis.testOnBorrow=true
#主机和端口号
redis.host1=IP
redis.port1=7000
redis.host2=IP
redis.port2=7001
redis.host3=IP
redis.port3=7002
redis.host4=IP
redis.port4=7003
redis.host5=IP
redis.port5=7004
redis.host6=IP
redis.port6=7005

3.3 applicationContext-redis.xml




    
    
        
        
        
        
    

    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    
    
        
        
    

    
        
            
                
                
                
                
                
                
            
        
        
    


你可能感兴趣的:(使用jedisCluster连接Redis集群(xml和代码配置))