Dubbo高级篇_03_Redis的安装与使用

IP:192.168.1.51
环境:CentOS6.6
Redis版本:redis-3.0
安装目录:/usr/local/redis
用户:root

Connecting to 192.168.1.51:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.


Last login: Sun Apr  3 17:22:46 2016 from 192.168.1.2
[root@yxq ~]# 
编译和安装所需的包:
[root@yxq ~]# yum install gcc tcl
[root@yxq ~]# cd /usr/local/src
下载3.0版Redis
[root@yxq src]# wget http://download.redis.io/releases/redis-3.0.0.tar.gz
[root@yxq src]# ls
redis-3.0.0.tar.gz
创建安装目录
[root@yxq src]# mkdir /usr/local/redis


解压
[root@yxq src]# tar -zxvf redis-3.0.0.tar.gz
[root@yxq src]# ls
redis-3.0.0  redis-3.0.0.tar.g
[root@yxq src]# cd redis-3.0.0


安装(使用PREFIX指定安装目录):
[root@yxq redis-3.0.0]# make PREFIX=/usr/local/redis install
安装完成后,可以看到/usr/local/redis 目录下有一个 bin 目录,bin 目录里就是 redis 的命令脚本: 
[root@yxq redis-3.0.0]# ls /usr/local/redis/bin
redis-benchmark  redis-check-dump  redis-sentinel
redis-check-aof  redis-cli         redis-server
[root@yxq redis-3.0.0]# 
将 Redis 配置成服务:
Redis 的启动脚本为:/usr/local/src/redis3.0/utils/redis_init_script
将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为 redis
[root@yxq redis-3.0.0]# cp /usr/local/src/redis-3.0.0/utils/redis_init_script /etc/rc.d/init.d/redis
编辑/etc/rc.d/init.d/redis,修改相应配置,使之能注册成为服务:
[root@yxq redis-3.0.0]# 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 配置文件目录
[root@yxq redis-3.0.0]# mkdir /usr/local/redis/conf
复制 redis 配置文件/usr/local/src/redis3.0/redis.conf 到/usr/local/redis/conf 目录并按端口号重命名(方便集群,集群的话有主有备)为 6379.conf
[root@yxq redis-3.0.0]# cp /usr/local/src/redis-3.0.0/redis.conf /usr/local/redis/conf/6379.conf
[root@yxq redis-3.0.0]# cd /usr/local/redis/conf
[root@yxq conf]# ls
6379.conf
做了以上准备后,再对 CONF 属性作如下调整:
CONF="/etc/redis/${REDISPORT}.conf" 改为 CONF="/usr/local/redis/conf/${REDISPORT}.conf"
[root@yxq conf]# vi /etc/rc.d/init.d/redis
[root@yxq conf]# cat /etc/rc.d/init.d/redis | grep conf
#chkconfig: 2345 80 90
CONF="/usr/local/redis/conf/${REDISPORT}.conf"
(6)更改 redis 开启的命令,以后台运行的方式执行: $EXEC $CONF & #“&”作用是将服务转到后面运行
[root@yxq conf]# cat /etc/rc.d/init.d/redis | grep '$EXEC'
                $EXEC $CONF &
以上配置操作完成后,便可将 Redis 注册成为服务:
[root@yxq conf]# chkconfig --add redis
防火墙中打开对应的端口
[root@yxq conf]# vi /etc/sysconfig/iptables
[root@yxq conf]# cat /etc/sysconfig/iptables | grep 6379
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
[root@yxq conf]# service iptables restart
修改 redis 配置文件设置:
将daemonize no 改为> daemonize yes,改为yes作为后台进程使用,
pidfile /var/run/redis.pid 改为> pidfile /var/run/redis_6379.pid
设置为no,pid文件是不会生成,stop等命令就不会生效,/etc/rc.d/init.d
会报如下错误:
/var/run/redis_6379.pid does not exist, process is not running
[root@yxq conf]# vi /usr/local/redis/conf/6379.conf
[root@yxq conf]# cat /usr/local/redis/conf/6379.conf | grep daemonize
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
[root@yxq conf]# cat /usr/local/redis/conf/6379.conf | grep pidfile
pidfile /var/run/redis_6379.pid
启动 Redis 服务
[root@yxq conf]# service redis start
Starting Redis server...
[root@yxq conf]# ps -ef | grep redis
root      10201      1  0 18:10 ?        00:00:00 /usr/local/redis/bin/redis-server *:6379                         
root      10214   7095  0 18:11 pts/0    00:00:00 vi /etc/rc.d/init.d/redis
root      10258   7095  0 18:16 pts/0    00:00:00 grep redis
[root@yxq conf]# 
将 Redis 添加到环境变量中:
[root@yxq conf]# vi /etc/profile
在最后添加以下内容:
## Redis env
export PATH=$PATH:/usr/local/redis/bin

使其配置生效
[root@yxq conf]# source /etc/profile
现在就可以直接使用 redis-cli 等 redis 命令了:
[root@yxq conf]# redis-cli
127.0.0.1:6379> set yixq yixiaoqun
OK
127.0.0.1:6379> get yixq
"yixiaoqun"
127.0.0.1:6379> 
停止服务
[root@yxq conf]# service redis stop
Stopping ...
Redis stopped


默认情况下,Redis开启安全认证,可以通过/usr/local/redis/conf/6379.conf的requirepass指定一个验证密码
[root@yxq conf]# vi /usr/local/redis/conf/6379.conf 
[root@yxq conf]# cat /usr/local/redis/conf/6379.conf | grep requirepass
# If the master is password protected (using the "requirepass" configuration

 requirepass yxq123456

使用RedisDesktopManager登录管理

不输入验证密码

Dubbo高级篇_03_Redis的安装与使用_第1张图片

输入验证密码,测试连接

Dubbo高级篇_03_Redis的安装与使用_第2张图片

Dubbo高级篇_03_Redis的安装与使用_第3张图片

客户端使用console

Dubbo高级篇_03_Redis的安装与使用_第4张图片

Jedis的使用

Dubbo高级篇_03_Redis的安装与使用_第5张图片

public class RedisTest {
	private static final Log log = LogFactory.getLog(RedisTest.class);

	public static void main(String[] args) {
		
		Jedis jedis = new Jedis("192.168.1.51");
		jedis.auth("yxq123456");
		String key = "redis";
		String value = "";
		
		jedis.del(key); // 删数据
		
		jedis.set(key, "YiXiaoqun"); // 存数据
		value = jedis.get(key); // 取数据
		log.info(key + "=" + value);
		
		jedis.set(key, "YiXiaoqun2"); // 存数据
		value = jedis.get(key); // 取数据
		log.info(key + "=" + value);
		
		//jedis.del(key); // 删数据
		//value = jedis.get(key); // 取数据
		//log.info(key + "=" + value);
	}
}

Jedis连接池的使用,这里设置redis不使用密码验证

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"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Jedis链接池配置 -->
	
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="testWhileIdle" value="true" />
		<property name="minEvictableIdleTimeMillis" value="60000" />
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<property name="numTestsPerEvictionRun" value="-1" />
		<property name="maxTotal" value="8" />
		<property name="maxIdle" value="8" />
		<property name="minIdle" value="0" />
	</bean>

	<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
		<constructor-arg index="0" ref="jedisPoolConfig" />
		<constructor-arg index="1">
			<list>
				<bean class="redis.clients.jedis.JedisShardInfo">
					<constructor-arg index="0" value="192.168.1.51" />
					<constructor-arg index="1" value="6379" type="int" />
				</bean>
			</list>
		</constructor-arg>
	</bean>
</beans>
spring-context.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
	default-autowire="byName" default-lazy-init="false">
	
	<!-- 基于Dubbo的分布式系统架构视频教程,吴水成,[email protected],学习交流QQ群:367211134 -->

	<!-- 采用注释的方式配置bean -->
	<context:annotation-config />

	<!-- 配置要扫描的包 -->
	<context:component-scan base-package="redis.edu.demo" />

	<!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />	
	
	<import resource="spring-redis.xml" />
</beans>
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>	
	<groupId>redis.edu.demo</groupId>
	<artifactId>edu-demo-redis</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>edu-demo-redis</name>
	<url>http://maven.apache.org</url>
	<build>
		<finalName>edu-demo-redis</finalName>
		<resources>
			<resource>
				<targetPath>${project.build.directory}/classes</targetPath>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
		</resources>
	</build>
	<dependencies>

		<!-- Common Dependency Begin -->
		<dependency>
			<groupId>antlr</groupId>
			<artifactId>antlr</artifactId>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<classifier>jdk15</classifier>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
		</dependency>
		<dependency>
			<groupId>oro</groupId>
			<artifactId>oro</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-validator</groupId>
			<artifactId>commons-validator</artifactId>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
		</dependency>

		<!-- Common Dependency End -->

		<!-- Spring Dependency Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
		</dependency>

		<!-- Spring Dependency End -->

		<!-- Redis client -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.6.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.3</version>
		</dependency>
	</dependencies>
</project>
RedisSpringTest.java

public class RedisSpringTest {
	private static final Log log = LogFactory.getLog(RedisSpringTest.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();
			
			ShardedJedisPool pool = (ShardedJedisPool) context.getBean("shardedJedisPool");
		
			ShardedJedis jedis = pool.getResource();
			String key = "redis";
			String value = "";
			
			jedis.del(key); // 删数据
			
			jedis.set(key, "WuShuicheng"); // 存数据
			value = jedis.get(key); // 取数据
			log.info(key + "=" + value);
			
			jedis.set(key, "WuShuicheng2"); // 存数据
			value = jedis.get(key); // 取数据
			log.info(key + "=" + value);
			
			jedis.del(key); // 删数据
			value = jedis.get(key); // 取数据
			log.info(key + "=" + value);

			context.stop();
		} catch (Exception e) {
			log.error("==>RedisSpringTest context start error:", e);
			System.exit(0);
		} finally {
			log.info("===>System.exit");
			System.exit(0);
		}
	}
}






你可能感兴趣的:(redis)