Redis集群搭建(RedisCluster)
之前的Redis集群搭建(在虚拟机上)文档,放博客上以免丢失,仅供参考
哨兵模式请看:Redis搭建主从+哨兵
- Redis Cluster 的性能与单节点部署是同级别的。多主节点、负载均衡、读写分离
- Redis Cluster 支持标准的 主从复制配置来保障高可用和高可靠。
- failover(故障转移)
- Redis Cluster 也实现了一个类似 Raft 的共识方式,来保障整个集群的可用性。
- 向 Redis Cluster 中添加新节点,或者移除节点,都是透明的,不需要停机。
- 水平、垂直方向都非常容易扩展。
- 数据分区,海量数据,数据存储
- 部署 Redis Cluster 不需要其他的代理或者工具,而且 Redis Cluster 和单机 Redis 几乎完全兼容。
1. Redis简介
Redis是基于内存,也可以基于磁盘持久化nosql数据库,使用c语言开发。
数据存储结构:key-value
2. 安装环境准备
Redis使用c语言开发,需要使用gcc编译程序进行编译。
1) 安装gcc
安装命令:yum -y install gcc
2) 安装上传文件插件
- 安装命令:yum –y install lrzsz
- 上传命令:rz
3. 安装redis
3.1 上传安装包
使用rz命令上传redis安装包,到/usr/local/soft目录下:
[root@localhost soft]# ll
-rw-r--r--. 1 root root 1358081 May 14 2015 redis-3.0.0.tar.gz
3.2 解压
解压命令:tar -zxvf redis-3.0.0.tar.gz
3.3 安装redis
1) 编译
a) 进入redis解压目录:执行编译命令: make
b) 执行编译程序:生成编译文件在src目录下
2) 安装
a) 命令:make install PREFIX=/usr/local/soft/redis
b) 进入redis解压目录:执行安装命令
[root@localhost redis-3.0.0]# make install PREFIX=/usr/local/soft/redis
cd src && make install
make[1]: Entering directory `/usr/local/soft/redis-3.0.0/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/soft/redis-3.0.0/src'
3.4 启动redis服务
3.4.1 前台启动
启动命令:./redis-server
特点:默认启动前台服务,进程一种阻塞,不能直接退出,使用客户端进行登录。
3.4.2 后台启动
修改redis配置文件,redis.conf配置文件,此时bin安装目录没有配置文件,需要从解压目录中拷贝一份配置文件即可。
1) 拷贝redis.conf配置文件
a) redis.conf在redis解压目录中
b) 拷贝:cp redis.conf ../redis/bin/
[root@localhost bin]# ll
total 15520
-rw-r--r--. 1 root root 18 Jul 26 17:14 dump.rdb
-rwxr-xr-x. 1 root root 4587078 Jul 26 17:09 redis-benchmark
-rwxr-xr-x. 1 root root 22185 Jul 26 17:09 redis-check-aof
-rwxr-xr-x. 1 root root 45403 Jul 26 17:09 redis-check-dump
-rwxr-xr-x. 1 root root 4689993 Jul 26 17:09 redis-cli
-rw-r--r--. 1 root root 41403 Jul 26 17:16 redis.conf
lrwxrwxrwx. 1 root root 12 Jul 26 17:09 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 6448257 Jul 26 17:09 redis-server
2) 修改redis配置文件
a) daemonize no==daemonize yes
3) 启动redis,加载配置文件
a) 命令:./redis-server redis.conf
4) 登录redis
a) 登录命令:./redis-cli –h ip –p port
b) 登录:./redis-cli (默认登录6379端口redis服务)
4. Redis3.0集群
4.1 Redis集群搭建环境准备
redis版本>=5.xxx,直接使用 ./redis-cli --cluster create 指令构建redis集群。 redis版本<5.xxx,需要安装ruby、rubygems环境,使用 ./redis-trib.rb create 指令构建redis集群,
redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:
安装ruby
执行:
yum install ruby
再执行:
yum install rubygems
安装ruby和redis的接口程序
拷贝redis-3.0.0.gem(ruby和redis接口)至/usr/local下
执行:
gem install /usr/local/redis-3.0.0.gem
4.2 集群分析
搭建redis集群,必须的有多台redis服务器:保证一主一备(4台主机,4台从机),搭建6台redis服务器集群。
保证有8个redis服务:模拟8台redis服务(端口不冲突,开启集群配置)
搭建集群步骤:
1) 创建6个目录:7001,7002,7003。。。。。。。7006
目录名称和redis服务端口一致
2) 开启redis集群配置
3) 修改redis服务器端口
4) 删除持久化配置文件 (初始化集群必须保持纯洁性)
5) 启动6台服务器
6) 创建集群
4.3 创建集群目录
创建目录:redis-cluster
安装集群安装到redis-cluster目录下面。
1) 创建8个目录:
[root@localhost redis-cluster]# mkdir 700{1..6}
[root@localhost redis-cluster]# ll
total 32
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7001
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7002
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7003
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7004
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7005
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7006
2) 拷贝redis安装目录bin到7001:
[root@localhost redis]# cp -r bin/ ../redis-cluster/7001/
3) 修改配置文件,开启集群
cluster-enabled yes 把注释#去掉即可
4) 修改端口(redis配置文件:端口和目录名称相同)
port 7001
注意:
5) 拷贝7001目录下修改后的bin目录到7002…..7006等7个目录
drwxr-xr-x. 2 root root 4096 Jul 27 00:15 bin
[root@localhost 7001]# cp -r bin/ ../7002/
[root@localhost 7001]# cp -r bin/ ../7003/
[root@localhost 7001]# cp -r bin/ ../7004/
[root@localhost 7001]# cp -r bin/ ../7005/
[root@localhost 7001]# cp -r bin/ ../7006
6) 修改7002,7003………..7006redis服务端口
a) 要求redis服务端口和目录文件一致。
4.4 拷贝集群插件
使用redis-trib.rb插件创建集群:
redis-trib.rb插件位置在redis解压目录下的编译目录src中。
去src中拷贝插件到redis-Cluster集群目录中。
[root@localhost src]# cp redis-trib.rb /usr/local/soft/redis-cluster/
4.5 创建集群
1) 启动redis服务(启动6台redis服务)
[root@localhost redis-cluster]# cd 7001/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ..
[root@localhost 7001]# cd ..
[root@localhost redis-cluster]# cd 7002/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7003/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7004/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7005/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7006/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# ps -ef | grep redis
root 6926 1 0 Jul26 ? 00:00:21 ./redis-server *:6379
root 7637 1 0 00:25 ? 00:00:00 ./redis-server *:7001 [cluster]
root 7642 1 0 00:25 ? 00:00:00 ./redis-server *:7002 [cluster]
root 7646 1 0 00:25 ? 00:00:00 ./redis-server *:7003 [cluster]
root 7650 1 0 00:25 ? 00:00:00 ./redis-server *:7004 [cluster]
root 7655 1 0 00:26 ? 00:00:00 ./redis-server *:7005 [cluster]
root 7659 1 0 00:26 ? 00:00:00 ./redis-server *:7006 [cluster]
root 7663 6287 0 00:26 pts/0 00:00:00 grep redis
2) 创建集群
使用插件redis-trib.rb创建集群:
redis3.0创建命令:
./redis-trib.rb create --replicas 1 192.168.253.131:7001 192.168.253.131:7002 192.168.253.131:7003 192.168.253.131:7004 192.168.253.131:7005 192.168.253.131:7006
Create:创建集群
--replicas 1:每一台主机至少有一台从机。
集群成功创建信息:
>>> Creating cluster
Connecting to node 192.168.253.131:7001: OK
Connecting to node 192.168.253.131:7002: OK
Connecting to node 192.168.253.131:7003: OK
Connecting to node 192.168.253.131:7004: OK
Connecting to node 192.168.253.131:7005: OK
Connecting to node 192.168.253.131:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.253.131:7001
192.168.253.131:7002
192.168.253.131:7003
Adding replica 192.168.253.131:7004 to 192.168.253.131:7001
Adding replica 192.168.253.131:7005 to 192.168.253.131:7002
Adding replica 192.168.253.131:7006 to 192.168.253.131:7003
M: 1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001
slots:0-5460 (5461 slots) master
M: bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002
slots:5461-10922 (5462 slots) master
M: 2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003
slots:10923-16383 (5461 slots) master
S: 532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004
replicates 1b807d0b0bb849632e93853728f8ed3e51d0dd48
S: 7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005
replicates bd79a369ef0af24aa5e54455cae1f93e0cca636d
S: bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006
replicates 2dc7730fab2a36ad00937bde4ec70421e7656f40
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.253.131:7001)
M: 1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001
slots:0-5460 (5461 slots) master
M: bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002
slots:5461-10922 (5462 slots) master
M: 2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003
slots:10923-16383 (5461 slots) master
M: 532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004
slots: (0 slots) master
replicates 1b807d0b0bb849632e93853728f8ed3e51d0dd48
M: 7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005
slots: (0 slots) master
replicates bd79a369ef0af24aa5e54455cae1f93e0cca636d
M: bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006
slots: (0 slots) master
replicates 2dc7730fab2a36ad00937bde4ec70421e7656f40
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4.6 启动集群(批处理文件)
在redis-Cluster目录下创建启动集群批处理文件:start-all.sh
文件内容如下:
cd 7001/bin
./redis-server redis.conf
cd ../../7002/bin
./redis-server redis.conf
cd ../../7003/bin
./redis-server redis.conf
cd ../../7004/bin
./redis-server redis.conf
cd ../../7005/bin
./redis-server redis.conf
cd ../../7006/bin
./redis-server redis.conf
1) 运行启动sh文件
./start-all.sh
4.7 登录集群
登录命令:./redis-cli –c –h ip –p port
cd 7001/bin
查询集群状态:
[root@localhost bin]# ./redis-cli -c -h 192.168.253.131 -p 7001
192.168.253.131:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:433
cluster_stats_messages_received:433
查询集群节点信息:
192.168.253.131:7001> cluster nodes
bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002 master - 0 1469550879424 2 connected 5461-10922
2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003 master - 0 1469550877393 3 connected 10923-16383
7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005 slave bd79a369ef0af24aa5e54455cae1f93e0cca636d 0 1469550880434 5 connected
bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006 slave 2dc7730fab2a36ad00937bde4ec70421e7656f40 0 1469550881443 6 connected
532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004 slave 1b807d0b0bb849632e93853728f8ed3e51d0dd48 0 1469550878404 4 connected
1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001 myself,master - 0 0 1 connected 0-5460
5. Jedis连接集群
5.1 整合spring:
配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- Jedis配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="20"></property> <property name="maxTotal" value="1000"></property> </bean> <!-- 集群版Jedis --> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> </bean> </beans>
5.2 java测试代码:
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; public class SpringClusterJedis { /** * jedis连接集群版redis */ @Test public void jedisSpringRedis() { // 加载spring配置文件 ApplicationContext app = new ClassPathXmlApplicationContext( "applicationContext-jedisCluster.xml"); //获取jedisCluster对象 JedisCluster JedisCluster = app.getBean(JedisCluster.class); JedisCluster.set("username", "张三"); String username = JedisCluster.get("username"); System.out.println(username); } }