分布式架构学习之:Redis的linux安装与使用(单节点)

Redis 的安装与使用(单节点) 

IP:192.168.4.111

环境:CentOS 6.6

Redis 版本:redis-3.0 (考虑到 Redis3.0 在集群和性能提升方面的特性,rc 版为正式版的候选版,而且很快就出正式版)

安装目录:/usr/local/redis

用户:root

编译和安装所需的包

yum install gcc tcl

下载 3.0  Redis(当前最新版 redis-3.0.0-rc5.tar.gz,请学员们在安装时自行选用最新版

cd /usr/local/src

wget https://github.com/antirez/redis/archive/3.0.0-rc5.tar.gz

创建安装目录:

mkdir /usr/local/redis

解压:

tar -zxvf 3.0.0-rc5.tar.gz

mv redis-3.0.0-rc5 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-dump redis-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=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 -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

查看以上 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=6379

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid

CONF="/usr/local/redis/conf/${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 -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

以上配置操作完成后,便可将 Redis 注册成为服务:

chkconfig --add redis

防火墙中打开对应的端口

vi /etc/sysconfig/iptables

添加:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

重启防火墙:

service iptables restart

修改 redis 配置文件设置:

vi /usr/local/redis/conf/6379.conf

修改如下配置

daemonize no 改为>daemonize yes

pidfile /var/run/redis.pid 改为>pidfile /var/run/redis_6379.pid

启动 Redis 服务

service redis start

 Redis 添加到环境变量中:

vi /etc/profile

在最后添加以下内容:

## Redis env

export PATH=$PATH:/usr/local/redis/bin

使配置生效:

source /etc/profile

现在就可以直接使用redis-cli redis 命令了:

关闭 Redis 服务

service redis stop

默认情况下,Redis 开启安全认证,可以通过/usr/local/redis/conf/6379.conf requirepass 指定一个验证密码。

 

java代码测试

[java]  view plain  copy
  1. public class RedisTest {  
  2.     private static final Log log = LogFactory.getLog(RedisTest.class);  
  3.   
  4.     public static void main(String[] args) {  
  5.           
  6.         Jedis jedis = new Jedis("192.168.4.111");  
  7.           
  8.         String key = "wusc";  
  9.         String value = "";  
  10.           
  11.         jedis.del(key); // 删数据  
  12.           
  13.         jedis.set(key, "WuShuicheng"); // 存数据  
  14.         value = jedis.get(key); // 取数据  
  15.         log.info(key + "=" + value);  
  16.           
  17.         jedis.set(key, "WuShuicheng2"); // 存数据  
  18.         value = jedis.get(key); // 取数据  
  19.         log.info(key + "=" + value);  
  20.           
  21.         //jedis.del(key); // 删数据  
  22.         //value = jedis.get(key); // 取数据  
  23.         //log.info(key + "=" + value);  
  24.     }  
  25. }  

spring整合


spring-redis.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.   
  7.     <!-- Jedis链接池配置 -->  
  8.       
  9.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  10.         <property name="testWhileIdle" value="true" />  
  11.         <property name="minEvictableIdleTimeMillis" value="60000" />  
  12.         <property name="timeBetweenEvictionRunsMillis" value="30000" />  
  13.         <property name="numTestsPerEvictionRun" value="-1" />  
  14.         <property name="maxTotal" value="8" />  
  15.         <property name="maxIdle" value="8" />  
  16.         <property name="minIdle" value="0" />  
  17.     </bean>  
  18.   
  19.     <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">  
  20.         <constructor-arg index="0" ref="jedisPoolConfig" />  
  21.         <constructor-arg index="1">  
  22.             <list>  
  23.                 <bean class="redis.clients.jedis.JedisShardInfo">  
  24.                     <constructor-arg index="0" value="192.168.4.111" />  
  25.                     <constructor-arg index="1" value="6379" type="int" />  
  26.                 </bean>  
  27.             </list>  
  28.         </constructor-arg>  
  29.     </bean>  
  30. </beans>  

测试代码
[java]  view plain  copy
  1. public class RedisSpringTest {  
  2.     private static final Log log = LogFactory.getLog(RedisSpringTest.class);  
  3.   
  4.     public static void main(String[] args) {  
  5.         try {  
  6.             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");  
  7.             context.start();  
  8.               
  9.             ShardedJedisPool pool = (ShardedJedisPool) context.getBean("shardedJedisPool");  
  10.             ShardedJedis jedis = pool.getResource();  
  11.               
  12.             String key = "wusc";  
  13.             String value = "";  
  14.               
  15.             jedis.del(key); // 删数据  
  16.               
  17.             jedis.set(key, "WuShuicheng"); // 存数据  
  18.             value = jedis.get(key); // 取数据  
  19.             log.info(key + "=" + value);  
  20.               
  21.             jedis.set(key, "WuShuicheng2"); // 存数据  
  22.             value = jedis.get(key); // 取数据  
  23.             log.info(key + "=" + value);  
  24.               
  25.             jedis.del(key); // 删数据  
  26.             value = jedis.get(key); // 取数据  
  27.             log.info(key + "=" + value);  
  28.   
  29.             context.stop();  
  30.         } catch (Exception e) {  
  31.             log.error("==>RedisSpringTest context start error:", e);  
  32.             System.exit(0);  
  33.         } finally {  
  34.             log.info("===>System.exit");  
  35.             System.exit(0);  
  36.         }  
  37.     }  
  38. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值