Redis單機版和集羣版測試、對於java客戶端的區別

 

1、單機版是直接從JedisPool的getresource()方法獲取Jedis對象;

 <!-- jedis客戶端單機版 -->
    <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.22.16"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>

2、集羣版本是JedisCluster對象可以直接執行Redis的命令

<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
</bean>

 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<HostAndPort> 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();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章