Redis+Spring 整合应用教程


2、redis安装

      安装的前提条件(redis C编写的):
      需要安装gcc:
yum install gcc-c++


      2.1、下载redis的源码包。
      2.2、把源码包上传到linux服务器
      2.3、解压源码包:tar -zxvf redis-3.0.0.tar.gz 
      2.4、Make
      2.5、Make install
      [root@bogon redis-3.0.0]#make install PREFIX=/usr/local/redis(安装目录
 )  


3、启动redis

      3.1、前端启动模式

      进入安装目录:./redis-server

      默认是前端启动模式,端口是6379


3.2、后端启动

      1)从redis的源码目录中复制redis.conf到redis的安装目录。
      2)修改配置文件

      [root@localhost]# ./redis-server redis.conf



4、Redis+spring 整合应用教程

package org.chenzhengyou.redis.jedis;

import org.junit.Before;
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.util.HashSet;

/**
 * @auther 陈郑游
 * @create 2016/12/17 0017
 * @description: redis测试:
 * @problem:
 * @company address:
 * @Modify the time again:
 * @Modify the author again:
 */
public class JedisTest {

    private static final String RedisAddress = "192.168.29.131";
    private static final int RedisPort = 6379;
 


    /**
     * jdies
     */
    @Test
    public void jedis() {

        //创建一个jedis的对象。
        Jedis jedis = new Jedis(RedisAddress, RedisPort);
        //调用jedis对象的方法,方法名称和redis的命令一致。
        jedis.set("key1", "jedis test");
        String string = jedis.get("key1");
        System.out.println(string);
        //关闭jedis。
        jedis.close();
    }

    /**
     * jdies连接池(JedisPool)
     */
    @Test
    public void jedis01() {
        JedisPool jedisPool = new JedisPool(RedisAddress, RedisPort);
        Jedis jedis = jedisPool.getResource();
        jedis.set("czy", "chenzhengyou");
        System.out.println(jedis.get("czy"));
        jedis.close();
        jedisPool.close();
    }

 /**
     * 整合测试:单机版测试
     */
    @Test
    public void testSpring() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/application-jedis.xml");
        JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
        Jedis jedis = pool.getResource();
        String string = jedis.get("key1");
        System.out.println(string);
        jedis.close();
        pool.close();
    }
}



4.2、application-jedis.xml(与spring整合)

<?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:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:config/jedis.properties" />

    <!-- 连接池配置 -->
    <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="true" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>

    <!-- jedis客户端单机版 -->
    <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg value="${jedis.host}"></constructor-arg>
        <constructor-arg value="${jedis.port}"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>


</beans>


4.3、jedis.properties配置文件

jedis.host=192.168.29.131
#单机端口
jedis.port=6379









  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值