redis 安装
0、安装redis安装环境
yum -y install gcc
yum -y install pcre-devel
yum -y install zlib-devel
yum -y install openssl
yum -y install openssl-devel
1、安装redis
上传文件
rz
解压
tar –zxvf redis-5.0.3.tar.gz
编译redis
进入redis安装目录,执行命令
make
然后执行
make test
然后执行
make install
2、配置redis
统一管理
创建bin和etc文件夹
mkdir /usr/local/redis/bin
mkdir /usr/local/redis/etc
执行Linux文件移动命令
mv /usr/local/redis/redis.conf /usr/local/redis/etc
cd /usr/local/redis/src
mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server /usr/local/redis/bin
3、执行Redis-server 命令,启动Redis 服务
进入刚刚创建的bin目录执行命令
./redis-server
Ctrl+c 退出…
4、后台启动redis服务
首先编辑conf文件,将daemonize属性改为yes(表明需要在后台运行)
Vim etc/redis.conf
修改redis.conf配置文件:
## 为了redis客户端远程能够访问
1.将bind 127.0.0.1改为**#bind 127.0.0.1**
2.将protected-mode yes改为protected-mode no,
## 指定日志文件目录 ,暂时可以不设置
## logfile “/usr/local/redis/server-out.log”
## 默认启动时为后台启动 原来是no,
daemonize yes
再次启动redis服务,并指定启动服务配置文件
redis-server /usr/local/redis/etc/redis.conf
5、设置Redis客户端登录密码(可以略过,否则以后每次操作都要密码)
修改配置
打开配置文件.在/redis/etc下的redis.conf
vim redis.conf
找到#requirepass foobared
去掉行前的注释,并修改密码为所需的密码,保存文件
requirepass 123456
重启服务:
以下命令需要到/redis/bin目录下执行
关闭Redis
redis-cli shutdown
关闭服务端
pkill redis-server
开启服务(指定配置文件的形式)
redis-server /usr/local/redis/etc/redis.conf
进入客户端(1) 不使用密码
redis-cli
可以进入,但是不能执行命令
进入客户端使用密码
redis-cli –a 123456
可以进入,并且可以执行命令
6 、注册Redis服务,开机自启
linux环境下创建redis服务,开启或关闭redis服务端
步骤一:文件操作
1.在/etc目录下创建redis目录:mkdir /etc/redis
2.将redis.conf文件复制到/etc/redis目录下:cp redis.conf /etc/redis
3.重命名/etc/redis目录下redis.conf为6379.conf:mv redis.conf 6379.conf
4.将utils/redis_init_script文件复制到/etc/init.d目录下:cp redis_init_script /etc/init.d
5.修改/etc/init.d/redis_init_script文件为redis:mv /etc/init.d/redis_init_script /etc/init.d/redis
6.编辑/etc/init.d/redis文件,在第二行添加:# chkconfig: 2345 80 90
7.编辑/etc/init.d/redis文件,修改EXEC和CLIEXEC对应的执行脚本的真实目录
步骤二:注册服务
1.chkconfig --add redis
步骤三:测试
1.启动redis:service redis start
2.关闭reids:service redis stop
关闭时出现
是因为 设置了密码
解决如下:
修改redis服务脚本,加入如下所示的红色授权信息即可:
vi /etc/init.d/redis
$CLIEXEC –a "password" -p $REDISPORT shutdown
7 java操作redis
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
1. 单实例连接
@Test
public void testJedis() {
//创建一个Jedis的连接
Jedis jedis = new Jedis("127.0.0.1", 6379);
//执行redis命令
jedis.set("mytest", "hello world, this is jedis client!");
//从redis中取值
String result = jedis.get("mytest");
//打印结果
System.out.println(result);
//关闭连接
jedis.close();
}
2. 连接池连接
@Test
public void testJedisPool() {
//创建一连接池对象
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
//从连接池中获得连接
Jedis jedis = jedisPool.getResource();
String result = jedis.get("mytest");
System.out.println(result);
//关闭连接
jedis.close();
//关闭连接池
jedisPool.close();
}
3. Spring整合jedisPool
添加spring的jar包
配置spring配置文件applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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 ">
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="false" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis单机 通过连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"
destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="192.168.87.128" />
<constructor-arg name="port" value="6379" />
</bean>
</beans>
@Autowired
private JedisPool jedisPool;
@RequestMapping("/testRedis1.do")
public String testRedis1() {
System.out.println("进入testRedis1");
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set("age", "18");
System.out.println("获得Redis值 : "+jedis.get("age"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
return null;
}
4. 使用SpringDataRedis
导入pom依赖
<!--redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
<!--SpringDataRedis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
spring:
redis:
host: 192.168.84.138
port: 6379
@Autowired // 由Spring进行注入,可以直接使用
private RedisTemplate redisTemplate;
8. 搭建集群
- 在/usr/local下创建一个文件夹redis-cluster
cd /usr/local
mkdir redis-cluster
-
将redis目录下的bin 复制一份到redis-cluster下改名为Redis
cp -R /usr/local/redis/bin /usr/local/redis-cluster
mv /usr/local/redis-cluster/bin /usr/local/redis-cluster/redis
-
将redis.conf复制到redis-cluster下的redis中
cp /usr/local/redis/conf/redis.conf /usr/local/redis-cluster/redis
-
修改redis.conf 文件
开启 cluster-node-timeout 15000
修改为 cluster-enabled yes
修改pidfile /var/run/redis_6379.pid同一个虚拟机中pidfile不要重复
port 6379同一虚拟机中port不要重复
-
开启集群
开启所有redis服务
–cluster-replicas * 是1主*从
./redis-cli --cluster create 192.168.199.177:7000 192.168.199.177:7001 192.168.199.177:7002 192.168.199.132:7003 192.168.199.132:7004 192.168.199.132:7005 --cluster-replicas 1