redis-java客户端jedis测试

【README】

本文旨在记录 jedis 包连接redis集群的开发方式,并对api做简单测试,api不会深入;

maven引入的 jedis依赖

<dependencies>
  	<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.0.0</version>
		</dependency>
  </dependencies>

【1】 java redis 客户端

/**
 * redis集群客户端 
 * @author pacoson
 */
public class RedisClusterClient {
    private static JedisCluster jedis = null;
    //可用连接实例的最大数目,默认为8;
    //如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
    private static Integer MAX_TOTAL = 1024;
    //控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8
    private static Integer MAX_IDLE = 200;
    //等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
    //如果超过等待时间,则直接抛出JedisConnectionException
    private static Integer MAX_WAIT_MILLIS = 10000;
    //在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
    //如果为true,则得到的jedis实例均是可用的
    private static Boolean TEST_ON_BORROW = true;
    //在空闲时检查有效性, 默认false
    private static Boolean TEST_WHILE_IDLE = true;
    //是否进行有效性检查
    private static Boolean TEST_ON_RETURN = true;

    private RedisClusterClient(){
    }
    /**
     * 静态块,初始化Redis连接池
     */
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
        /*注意:
            在高版本的jedis jar包,比如本版本2.9.0,JedisPoolConfig没有setMaxActive和setMaxWait属性了
            这是因为高版本中官方废弃了此方法,用以下两个属性替换。
            maxActive  ==>  maxTotal
            maxWait==>  maxWaitMillis
         */
            config.setMaxTotal(MAX_TOTAL);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT_MILLIS);
            config.setTestOnBorrow(TEST_ON_BORROW);
            config.setTestWhileIdle(TEST_WHILE_IDLE);
            config.setTestOnReturn(TEST_ON_RETURN);
            /* redis节点集合 */
            Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
            jedisClusterNode.add(new HostAndPort("192.168.163.201", 6379));
            jedisClusterNode.add(new HostAndPort("192.168.163.201", 6380));
            jedisClusterNode.add(new HostAndPort("192.168.163.201", 6381));
            
            jedisClusterNode.add(new HostAndPort("192.168.163.202", 6379));
            jedisClusterNode.add(new HostAndPort("192.168.163.202", 6380));
            jedisClusterNode.add(new HostAndPort("192.168.163.202", 6381));
            
            jedisClusterNode.add(new HostAndPort("192.168.163.203", 6379));
            jedisClusterNode.add(new HostAndPort("192.168.163.203", 6380));
            jedisClusterNode.add(new HostAndPort("192.168.163.203", 6381));
            jedis = new JedisCluster(jedisClusterNode,1000,1000,5,config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public final static JedisCluster getJedis(){
        return jedis;
    }
}

【2】测试用例

/**
 * jedis测试用例 
 */
public class RedisClusterApiTest {
	public static void main(String[] args) {
		/* 获取redis集群客户端  */ 
		JedisCluster jedisClient = RedisClusterClient.getJedis(); 
		jedisClient.set("cluster","hello world");
	    System.out.println(jedisClient.get("cluster"));// hello world 
	}
}

【3】centos登录redis集群,通过命令行查看 cluster key的值;

[root@centos201 ~]# /usr/local/redis-cluster/bin/redis-cli -c -h 192.168.163.201 -p 6381
192.168.163.201:6381> get cluster
-> Redirected to slot [14041] located at 192.168.163.203:6379
"hello world"

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值