(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(); 
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章