redis单机的搭建Centos7

安装包下载

http://download.redis.io/releases/ 下载redis的压缩包,并放在/usr/soft文件夹下

解压压缩包:

tar -zxf redis-3.0.7.tar.gz

安装

 这里安装redis在/usr/local/redis文件夹中

 进入安装包:cd /usr/soft/redis-3.0.7,执行命令

make PREFIX=/usr/local/redis/ install

 安装成功后

redis.conf是redis的配置文件,redis.conf在redis源码目录。注意修改port作为redis进程的端口,port默认6379。

在安装目录中创建目录conf,将redis源安装文件中的redis.cinf拷贝到redis的安装目录中

 cp /usr/soft/redis-3.0.7/redis.conf  /usr/local/redis/bin/

为了关闭窗口后不关闭redis,需要使用后台启动

修改redis.conf的daemonize的no为yes

redis启动

切到该目录下去

使用命令 redis-server redis.conf  启动

检测redis是否运行正常

  使用 ps -ef|grep redis 查看进程

使用redis的客户端查看

     当输入ping命令时,返回PONG就表示连接正常

java代码测试

通过jedis连接redis单机

 需要的依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.0</version>
</dependency>

也就是这两个包

下面是代码的测试  一个要电脑的ip  和  redis的端口

接下来整合spring

添加配置文件applicationContext-jedis.xml

添加配置文件applicationContext-jedis.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 读取jedis配置文件; 这里可以不用配置,-dao已经配置了扫描配置文件 -->
    <!-- <context:property-placeholder location="classpath:/properties/*.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 name="host" value="${JEDIS_HOST}"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>  
    <bean id="redisClientDao" class="com.taotao.rest.dao.impl.JedisClientSingle"></bean> <!--定义自己定义的jedis工具类-->
</beans>

测试

 /**
     * 结合spring的redis单机版测试
     */
    @Test
    public void testSpringSingle(){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
        JedisPool jedisPool = (JedisPool)context.getBean("redisClient");
        Jedis jedis = jedisPool.getResource();
        jedis.set("key1", "1111");
        String str = jedis.get("key1");
        System.out.println("--:"+str);
        jedis.close();
        jedisPool.close();
    }

这里可是自己封装一个工具类

/**
 * 
 */
package demo;

/**
 * @author liuchaojun
 * @date 2018-8-17 下午03:51:29 
 */
import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;



/**
 * 在配置文件中注解
 * 
 * @author Administrator
 *
 */
public class JedisClientSingle implements JedisClient {

    @Autowired
    private JedisPool jedisPool;

     
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String str = jedis.get(key);
        jedis.close();
        return str;
    }

     
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String str = jedis.set(key, value);
        jedis.close();
        return str;
    }

     
    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String str = jedis.hget(hkey, key);
        jedis.close();
        return str;
    }

    
    public Long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    
    public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

     
    public long expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, seconds);
        jedis.close();
        return result;
    }

    
    public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        return result;
    }

    
    public long del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        return result;
    }

    
    public long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(hkey, key);
        return result;
    }

}

测试结果

猜你喜欢

转载自blog.csdn.net/qq_27026603/article/details/81772075