CentOS6.5安装Redis

本文详细介绍了Redis的本地与Docker环境配置流程,包括解压编译、设置开机启动及Spring框架下的集成方法。涵盖了从环境搭建到Spring Boot项目的具体实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.解压编译并配置

tar -xvf redis-4.0.9.tar.gz
mv redis-4.0.9/ redis
cd redis
make && make install
vim redis.conf

修改以下配置:

#bind 127.0.0.1 # 将这行代码注释,监听所有的ip地址,外网可以访问
protected-mode no # 把yes改成no,允许外网访问
daemonize yes # 把no改成yes,后台运行

2.设置开机启动

输入命令,新建文件

vim /etc/init.d/redis

输入下面内容:

#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis.pid

CONF="/home/leyou/redis/redis.conf"

case "$1" in  
    start)  
        if [ -f $PIDFILE ]  
        then  
                echo "$PIDFILE exists, process is already running or crashed"  
        else  
                echo "Starting Redis server..."  
                $EXEC $CONF  
        fi  
        if [ "$?"="0" ]   
        then  
              echo "Redis is running..."  
        fi  
        ;;  
    stop)  
        if [ ! -f $PIDFILE ]  
        then  
                echo "$PIDFILE does not exist, process is not running"  
        else  
                PID=$(cat $PIDFILE)  
                echo "Stopping ..."  
                $REDIS_CLI -p $REDISPORT SHUTDOWN  
                while [ -x ${PIDFILE} ]  
               do  
                    echo "Waiting for Redis to shutdown ..."  
                    sleep 1  
                done  
                echo "Redis stopped"  
        fi  
        ;;  
   restart|force-reload)  
        ${0} stop  
        ${0} start  
        ;;  
  *)  
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
        exit 1  
esac

注意:以下信息需要根据安装目录进行调整:


EXEC=/usr/local/bin/redis-server # 执行脚本的地址

REDIS_CLI=/usr/local/bin/redis-cli # 客户端执行脚本的地址

PIDFILE=/var/run/redis.pid # 进程id文件地址

CONF="/usr/local/src/redis-3.0.2/redis.conf" #配置文件地址

设置权限

chmod 755 /etc/init.d/redis

设置开机自启动

chkconfig --add /etc/init.d/redis
chkconfig redis on

启动

redis-server redis.conf

在命令行使用

redis-cli

3.Docker环境配置

systemctl docker
docker pull redis
docker run -di --name=xxx_redis -p 6379:6379 redis

4.使用说明

在SpringMVC环境

1.引入依赖

<!-- 缓存 -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.2.RELEASE</version>
</dependency>

其中jedis是java对redis操作的封装,相当于jdbc,spring-data-redis是对jedis的进一步封装,让我们能更方便的写代码,相当于jpa。

2.配置Redis属性

# Redis settings 
# server IP 
redis.host=127.0.0.1
# server port 
redis.port=6379
# server pass 
redis.pass=
# use dbIndex 
redis.database=0
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 
redis.maxIdle=300
# 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;  
redis.maxWait=3000
# 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的  
redis.testOnBorrow=true

3.在Spring配置文件中添加redis配置

<?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:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  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   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
   <!-- redis 属性文件读取 -->
   <context:property-placeholder location="classpath*:properties/*.properties" />   

   <!-- jedis连接池 -->
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
     <property name="maxIdle" value="${redis.maxIdle}" />   
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
     <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
   </bean>
   <!-- SpringDataRedis连接信息 -->
   <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
       p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
   <!-- 配置redisTemplate,操作redis数据库 -->
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
    	<property name="connectionFactory" ref="JedisConnectionFactory" />  
   </bean>  
      
</beans>  
在SpringBoot环境

1.引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

2.修改配置文件

spring.redis.host=192.168.xxx.xxx
常用操作
//向redis里存入数据和设置缓存时间
opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);
//根据key获取缓存中的val
opsForValue().get("test");
//val做-1操作
boundValueOps("test").increment(-1);
//val +1
boundValueOps("test").increment(1);
//根据key获取过期时间
getExpire("test");
//根据key获取过期时间并换算成指定单位
getExpire("test",TimeUnit.SECONDS);
//根据key删除缓存
delete("test");
//检查key是否存在,返回boolean值
hasKey("546545");
//设置过期时间
expire("red_123",1000 , TimeUnit.MILLISECONDS);
//向指定key中存放set集合
opsForSet().add("red_123", "1","2","3");
//根据key查看集合中是否存在指定数据
opsForSet().isMember("red_123", "1");
//根据key获取set集合
opsForSet().members("red_123");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值