JEESZ-Redis分布式缓存安装和使用
独立缓存服务器: LinuxCentOSRedis版本: 3.0下面我们针对于Redis安装做下详细的记录:编译和安装所需的包:#yum install gcc tcl创建安装目录:#mkdir /usr/local/redis解压:#tar -zxvf 3.xx.tar.gz#mv redis-3.xx redis3.0#cd redis3.0安装(使用PREFIX指定安装目录):#make PREFIX=/usr/local/redis install安装完成后,可以看到/usr/local/redis目录下有一个bin目录,bin目录里就是redis的命令脚本:redis-benchmark redis-check-aof redis-check-dumpredis-cli redis-server将Redis配置成服务:按上面的操作步骤,Redis的启动脚本为:/usr/local/src/redis3.0/utils/redis_init_script将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为redis#cp /usr/local/src/redis3.0/utils/redis_init_script/etc/rc.d/init.d/redis编辑/etc/rc.d/init.d/redis,修改相应配置,使之能注册成为服务:#vi/etc/rc.d/init.d/redis#!/bin/sh## Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.REDISPORT=6379EXEC=/usr/local/bin/redis-serverCLIEXEC=/usr/local/bin/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pidCONF="/etc/redis/${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;esac查看以上redis服务脚本,关注标为橙色的几个属性,做如下几个修改的准备:(1)在脚本的第一行后面添加一行内容如下:#chkconfig: 2345 80 90提醒:如果不添加上面的内容,在注册服务时会提示:service redis does not support chkconfig(2)REDISPORT端口保持6379不变;(特别注意:端口名将与下面的配置文件名有关)(3)EXEC=/usr/local/bin/redis-server改为EXEC=/usr/local/redis/bin/redis-server(4)CLIEXEC=/usr/local/bin/redis-cli改为CLIEXEC=/usr/local/redis/bin/redis-cli(5)配置文件设置:创建redis配置文件目录#mkdir /usr/local/redis/conf复制redis配置文件/usr/local/src/redis3.0/redis.conf到/usr/local/redis/conf目录并按端口号重命名为6379.conf#cp /usr/local/src/redis3.0/redis.conf/usr/local/redis/conf/6379.conf做了以上准备后,再对CONF属性作如下调整:CONF="/etc/redis/${REDISPORT}.conf"改为CONF="/usr/local/redis/conf/${REDISPORT}.conf"(6)更改redis开启的命令,以后台运行的方式执行:$EXEC $CONF&#“&”作用是将服务转到后面运行修改后的/etc/rc.d/init.d/redis服务脚本内容为:#!/bin/sh#chkconfig: 2345 80 90## Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.REDISPORT=6379EXEC=/usr/local/redis/bin/redis-serverCLIEXEC=/usr/local/redis/bin/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pidCONF="/usr/local/redis/conf/${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONF&fi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;esac以上配置操作完成后,便可将Redis注册成为服务:#chkconfig --add redis防火墙中打开对应的端口#vi /etc/sysconfig/iptables添加:-A INPUT -m state --state NEW -m tcp -p tcp --dport6379 -j ACCEPT重启防火墙:#service iptables restart修改redis配置文件设置:#vi /usr/local/redis/conf/6379.conf修改如下配置daemonizeno改为daemonizeyes备注:如果不改为yes,pid文件是不会生成,start、stop命令是不会生效的(依赖pid文件)pidfile /var/run/redis.pid改为pidfile /var/run/redis_6379.pid启动Redis服务#service redis start将Redis添加到环境变量中:#vi /etc/profile在最后添加以下内容:## Redis envexport PATH=$PATH:/usr/local/redis/bin使配置生效:#source /etc/profile当前可以直接使用redis-cli等redis命令了:#redis-clihttp://upload-images.jianshu.io/upload_images/5712789-1b803836c3f4b1e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240
关闭Redis服务#service redis stop提醒:默认情况下,Redis开启安全认证,可以通过/usr/local/redis/conf/6379.conf的requirepass指定一个验证密码在jeesz中主要使用redis做分布式缓存,存储登录用户信息,做session共享管理
http://upload-images.jianshu.io/upload_images/5712789-78937ebc0c7cc29c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240
源码来源:http://minglisoft.cn/technology欢迎大家一起学习研究相关技术,源码获取请加求求(企鹅): 2042849237
页:
[1]