(08)redis之使用java客户端、spring连接redis、redis集群示例

一、java代码连接

  1、新建工程,并引入以下包:

  jedis-2.7.0.jar、commons-pool2-2.3.jar、junit-4.10.jar

  2、单实例连接

  /**
     * 单实例连接
     */
    @Test
    public  void jedisClient(){
        //创建一个Jedis的连接
        Jedis jedis=new Jedis("192.168.7.151",6379);
        //可以选择库
        jedis.select(2);
        //写入值
        jedis.set("jediskey", "哈哈哈哈");
        //获取值
        String jedisStr=jedis.get("jediskey");
        System.out.println(jedisStr);
        Assert.assertEquals("哈哈哈哈",jedisStr);
        //关闭连接
        jedis.close();
    }

  2、连接池连接

  /**
     * 连接池连接
     */
    @Test
    public void jedisPool(){
        //创建一连接池对象
        JedisPool pool=new JedisPool("192.168.7.151",6379);
        //从连接池中获得连接
        Jedis jedis=pool.getResource();
        jedis.select(3);
        jedis.set("jeds", "jedis连接池");
        String getStr=jedis.get("jeds");
        System.out.println(getStr);
        //关闭连接
        jedis.close();
        //关闭连接池
        pool.close();
        Assert.assertEquals("jedis连接池", getStr);
    }

  3、JedisCluster连接集群

  /**
     * JedisCluster 连接集群
     * @throws Exception
     */
    @Test
    public void testJedisCluster() throws Exception {
        //创建一连接,JedisCluster对象,在系统中是单例存在
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.7.151", 7001));
        nodes.add(new HostAndPort("192.168.7.151", 7002));
        nodes.add(new HostAndPort("192.168.7.151", 7003));
        nodes.add(new HostAndPort("192.168.7.151", 7004));
        nodes.add(new HostAndPort("192.168.7.151", 7005));
        nodes.add(new HostAndPort("192.168.7.151", 7006));
        nodes.add(new HostAndPort("192.168.7.151", 7007));
        nodes.add(new HostAndPort("192.168.7.151", 7008));
        JedisCluster cluster = new JedisCluster(nodes);
        //执行JedisCluster对象中的方法,方法和redis一一对应。
        cluster.set("cluster-test", "my jedis cluster test");
        String result = cluster.get("cluster-test");
        System.out.println(result);
        //程序结束时需要关闭JedisCluster对象
        cluster.close();
    }

二、使用spring配置单机redis

  1、新建工程,并引入以下包:

  commons-pool2-2.3.jar
  jedis-2.7.0.jar
  junit-4.10.jar
  spring-expression-3.2.0.RELEASE.jar
  spring-core-3.2.0.RELEASE.jar
  spring-context-3.2.0.RELEASE.jar
  spring-beans-3.2.0.RELEASE.jar
  commons-logging-1.1.1.jar

  2、工程中新建Source Folder目录:config,config里面添加applicationContext.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="10" />
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="false" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>

    <!-- redis单机 通过连接池 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="192.168.7.151" />
        <constructor-arg name="port" value="6379" />
    </bean>
</beans>

  3、测试方法如下:

  /**
     * spring配置单机redis
     */
    @Test
    public void getJedisPool(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
        Jedis jedis=pool.getResource();
        jedis.set("spring","spring测试redis");
        String spring = jedis.get("spring");
        System.out.println(spring);
        jedis.close();
        pool.close();
        applicationContext.close(); 
    }

三、使用spring配置redis集群

  1、工程与引入的包如上

  2、新建applicationContext2.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="10" />
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="false" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>

    <!-- redis集群 -->
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <constructor-arg index="0">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7001"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7002"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7003"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7004"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7005"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7006"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7007"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.7.151"></constructor-arg>
                    <constructor-arg index="1" value="7008"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>
    </bean>
    
</beans>

  3、测试方法如下:

  /**
     * spring配置redis集群
     */
    @Test
    public void getJedisPool(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext2.xml");
        JedisCluster jedisCluster = (JedisCluster) applicationContext.getBean("jedisCluster");
        jedisCluster.set("jedisCluster","spring测试jedisCluster");
        String spring = jedisCluster.get("jedisCluster");
        System.out.println(spring);
        applicationContext.close(); 
    }
原文地址:https://www.cnblogs.com/javasl/p/12099052.html