Redis单机版和集群版测试、对于java客户端的区别

 

1、单机版是直接从JedisPool的getresource()方法获取Jedis对象;

 
    
        
        
        
    

2、集群版本是JedisCluster对象可以直接执行Redis的命令


 3、Redis单机版和集群版测试用例:

package com.taotao.rest.jedis;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashSet;

/**
 * @创建人 steffens
 * @创建时间 2019/6/14
 * @描述 文件创建
 */
public class JedisTest {

    @Test
    public void testJedisSingle() {
        //创建jedis对象
        Jedis jedis = new Jedis("192.168.22.16", 6379);
        //调用jedis对象的方法,方法同redis同命令相同
        jedis.set("mykey", "test my key");
        String mykey = jedis.get("mykey");
        System.out.println("mykey:" + mykey);
        //关闭jedis
        jedis.close();
    }

    /**
     * 使用连接池
     */
    @Test
    public void testJedisPool() {
        //创建连接池
        JedisPool jedisPool = new JedisPool("192.168.22.16", 6379);
        //从连接池获取Jedis对象
        Jedis resource = jedisPool.getResource();
        resource.set("keysource", "===>hello world");
        String keysource = resource.get("keysource");
        System.out.println("KeySource:" + keysource);
        //释放连接池,关闭redis
        resource.close();
        jedisPool.close();
    }

    /**
     * 集群版本
     */
    @Test
    public void testJedisCluster() {
        HashSet nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.22.16", 7001));
        nodes.add(new HostAndPort("192.168.22.16", 7002));
        nodes.add(new HostAndPort("192.168.22.16", 7003));
        nodes.add(new HostAndPort("192.168.22.16", 7004));
        nodes.add(new HostAndPort("192.168.22.16", 7005));
        nodes.add(new HostAndPort("192.168.22.16", 7006));

        JedisCluster jedisCluster = new JedisCluster(nodes);
        jedisCluster.set("keycluster1", "==========cluster1 hello world ~~");
        String keycluster = jedisCluster.get("keycluster");
        System.out.println("Print keycluster:" + keycluster);

        try {
            jedisCluster.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试SpringJedis单机版
     */
    @Test
    public void testSpringJedisSingle() {
        //初始化容器
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-jedis.xml");
        //从容器中获取连接池
        JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
        //从连接池获取jedis对象
        Jedis jedis = pool.getResource();
        jedis.set("springkey", "This is xml config 1234");
        String springkey = jedis.get("springkey");
        System.out.println("Springkey is: " + springkey);
        //关闭资源
        jedis.close();
        pool.close();
    }

    /**
     * 测试SpringJedisCluster版本
     */
    @Test
    public void testSpringJedisCluster() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-jedis.xml");
        JedisCluster jedisCluster  = (JedisCluster) applicationContext.getBean("jedisCluster");
        jedisCluster .set("redisclusterkey", "THis is cluster key.oksteffens");
        String redisclusterkey = jedisCluster .get("redisclusterkey");
        System.out.println("RedisClusterKey value :" + redisclusterkey);
        try {
            jedisCluster .close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        applicationContext.destroy();
    }
}

 

你可能感兴趣的:(Redis单机版和集群版测试、对于java客户端的区别)