1. Redis简介
Redis是使用c语言开发nosql系列的数据库,存储结构:key-value
2. Redis环境准备
Redis使用c语言开发的程序,需要使用gcc编译程序编译redis。
安装gcc编译程序命令:
1yum -y install gcc
安装上传文件插件:
1yum -y install lrzsz
3. 安装redis
上传redis安装包
命令:使用rz插件
Atl+P上传redis安装包
外部上传工具
解压redis安装包
命令:
1tar -zxvf redis-3.0.7.tar.gz
2//–z:解压使用gzip压缩的文件
3//–x:解压tar包文件
4//–v:显示解压过程
5//–f:指定解压文件
3. 编译redis
命令:make
需要进入redis目录:进行编译
4. 安装redis
编译命令:
1make install PREFIX=/usr/local/redis
2//要求:必须进入redis目录
示例:
1[root@zk01 redis-3.0.7]# make install PREFIX=/usr/local/redis
2cd src && make install
3make[1]: Entering directory `/usr/local/redis-3.0.7/src'
4
5Hint: It's a good idea to run 'make test' ;)
6
7 INSTALL install
8 INSTALL install
9 INSTALL install
10 INSTALL install
11 INSTALL install
12make[1]: Leaving directory `/usr/local/redis-3.0.7/src'
13
5. 登录redis(前台启动)
启动命令:
1./redis-server
6. 后台启动(redis作为一个进程在后台运行)
拷贝redis.conf配置文件到安装目录/usr/local/redis/
1[root@zk01 redis-3.0.7]# ll
2total 152
3-rw-rw-r--. 1 root root 36761 Jan 25 2016 00-RELEASENOTES
4-rw-rw-r--. 1 root root 53 Jan 25 2016 BUGS
5-rw-rw-r--. 1 root root 1805 Jan 25 2016 CONTRIBUTING
6-rw-rw-r--. 1 root root 1487 Jan 25 2016 COPYING
7drwxrwxr-x. 6 root root 4096 Aug 25 17:11 deps
8-rw-rw-r--. 1 root root 11 Jan 25 2016 INSTALL
9-rw-rw-r--. 1 root root 151 Jan 25 2016 Makefile
10-rw-rw-r--. 1 root root 4223 Jan 25 2016 MANIFESTO
11-rw-rw-r--. 1 root root 5201 Jan 25 2016 README
12-rw-rw-r--. 1 root root 41560 Jan 25 2016 redis.conf
13-rwxrwxr-x. 1 root root 271 Jan 25 2016 runtest
14-rwxrwxr-x. 1 root root 280 Jan 25 2016 runtest-cluster
15-rwxrwxr-x. 1 root root 281 Jan 25 2016 runtest-sentinel
16-rw-rw-r--. 1 root root 7109 Jan 25 2016 sentinel.conf
17drwxrwxr-x. 2 root root 4096 Aug 25 17:13 src
18drwxrwxr-x. 10 root root 4096 Jan 25 2016 tests
19drwxrwxr-x. 5 root root 4096 Jan 25 2016 utils
20[root@zk01 redis-3.0.7]# cp redis.conf ../redis/bin/
修改redis.conf配置文件后台启动模式
启动命令:
1//注意:启动redis需要加载redis.conf配置文件
2./redis-server redis.conf
7. Redis数据结构
redis是一种高级的key:value存储系统,其中value支持五种数据类型:
字符串(strings)
字符串列表(lists)
字符串集合(sets)
有序字符串集合(sorted sets)
哈希(hashes)
而关于key,有几个点要提醒大家:
key不要太长,尽量不要超过1024字节,这不仅消耗内存,而且会降低查找的效率;
key也不要太短,太短的话,key的可读性会降低;
在一个项目中,key最好使用统一的命名模式,例如user:10000:passwd。
8. Redis登录
登录命令:
1./redis-cli –h ip –p port
登录本地默认端口:
1./redis-cli (默认登录6379端口redis服务)
9.Strings
Redis存储数据是string类型,redis数据结构:key:value,value是strings类型
命令操作:
set name zhangsan
给strings类型数据结构key:name 设置一个zhangsan的值get name
获取name属性中值incr age
给指定key加上1(把字符类型数字转换成integer类型,再加上1)decr age
给age减去1(把字符类型数字转换成integer类型,再减去1)incrby age 10
给age指定需要加上数字,加上10
把字符类型数字转换成integer类型,再加上10decrby age 10
给age指定需要减去10
10. Hashes
Hash存储结构:key value(hash)
Redis存储hash类型:key filed value
第一种存储方式:
使用一个key存储一个对象,获取对象必须进行序列化和反序列化。反序列化占用IO资源
第二种方式:
方式二满足redis存储结构,redis基本类型回归到strings类型,用户ID有重复。
第三种方式:
Redis存储结构:
1user username zhangsanfeng
2user age 22
命令:
1./redis-cli –raw //查询中文登录命令
hset user username zhangsanfeng
给User对象中Username属性设置一个值hget user username
获取User对象中Username属性值hdel user username age
删除User中Username,age属性hsetnx user age 33
如果age属性存在,不进行覆盖
如果age不存在,创建一个新的属性,设置值hmset user email 11@qq.com address 西三旗
同时设置多个属性值hmget user username address
同时获取多个属性值
11. Lists
Redis lists数据结构是链表.
单向链表:
单向链表只能从一个方法进行插入元素,只能从一个方向进行查询元素。
双向链表:
Redis list数据 结构使用双向链表:
命令:
lpush mylist a b c d
给list集合mylist设置4个值a b c dlrange mylist 0 -1
获取mylist集合中所有元素值
0:链表起始位置
-1:链表结束位置lrem mylist 3 a
删除集合mylist中前3个a元素lset mylist 1 s
Arr[1]=a ;arr[1]=b
给mylist集合1位置s值linsert mylist after b w
给集合mylist中b元素后面插入元素w
12.Set
命令:
sadd myset a b c d
给set集合myset添加4个元素smembers myset
获取集合,myset中所有值srem myset a b
删除集合myset中元素
13.Sorted set
命令:
zadd myzset 2 one 6 two 9 three 10 four 19 five 20 six
给集合myzset设置6个元素(带得分元素)zcount myzset 2 20
统计分数在2到20之间元素的个数,分数查询区间(闭区间)
1zcount myzset (2 20 [设置左边开区间]
2zcount myzset -inf +inf
3//统计所有分数元素个数
4//–inf:代表最小值
5//+inf:最大值
6
zrange myzset 0 -1 withscores
根据角标查询集合(查询集合元素包含分数)zrangebyscore myzset 2 20 withscores limit 2 2
根据分数查询myzset集合,分页查询
Limit 2,2
第一个2:查询的起始角标
第二个2:每页显示2条zrem myzset one two three
删除集合myzset中元素
14.Keys
keys *
匹配查询redis数据库有多少keyDEL key [key …]
expire name 22
设置过期时间ttl name
查询过期剩余时间
15.多数据库实例
Redis数据库实例:
选择1号数据库:
1myset
2127.0.0.1:6379> select 1
3OK
4127.0.0.1:6379[1]>
5//需求:把0号数据库中key移动到1号数据库
6move user 1 //把当前库中User移动到1号数据库
16. redis事务
redis事务
命令:
1Multi:开启事务
2Exec:提交事务
3Discard:事务回滚
4Watch:监控事务(乐观锁)
例如:
A,B同时购买一个商品,此商品的数量只有一个(itemNum=1)
监控itemNum是否发生变化,另一用户将会放弃事务。
1Multi exec
2127.0.0.1:6379> get itemNum
31
4127.0.0.1:6379> multi //开启事务
5OK
6127.0.0.1:6379> incr itemNum
7QUEUED
8127.0.0.1:6379> incrby itemNum 6
9QUEUED
10127.0.0.1:6379> lpop itemNum //模拟错误,使用操作list集合命令操作strings
11QUEUED
12127.0.0.1:6379> decr itemNum
13QUEUED
14127.0.0.1:6379> decrby itemNum 6
15QUEUED
16127.0.0.1:6379> exec //事务提交
172
188
19WRONGTYPE Operation against a key holding the wrong kind of value
20
217
221
Redis事务不遵循ACID大一统理论,即使执行失败,后面命令一样执行。
事务回滚
1127.0.0.1:6379> multi //开启事务
2OK
3127.0.0.1:6379> incr itemNum
4QUEUED
5127.0.0.1:6379> incrby itemNum 8
6QUEUED
7127.0.0.1:6379> discard //事务回滚
8OK
9127.0.0.1:6379> get itemNum
101
乐观锁
1Redis事务乐观锁使用watch:
21
3127.0.0.1:6379> watch itemNum //监控itemNum商品数量
4OK
5127.0.0.1:6379> multi //开启事务
6OK
7127.0.0.1:6379> decr itemNum
8QUEUED
9127.0.0.1:6379> exec //提交事务
10
11127.0.0.1:6379> get itemNum
120
A,B两个用户同时购买一个商品,商品数量itemNum=1,只能有一个用户购买成功
思考:控制事务?
使用乐观锁:
A和B同时监控事务
某一时刻:B购买商品并且提交事务
A用户也购买此商品,监控商品数量变化,一旦发现此商品数量发生变化,事务回滚。
17. Redis持久化
Redis默认持久化是rdb持久化。
Rdb持久化
优点
持久化文件只包含一个文件
容错备份,灾难回复速度快。
Redis fork出一个子进程持久化
缺点
数据容错比较差
持久化策略
缺省情况下,Redis会将数据集的快照dump到dump.rdb文件中。此外,我们也可以通过配置文件来修改Redis服务器dump快照的频率,在打开redis.conf文件之后,我们搜索save,可以看到下面的配置信息:
1save 900 1 #在900秒(15分钟)之后,如果至少有1个key发生变化,则dump内存快照。
2save 300 10 #在300秒(5分钟)之后,如果至少有10个key发生变化,则dump内存快照。
3save 60 10000 #在60秒(1分钟)之后,如果至少有10000个key发生变化,则dump内存快照。
Aof持久化
需要开启redis的aof持久化。
优点
1)数据安全性高
2)redis-check-aof工具来帮助我们解决数据一致性的问题
缺点
1)aof持久化策略比rdb持久化策略慢
持久化策略
开启持久化
修改aof持久化同步策略
Rdb和aof
Rdb和aof同时存在,redis优先使用aof持久化策略。
18. Redis主从复制
Redis主从复制:(服务器至少2台redis服务)[模拟2台redis服务]
备份
容错
提供并发量(读写分离)
模拟2台redis服务:
创建2个目录:6379,6380(要求目录中redis服务的端口和目录相同)
拷贝redis安装目录bin到6379目录,6380目录
1[root@zk01 hadoop]# cp -r redis/bin redisMR/6379/
2[root@zk01 hadoop]# cp -r redis/bin redisMR/6380/
修改6380服务端口。
临时性主从复制
临时性的主从复制:使用命令设置临时性主从复制,服务器一旦从新启动,主从复制消失。需求:设置6379为主机,6380为6379从机。
1[root@zk01 bin]# ./redis-cli -h 192.168.66.66 -p 6380
2192.168.66.66:6380> slaveof 192.168.66.66 6379
3OK
4192.168.66.66:6380> keys *
51) "username"
6192.168.66.66:6380> get username
7"ssa"
8192.168.66.66:6380> get username
9"lili"
10192.168.66.66:6380> set age 12
11(error) READONLY You can't write against a read only slave.
12192.168.66.66:6380> get username
13"\xe5\xa5\xa5\xe5\xb7\xb4\xe5\xb7\xb4"
14192.168.66.66:6380> get username
15"zhangwuji"
16
永久性主从复制
要想主从复制永久生效,需要配置redis配置文件:Redis.conf中设置6380为6379的从机。
18. Jedis
Jedis简介
Jedis是用来操作redis服务器客户端程序。
创建java工程
导入jar
连接单机版
1/**
2 * 需求:连接单机版redis服务
3 */
4 @Test
5 public void linkOneRedis(){
6 //创建Jedis核心对象,连接redis服务
7 Jedis jedis = new Jedis("192.168.66.66", 6379);
8 //操作redis服务器
9 jedis.set("username", "奥巴马喜欢凤姐");
10 //获取redis服务值
11 String username = jedis.get("username");
12 System.out.println(username);
13}
连接池连接redis
1/**
2 * 需求:使用连接池连接redis服务
3 */
4@Test
5public void linkPoolRedis(){
6 //创建连接池配置对象,配置连接池
7 JedisPoolConfig poolConfig = new JedisPoolConfig();
8 //连接最大空闲数为20
9 poolConfig.setMaxIdle(20);
10 //设置最大连接数
11 poolConfig.setMaxTotal(1000);
12
13 //创建连接池核心对象,连接redis服务
14 JedisPool jPool = new JedisPool(poolConfig, "192.168.66.66", 6379);
15 //从连接池中获取Jedis对象,操作redis服务
16 Jedis jedis = jPool.getResource();
17 //操作redis服务器
18 jedis.set("username", "希拉里");
19 //获取redis服务值
20 String username = jedis.get("username");
21 System.out.println(username);
22}
Spring管理jedis
Spring配置文件:
1<!-- 把Jedis相关对象交给spring管理 -->
2<!-- 把jedisPoolCOnfig交给spirng管理 -->
3<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
4 <property name="maxIdle" value="20"></property>
5 <property name="maxTotal" value="1000"></property>
6</bean>
7
8<!-- 把jedisPool交给spring管理 -->
9<bean class="redis.clients.jedis.JedisPool">
10 <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
11 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
12 <constructor-arg name="port" value="6379"></constructor-arg>
13</bean>
测试代码:
1/**
2 * 需求:使用spring管理jedis,连接redis服务
3 */
4@Test
5public void springLinkRedis() {
6 // 加载spring配置文件
7 ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
8 //获取jedisPool对象
9 JedisPool jedisPool = app.getBean(JedisPool.class);
10 //从连接池中获取jedis对象
11 Jedis jedis = jedisPool.getResource();
12 //给redis服务设置值
13 jedis.set("email", "xxx@xx.com");
14 String email = jedis.get("email");
15 System.out.println(email);
16}
17. 连接异常问题处理
1public class TestPing {
2 public static void main(String[] args) {
3 Jedis jedis = new Jedis("192.168.201.128", 6379);
4 System.out.println(jedis.ping());
5 }
6}
运行报错,控制台信息如下:
1Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
关闭虚拟机的防火墙
暂时关闭防火墙:/etc/init.d/iptables stop
重启虚拟机生效:chkconfig iptables off 或者/sbin/shconfig --level
2345 iptables off
查看防火墙状态:/etc/init.d/iptables status
继续运行上述代码,报错如下:
1Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
解决方案:修改redis.conf配置文件,将端口127.0.0.1注释掉,这样任何IP都能访问
1Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException:
2DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:
31) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
42) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.
53) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
64) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
7
这个异常大致意思:拒绝redis在保护模式下运行的,没有绑定IP地址,没有授权密码,如果你想从外部电脑连接redis的话,你必须要采纳以下其中的某一个解决方法
楼主采用的第二种方法,修改redis.conf配置文件,将protected-mode yes 改为no,如下图所示
重启虚拟机,启动redis服务 即可正常使用了
18. Redis集群
集群思想
为什么搭建集群?
备份
容错
提高并发量
集群思想:备份
缺点:
备份数据量太大,同步数据速度太慢,占用IO资源
集群思想:分块
Redis集群搭建
集群环境准备
redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:安装ruby
1yum install ruby(联网和使用磁盘镜像都可以)
2yum install rubygems(联网使用磁盘镜像都可)
3
安装ruby和redis的接口程序
拷贝redis-3.0.0.gem(ruby和redis接口)至/usr/local下
执行:
1gem install /usr/local/redis-3.0.0.gem
redis集群
redis集群,必须是多台redis服务。模拟8台redis服务。
创建8个目录7001,7002…7008(保证目录名和redis端口号一致)
创建8个目录
1[root@zk01 clusterRedis]# mkdir 700{1..8}
2[root@zk01 clusterRedis]# ll
3total 32
4drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7001
5drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7002
6drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7003
7drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7004
8drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7005
9drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7006
10drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7007
11drwxr-xr-x. 2 root root 4096 Aug 26 00:36 7008
拷贝安装目录bin到7001
1[root@zk01 clusterRedis]# cp -r ../redis/bin 7001/
2[root@zk01 clusterRedis]# cd 7001/
3[root@zk01 7001]# ll
4total 4
5drwxr-xr-x. 2 root root 4096 Aug 26 00:37 bin
开启集群
修改redis服务配置文件,开启集群配置:
修改端口:
1Port 7001
拷贝开启集群配置7001安装目录到每一个文件目录
1[root@zk01 clusterRedis]# cp -r 7001/bin 7002/
2[root@zk01 clusterRedis]# cp -r 7001/bin 7003
3[root@zk01 clusterRedis]# cp -r 7001/bin 7004
4[root@zk01 clusterRedis]# cp -r 7001/bin 7005
5[root@zk01 clusterRedis]# cp -r 7001/bin 7006
6[root@zk01 clusterRedis]# cp -r 7001/bin 7007
7[root@zk01 clusterRedis]# cp -r 7001/bin 7008
修改端口
修改每一个redis服务端口:7002…7008拷贝搭建插件
创建集群:使用redis-trib.rb插件创建集群,拷贝插件到集群目录clusterRedis
命令:
1cp redis-trib.rb ../../clusterRedis/
启动redis服务
1[root@zk01 clusterRedis]# cd 7001/bin/
2[root@zk01 bin]# ./redis-server redis.conf
3[root@zk01 bin]# cd ..
4[root@zk01 7001]# cd ..
5[root@zk01 clusterRedis]# cd 7002/bin/
6[root@zk01 bin]# ./redis-server redis.conf
7[root@zk01 bin]# cd ../../
8[root@zk01 clusterRedis]# cd 7003/bin/
9[root@zk01 bin]# ./redis-server redis.conf
10[root@zk01 bin]# cd ../../7004/bin/
11[root@zk01 bin]# ./redis-server redis.conf
12[root@zk01 bin]# cd ../../7005/bin/
13[root@zk01 bin]# ./redis-server redis.conf
14[root@zk01 bin]# cd ../../7006/bin/
15[root@zk01 bin]# ./redis-server redis.conf
查看redis启动进程:
1[root@zk01 bin]# ps -ef | grep redis
2root 8933 1 0 Aug25 ? 00:00:08 ./redis-server *:6379
3root 8997 1 0 Aug25 ? 00:00:07 ./redis-server *:6380
4root 9370 1 0 00:48 ? 00:00:00 ./redis-server *:7001 [cluster]
5root 9374 1 0 00:48 ? 00:00:00 ./redis-server *:7002 [cluster]
6root 9379 1 0 00:48 ? 00:00:00 ./redis-server *:7003 [cluster]
7root 9383 1 0 00:48 ? 00:00:00 ./redis-server *:7004 [cluster]
8root 9387 1 0 00:48 ? 00:00:00 ./redis-server *:7005 [cluster]
9root 9391 1 0 00:49 ? 00:00:00 ./redis-server *:7006 [cluster]
创建集群
创建集群命令:
1./redis-trib.rb create --replicas 1 192.168.66.66:7001 192.168.66.66:7002 192.168.66.66:7003 192.168.66.66:7004 192.168.66.66:7005 192.168.66.66:7006
create 是redis-trib命令,创建集群命令
--replicas 1 指定每台主机至少有一台从机
后面参数:需要加入集群服务器节点
1[root@zk01 clusterRedis]# ./redis-trib.rb create --replicas 1 192.168.66.66:7001 192.168.66.66:7002 192.168.66.66:7003 192.168.66.66:7004 192.168.66.66:7005 192.168.66.66:7006
2>>> Creating cluster
3>>> Performing hash slots allocation on 6 nodes...
4Using 3 masters:
5192.168.66.66:7001
6192.168.66.66:7002
7192.168.66.66:7003
8Adding replica 192.168.66.66:7004 to 192.168.66.66:7001
9Adding replica 192.168.66.66:7005 to 192.168.66.66:7002
10Adding replica 192.168.66.66:7006 to 192.168.66.66:7003
11M: d19f42568aee107eb07af31db0064797d49405f5 192.168.66.66:7001
12 slots:0-5460 (5461 slots) master
13M: 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 192.168.66.66:7002
14 slots:5461-10922 (5462 slots) master
15M: 24b4c7cfd52c5d05d195741f8981a4c98f67bd83 192.168.66.66:7003
16 slots:10923-16383 (5461 slots) master
17S: 5d1f2e78fd576f2339d112bc527841d79b342829 192.168.66.66:7004
18 replicates d19f42568aee107eb07af31db0064797d49405f5
19S: 79d73d668a20b005d92b1cbaddc67b45716252ca 192.168.66.66:7005
20 replicates 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382
21S: da2ca07b9d6e627e97b89b3893f49a305b72b55a 192.168.66.66:7006
22 replicates 24b4c7cfd52c5d05d195741f8981a4c98f67bd83
23Can I set the above configuration? (type 'yes' to accept): yes
24>>> Nodes configuration updated
25>>> Assign a different config epoch to each node
26>>> Sending CLUSTER MEET messages to join the cluster
27Waiting for the cluster to join.....
28>>> Performing Cluster Check (using node 192.168.66.66:7001)
29M: d19f42568aee107eb07af31db0064797d49405f5 192.168.66.66:7001
30 slots:0-5460 (5461 slots) master
31M: 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 192.168.66.66:7002
32 slots:5461-10922 (5462 slots) master
33M: 24b4c7cfd52c5d05d195741f8981a4c98f67bd83 192.168.66.66:7003
34 slots:10923-16383 (5461 slots) master
35M: 5d1f2e78fd576f2339d112bc527841d79b342829 192.168.66.66:7004
36 slots: (0 slots) master
37 replicates d19f42568aee107eb07af31db0064797d49405f5
38M: 79d73d668a20b005d92b1cbaddc67b45716252ca 192.168.66.66:7005
39 slots: (0 slots) master
40 replicates 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382
41M: da2ca07b9d6e627e97b89b3893f49a305b72b55a 192.168.66.66:7006
42 slots: (0 slots) master
43 replicates 24b4c7cfd52c5d05d195741f8981a4c98f67bd83
44[OK] All nodes agree about slots configuration.
45>>> Check for open slots...
46>>> Check slots coverage...
47[OK] All 16384 slots covered.
48
登录集群
登录命令:
1./redis-cli –c –h ip –p port
2[root@zk01 bin]# ./redis-cli -c -h 192.168.66.66 -p 7001//登录集群
3192.168.66.66:7001> cluster info //查询集群状态
4cluster_state:ok
5cluster_slots_assigned:16384
6cluster_slots_ok:16384
7cluster_slots_pfail:0
8cluster_slots_fail:0
9cluster_known_nodes:6
10cluster_size:3
11cluster_current_epoch:6
12cluster_my_epoch:1
13cluster_stats_messages_sent:397
14cluster_stats_messages_received:397
15192.168.66.66:7001> cluster nodes //查看集群节点信息
16da2ca07b9d6e627e97b89b3893f49a305b72b55a 192.168.66.66:7006 slave 24b4c7cfd52c5d05d195741f8981a4c98f67bd83 0 1472144198008 6 connected
1724b4c7cfd52c5d05d195741f8981a4c98f67bd83 192.168.66.66:7003 master - 0 1472144195963 3 connected 10923-16383
185d1f2e78fd576f2339d112bc527841d79b342829 192.168.66.66:7004 slave d19f42568aee107eb07af31db0064797d49405f5 0 1472144199438 4 connected
190f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 192.168.66.66:7002 master - 0 1472144199030 2 connected 5461-10922
20d19f42568aee107eb07af31db0064797d49405f5 192.168.66.66:7001 myself,master - 0 0 1 connected 0-5460
2179d73d668a20b005d92b1cbaddc67b45716252ca 192.168.66.66:7005 slave 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 0 1472144200054 5 connected
19. 动态扩展集群
需求,动态向集群中添加2台机器,保证2台机器是一主一备。
添加集群节点
Redis集群添加节点:默认add-node添加主节点
启动7007redis服务:
1./redis-trib.rb add-node 192.168.66.66:7007 192.168.66.66:7001
21)add-node 添加节点命令
32)192.168.66.66:7007 :需要添加到集群中节点(主节点)主机
43)192.168.66.66:7001:参考节点,添加集群节点到7001所在集群
5[root@zk01 clusterRedis]# ./redis-trib.rb add-node 192.168.66.66:7007 192.168.66.66:7001
6>>> Adding node 192.168.66.66:7007 to cluster 192.168.66.66:7001
7>>> Performing Cluster Check (using node 192.168.66.66:7001)
8M: d19f42568aee107eb07af31db0064797d49405f5 192.168.66.66:7001
9 slots:0-5460 (5461 slots) master
10 1 additional replica(s)
11S: da2ca07b9d6e627e97b89b3893f49a305b72b55a 192.168.66.66:7006
12 slots: (0 slots) slave
13 replicates 24b4c7cfd52c5d05d195741f8981a4c98f67bd83
14M: 24b4c7cfd52c5d05d195741f8981a4c98f67bd83 192.168.66.66:7003
15 slots:10923-16383 (5461 slots) master
16 1 additional replica(s)
17S: 5d1f2e78fd576f2339d112bc527841d79b342829 192.168.66.66:7004
18 slots: (0 slots) slave
19 replicates d19f42568aee107eb07af31db0064797d49405f5
20M: 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 192.168.66.66:7002
21 slots:5461-10922 (5462 slots) master
22 1 additional replica(s)
23S: 79d73d668a20b005d92b1cbaddc67b45716252ca 192.168.66.66:7005
24 slots: (0 slots) slave
25 replicates 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382
26[OK] All nodes agree about slots configuration.
27>>> Check for open slots...
28>>> Check slots coverage...
29[OK] All 16384 slots covered.
30>>> Send CLUSTER MEET to node 192.168.66.66:7007 to make it join the cluster.
31[OK] New node added correctly. 添加集群节点成功
32[root@zk01 clusterRedis]# cd 7001/bin/
33[root@zk01 bin]# ./redis-cli -c h 192.168.66.66 -p 7001
34(error) ERR unknown command 'h'
35[root@zk01 bin]# ./redis-cli -c -h 192.168.66.66 -p 7001
36192.168.66.66:7001> cluster nodes
37da2ca07b9d6e627e97b89b3893f49a305b72b55a 192.168.66.66:7006 slave 24b4c7cfd52c5d05d195741f8981a4c98f67bd83 0 1472144749370 6 connected
3836c2fb90bb615a51c0873d4ccaae0c73288e4a89 192.168.66.66:7007 master - 0 1472144747295 0 connected
3924b4c7cfd52c5d05d195741f8981a4c98f67bd83 192.168.66.66:7003 master - 0 1472144748345 3 connected 10923-16383
405d1f2e78fd576f2339d112bc527841d79b342829 192.168.66.66:7004 slave d19f42568aee107eb07af31db0064797d49405f5 0 1472144751309 4 connected
410f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 192.168.66.66:7002 master - 0 1472144751410 2 connected 5461-10922
42d19f42568aee107eb07af31db0064797d49405f5 192.168.66.66:7001 myself,master - 0 0 1 connected 0-5460
4379d73d668a20b005d92b1cbaddc67b45716252ca 192.168.66.66:7005 slave 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 0 1472144750393 5 connected
44
分配存储空间
分配存储空间命令:
1./redis-trib.rb reshard 192.168.66.66:7001
参考教案
添加从节点
启动7008redis服务:
1./redis-server redis.conf
给7007添加从节点:
添加从节点命令:
1./redis-trib.rb add-node --slave --master-id 36c2fb90bb615a51c0873d4ccaae0c73288e4a89 192.168.66.66:7008 192.168.66.66:7001
add-node 添加节点
--slave:添加从节点
--master-id : 主节点Id,表示添加的节点是此masterid从节点
192.168.101.3:7008:需要添加的节点
192.168.66.66:7001 :添加从节点到7001所在集群
1[root@zk01 clusterRedis]# ./redis-trib.rb add-node --slave --master-id 36c2fb90bb615a51c0873d4ccaae0c73288e4a89 192.168.66.66:7008 192.168.66.66:7001
2>>> Adding node 192.168.66.66:7008 to cluster 192.168.66.66:7001
3>>> Performing Cluster Check (using node 192.168.66.66:7001)
4M: d19f42568aee107eb07af31db0064797d49405f5 192.168.66.66:7001
5 slots:221-5460 (5240 slots) master
6 1 additional replica(s)
7S: da2ca07b9d6e627e97b89b3893f49a305b72b55a 192.168.66.66:7006
8 slots: (0 slots) slave
9 replicates 24b4c7cfd52c5d05d195741f8981a4c98f67bd83
10M: 36c2fb90bb615a51c0873d4ccaae0c73288e4a89 192.168.66.66:7007
11 slots:0-220,5461-5683,10923-11143 (665 slots) master
12 0 additional replica(s)
13M: 24b4c7cfd52c5d05d195741f8981a4c98f67bd83 192.168.66.66:7003
14 slots:11144-16383 (5240 slots) master
15 1 additional replica(s)
16S: 5d1f2e78fd576f2339d112bc527841d79b342829 192.168.66.66:7004
17 slots: (0 slots) slave
18 replicates d19f42568aee107eb07af31db0064797d49405f5
19M: 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382 192.168.66.66:7002
20 slots:5684-10922 (5239 slots) master
21 1 additional replica(s)
22S: 79d73d668a20b005d92b1cbaddc67b45716252ca 192.168.66.66:7005
23 slots: (0 slots) slave
24 replicates 0f3378f9402c62dc0ae7c4b4d07403ba5b3b2382
25[OK] All nodes agree about slots configuration.
26>>> Check for open slots...
27>>> Check slots coverage...
28[OK] All 16384 slots covered.
29>>> Send CLUSTER MEET to node 192.168.66.66:7008 to make it join the cluster.
30Waiting for the cluster to join.
31>>> Configure node as replica of 192.168.66.66:7007.
32[OK] New node added correctly.
33
20. Jedis连接集群
测试集群
1/**
2 * 需求:连接redis集群
3 */
4@Test
5public void linkRedisCluster(){
6 //创建集群连接池配置对象
7 JedisPoolConfig poolConfig = new JedisPoolConfig();
8 //设置最大连接数
9 poolConfig.setMaxTotal(1000);
10 //设置最大空闲数
11 poolConfig.setMaxIdle(20);
12
13 //创建set集合封装集群节点
14 Set<HostAndPort> nodes = new HashSet<HostAndPort>();
15 nodes.add(new HostAndPort("192.168.66.66", 7001));
16 nodes.add(new HostAndPort("192.168.66.66", 7002));
17 nodes.add(new HostAndPort("192.168.66.66", 7003));
18 nodes.add(new HostAndPort("192.168.66.66", 7004));
19 nodes.add(new HostAndPort("192.168.66.66", 7005));
20 nodes.add(new HostAndPort("192.168.66.66", 7006));
21 nodes.add(new HostAndPort("192.168.66.66", 7007));
22 nodes.add(new HostAndPort("192.168.66.66", 7008));
23 //创建redis集群核心对象
24 JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);
25
26 //集群操作redis集群
27 jedisCluster.set("alias", "baobao");
28 //获取值
29 String alias = jedisCluster.get("alias");
30 System.out.println(alias);
31
32}
Spring整合jedis集群
Spring配置文件:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3xmlns:context="http://www.springframework.org/schema/context"
4xmlns:p="http://www.springframework.org/schema/p"
5xmlns:aop="http://www.springframework.org/schema/aop"
6xmlns:tx="http://www.springframework.org/schema/tx"
7xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8xsi:schemaLocation="http://www.springframework.org/schema/beans
9http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
10http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context-4.0.xsd
12http://www.springframework.org/schema/aop
13http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
14http://www.springframework.org/schema/tx
15http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
16http://www.springframework.org/schema/util
17http://www.springframework.org/schema/util/spring-util-4.0.xsd">
18
19<!-- 把Jedis相关对象交给spring管理 -->
20<!-- 把jedisPoolCOnfig交给spirng管理 -->
21<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
22 <property name="maxIdle" value="20"></property>
23 <property name="maxTotal" value="1000"></property>
24</bean>
25
26<!-- 把jedisCluster交给spring管理 -->
27<bean class="redis.clients.jedis.JedisCluster">
28 <constructor-arg name="nodes">
29 <set>
30 <bean class="redis.clients.jedis.HostAndPort">
31 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
32 <constructor-arg name="port" value="7001"></constructor-arg>
33 </bean>
34
35 <bean class="redis.clients.jedis.HostAndPort">
36 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
37 <constructor-arg name="port" value="7002"></constructor-arg>
38 </bean>
39
40 <bean class="redis.clients.jedis.HostAndPort">
41 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
42 <constructor-arg name="port" value="7003"></constructor-arg>
43 </bean>
44
45 <bean class="redis.clients.jedis.HostAndPort">
46 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
47 <constructor-arg name="port" value="7004"></constructor-arg>
48 </bean>
49
50 <bean class="redis.clients.jedis.HostAndPort">
51 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
52 <constructor-arg name="port" value="7005"></constructor-arg>
53 </bean>
54
55 <bean class="redis.clients.jedis.HostAndPort">
56 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
57 <constructor-arg name="port" value="7006"></constructor-arg>
58 </bean>
59
60 <bean class="redis.clients.jedis.HostAndPort">
61 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
62 <constructor-arg name="port" value="7007"></constructor-arg>
63 </bean>
64
65 <bean class="redis.clients.jedis.HostAndPort">
66 <constructor-arg name="host" value="192.168.66.66"></constructor-arg>
67 <constructor-arg name="port" value="7008"></constructor-arg>
68 </bean>
69 </set>
70 </constructor-arg>
71 <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
72</bean>
73
74</beans>
加载spring配置文件:
1 /**
2 * 需求:使用spring管理jedis,连接redis集群服务
3 */
4 @Test
5 public void springLinkClusterRedis() {
6 // 加载spring配置文件
7 ApplicationContext app = new ClassPathXmlApplicationContext("jedisCluster.xml");
8 //获取jedisCluster对象
9 JedisCluster jedisCluster = app.getBean(JedisCluster.class);
10
11 //给redis服务设置值
12 jedisCluster.set("email", "babao@qq.com");
13 String email = jedisCluster.get("email");
14 System.out.println(email);
15 }