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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值