java操作redis集群配置[可配置密码]和工具类

导包:


            redis.clients
            jedis
            2.9.0
        
        
            org.apache.commons
            commons-pool2
            2.4.2
        

版本:jedis 2.9.0[此版本可以 加入密码配置]+commons-pools2.4.2


配置:

  
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    
    
        
        
    

    
        
        
    

    
        
        
    

    
        
        
    

    
        
        
    

    
        
        
    

    
        
            
                
                
                
                
                
                
            
        
        
        
        
        password" value="${redis.clusterpassword}"/>
        
            
        
    
    

创建cache.properties:

redis.host =192.168.xx.xxx
redis.port1=7001

redis.port2=7002

redis.port3=7003

redis.port4=7004

redis.port5=7005

redis.port6=7006

redis.clusterpassword=xxxxxxx

创建接口JedisClient:

import org.codehaus.jackson.type.TypeReference;

import java.util.List;

/**
 * Created by gzy on 2017/11/17 17:16.
 */

public interface JedisClient {
    String get(String key);

     T get(String key, TypeReference clazz);

     T get(String key, Class clazz);

    String get(String key, int select);

    void setAndExpire(String key, Object o, int expire);

    Long rpush(String key, String string);

    //    Long del(String... keys);
    Long lpush(String key, String string);

    void set(String key, Object o);

    String set(String key, String value);

    String hget(String hkey, String key);

    long hset(String hkey, String key, String value);

    long incr(String key);

    long expire(String key, int second);

    long ttl(String key);

    long del(String key);

    long hdel(String hkey, String key);

    Boolean exists(String key);

    Long decr(String key);

    List brpop(int timeout, String key);

}

jedisClient实现类JedisClientCluster:

/**
 * Created by gzy on 2017/11/17 17:17.
 */

import org.codehaus.jackson.type.TypeReference;
import com.thinkgem.jeesite.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.JedisCluster;

import java.util.List;

public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedisCluster;

    public  T get(String key, TypeReference clazz) {
        String json = jedisCluster.get(key);
        if (StringUtils.isNotEmpty(json)) {
            return JsonUtil.Json2Object(json, clazz);
        } else {
            return null;
        }
    }

    public  T get(String key, Class clazz) {
        String json = jedisCluster.get(key);
        if (StringUtils.isNotEmpty(json)) {
            return JsonUtil.Json2Object(json, clazz);
        } else {
            return null;
        }
    }

    public void set(String key, Object o) {
        String json = JsonUtil.Object2Json(o);
        jedisCluster.set(key, json);
    }

    public void setAndExpire(String key, Object o, int expire) {
        String json = JsonUtil.Object2Json(o);
        jedisCluster.set(key, json);
        jedisCluster.expire(key, expire);
    }

//    public long del(String key) {
//        return jedisCluster.del(key);
//    }

    public String get(String key) {
        return jedisCluster.get(key);
    }

    public String get(String key, int select) {
        jedisCluster.select(select);
        return jedisCluster.get(key);
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public String hget(String hkey, String key) {
        return jedisCluster.hget(hkey, key);
    }

    @Override
    public long hset(String hkey, String key, String value) {
        return jedisCluster.hset(hkey, key, value);
    }

    @Override
    public long incr(String key) {
        return jedisCluster.incr(key);
    }

    public Long decr(String key) {
        return jedisCluster.decr(key);
    }

    @Override
    public long expire(String key, int second) {
        return jedisCluster.expire(key, second);
    }

    @Override
    public long ttl(String key) {
        return jedisCluster.ttl(key);
    }

    @Override
    public long del(String key) {
        return jedisCluster.del(key);
    }

    @Override
    public long hdel(String hkey, String key) {

        return jedisCluster.hdel(hkey, key);
    }

    public Long rpush(String key, String string) {
        return jedisCluster.rpush(key, string);
    }

    public Long lpush(String key, String string) {
        return jedisCluster.lpush(key, string);
    }

    public Boolean exists(String key) {
        return jedisCluster.exists(key);
    }

    public List brpop(int timeout, String key) {
        return jedisCluster.brpop(timeout, key);
    }

}

创建测试类ClusterTest:

import xx.xx.xxx.xxxx.JedisClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by gzy on 2017/11/20 15:22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context-cache.xml")
public class ClusterTest {
   // private static JedisClientCluster redisCluster = SpringContextHolder.getBean("jedisClientCluster");
    @Autowired
    private JedisClient jedisClient;

    @Test
    public void testJCluster() {
	jedisClient.set("test:phone:" + "11111111","hhha");
	String result = jedisClient.get("test:phone:" + "11111111")
 	System.out.println("result==="+result)

}}


注意Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误.


你可能感兴趣的:(java)