目录
linux搭建redis
1.下载
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
2.解压
tar -zxvf redis-5.0.4.tar.gz
3.编译安装
cd redis-5.0.4
make MALLOC=libc
cd src && make install
4.修改配置脚本
vim redis-5.0.4/reids.conf
- 输入 /requirepass foobared 回车,将#去掉
foodbared
改为自己的密码 - 输入 /bind 127.0.0.1 回车,将其#,或者地址改为0.0.0.0,开放公网访问
- 输入 /port 6379 回车,可以修改端口号
- 输入 /protected-mode yes 回车,改为no,公网可以访问
- 输入 /daemonize no 回车,改为yes,以后台进程启动redis
5.设置开机启动
1.创建启动文件
这个文件名是什么名字,后面启动就用什么名字
vim /etc/init.d/redis
2. 添加文件内容
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -a "password" -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
内容是从 redis5.0.4/utils/redis_init_script cpye的不过要在最开始添加
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
用来设置chkconfig命令
3. 创建相应文件
REDISPORT=6379 # 端口号
EXEC=/usr/local/bin/redis-server # 执行脚本的地址
CLIEXEC=/usr/local/bin/redis-cli # 客户端执行脚本的地址
PIDFILE=/var/run/redis_${REDISPORT}.pid # 进程id文件地址,启动redis后才能看见
CONF="/etc/redis/${REDISPORT}.conf" #配置文件地址
# 如果设置密码这里需要注意 否则无法stop
$REDIS_CLI -a "password" -p $REDISPORT SHUTDOWN # password 改为密码如果没有密码 删除 -a "password"
源码中配置文件保存在/etc/redis/${REDISPORT}.conf,根据端口号创建配置文件即可并将文件复制到相应目录
mkdir /etc/redis
cp redis5.0.4/redis.conf /etc/redis/6379.conf
4. 设置权限
chmod 755 /etc/init.d/redis
5. 启动测试
/etc/init.d/redis start
6. 设置开机启动
chkconfig --add /etc/init.d/redis
chkconfig redis on
失败问题排查
查看第一步的配置文件信息,是否和第二步的文件信息一致,vim粘贴会出现粘贴不全的问题
6. redis的重启
# redis启动
./src/redis-server redis.conf
service redis start
# redis的关闭
# 查看进程号
ps -ef | grep redis
# kill
kill 进程号
service redis stop
Spring中配置redis
1.maven依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2. xml文件
创建spring-redis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--Jedis连接池的相关配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal">
<value>200</value>
</property>
<property name="maxIdle">
<value>50</value>
</property>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6379" type="int" />
<constructor-arg name="timeout" value="30000" type="int" />
<constructor-arg name="password" value="123456" type="java.lang.String" />
<!-- 0代表 Protocol.DEFAULT_DATABASE -->
<constructor-arg name="database" value="0" type="int" />
</bean>
</beans>
导入到spring-mvc.xml中
<import resource="spring-redis.xml" />
3.java代码
在service中使用即可
@Autowired
JedisPool jedisPool;
redis查看工具
链接: RedisDesktopManager.