前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis Cluster

Redis Cluster

作者头像
一点博客
发布2019-07-24 10:43:28
6140
发布2019-07-24 10:43:28
举报
文章被收录于专栏:一点博客

Redis Cluster

Redis-Cluster简介

什么是Redis-Cluster

为何要搭建Redis集群。Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系统的Hbase或者是MogoDB。Redis更适合处理高并发,一台设备的存储能力是很有限的,但是多台设备协同合作,就可以让内存增大很多倍,这就需要用到集群。

Redis集群搭建的方式有多种,例如使用客户端分片、Twemproxy、Codis等,但从redis 3.0之后版本支持redis-cluster集群,它是Redis官方提出的解决方案,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。其redis-cluster架构图如下:

客户端与 redis 节点直连,不需要中间 proxy 层.客户端不需要连接集群所有节点连接集群中任何一个可用节点即可。

所有的 redis 节点彼此互联(PING-PONG 机制),内部使用二进制协议优化传输速度和带宽.

分布存储机制-槽

(1)redis-cluster 把所有的物理节点映射到[0-16383]slot 上,cluster 负责维护

node<->slot<->value

(2)Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。

例如三个节点:槽分布的值如下:

SERVER1: 0-5460

SERVER2: 5461-10922

SERVER3: 10923-16383

容错机制-投票

(1)选举过程是集群中所有master参与,如果半数以上master节点与故障节点通信超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操作. 故障节点对应的从节点自动升级为主节点

(2)什么时候整个集群不可用(cluster_state:fail)?

如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态.

2搭建Redis-Cluster

1搭建要求

需要 6 台 redis 服务器。搭建伪集群。

需要 6 个 redis 实例。

需要运行在不同的端口 7001-7006

2准备工作

(1)安装gcc 【此步省略】

Redis 是 c 语言开发的。安装 redis 需要 c 语言的编译环境。如果没有 gcc 需要在线安装。

yum install gcc-c++

(2)使用yum命令安装 ruby (我们需要使用ruby脚本来实现集群搭建)【此步省略】

yum install rubyyum install rubygems

----- 知识点小贴士 -----Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp 语言。由 Ruby 语言本身还发展出了JRuby(Java平台)、IronRuby(.NET平台)等其他平台的 Ruby 语言替代品。Ruby的作者于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。因为Perl发音与6月诞生石pearl(珍珠)相同,因此Ruby以7月诞生石ruby(红宝石)命名RubyGems简称gems,是一个用于对 Ruby组件进行打包的 Ruby 打包系统

(3)将redis源码包上传到 linux 系统 ,解压redis源码包

(4)编译redis源码 ,进入redis源码文件夹

make

看到以下输出结果,表示编译成功

(5)创建目录/usr/local/redis-cluster目录, 安装6个redis实例,分别安装在以下目录

/usr/local/redis-cluster/redis-1

/usr/local/redis-cluster/redis-2

/usr/local/redis-cluster/redis-3

/usr/local/redis-cluster/redis-4

/usr/local/redis-cluster/redis-5

/usr/local/redis-cluster/redis-6

以第一个redis实例为例,命令如下

make install PREFIX=/usr/local/redis-cluster/redis-1

出现此提示表示成功,按此方法安装其余5个redis实例

(6)复制配置文件 将 /redis-3.0.0/redis.conf 复制到redis下的bin目录下

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-1/bin [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-2/bin [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-3/bin [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-4/bin [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-5/bin [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-6/bin

3配置集群

(1)修改每个redis节点的配置文件redis.conf

修改运行端口为7001 (7002 7003 .....)

将cluster-enabled yes 前的注释去掉(632行)

(2)启动每个redis实例

以第一个实例为例,命令如下

cd /usr/local/redis-cluster/redis-1/bin/ ./redis-server redis.conf

把其余的5个也启动起来,然后查看一下是不是都启动起来了

[root@localhost ~]# ps -ef | grep redis root 15776 15775 0 08:19 pts/1 00:00:00 ./redis-server *:7001 [cluster] root 15810 15784 0 08:22 pts/2 00:00:00 ./redis-server *:7002 [cluster] root 15831 15813 0 08:23 pts/3 00:00:00 ./redis-server *:7003 [cluster] root 15852 15834 0 08:23 pts/4 00:00:00 ./redis-server *:7004 [cluster] root 15872 15856 0 08:24 pts/5 00:00:00 ./redis-server *:7005 [cluster] root 15891 15875 0 08:24 pts/6 00:00:00 ./redis-server *:7006 [cluster] root 15926 15895 0 08:24 pts/7 00:00:00 grep redis

(3)上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本。

[root@localhost ~]# gem install redis-3.0.0.gem Successfully installed redis-3.0.0 1 gem installed Installing ri documentation for redis-3.0.0... Installing RDoc documentation for redis-3.0.0...

(4)使用 ruby 脚本搭建集群。

进入redis源码目录中的src目录 执行下面的命令

./redis-trib.rb create --replicas 1 192.168.25.140:7001 192.168.25.140:7002 192.168.25.140:7003192.168.25.140:7004 192.168.25.140:7005 192.168.25.140:7006

出现下列提示信息

>>> Creating cluster Connecting to node 192.168.25.140:7001: OK Connecting to node 192.168.25.140:7002: OK Connecting to node 192.168.25.140:7003: OK Connecting to node 192.168.25.140:7004: OK Connecting to node 192.168.25.140:7005: OK Connecting to node 192.168.25.140:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.25.140:7001 192.168.25.140:7002 192.168.25.140:7003 Adding replica 192.168.25.140:7004 to 192.168.25.140:7001 Adding replica 192.168.25.140:7005 to 192.168.25.140:7002 Adding replica 192.168.25.140:7006 to 192.168.25.140:7003 M: 1800237a743c2aa918ade045a28128448c6ce689 192.168.25.140:7001 slots:0-5460 (5461 slots) master M: 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d 192.168.25.140:7002 slots:5461-10922 (5462 slots) master M: 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 192.168.25.140:7003 slots:10923-16383 (5461 slots) master S: c2a39a94b5f41532cd83bf6643e98fc277c2f441 192.168.25.140:7004 replicates 1800237a743c2aa918ade045a28128448c6ce689 S: b0e38d80273515c84b1a01820d8ecee04547d776 192.168.25.140:7005 replicates 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d S: 03bf6bd7e3e6eece5a02043224497c2c8e185132 192.168.25.140:7006 replicates 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 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.25.140:7001) M: 1800237a743c2aa918ade045a28128448c6ce689 192.168.25.140:7001 slots:0-5460 (5461 slots) master M: 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d 192.168.25.140:7002 slots:5461-10922 (5462 slots) master M: 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 192.168.25.140:7003 slots:10923-16383 (5461 slots) master M: c2a39a94b5f41532cd83bf6643e98fc277c2f441 192.168.25.140:7004 slots: (0 slots) master replicates 1800237a743c2aa918ade045a28128448c6ce689 M: b0e38d80273515c84b1a01820d8ecee04547d776 192.168.25.140:7005 slots: (0 slots) master replicates 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d M: 03bf6bd7e3e6eece5a02043224497c2c8e185132 192.168.25.140:7006 slots: (0 slots) master replicates 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.

连接Redis-Cluster

1客户端工具连接

Redis-cli 连接集群:

redis-cli -p 主机ip -p 端口(集群中任意端口) -c

-c:代表连接的是 redis 集群

测试值的存取:

(1)从本地连接到集群redis 使用7001端口 加 -c 参数

(2)存入name值为abc ,系统提示此值被存入到了7002端口所在的redis (槽是5798)

(3)提取name的值,可以提取。

(4)退出(quit)

(5)再次以7001端口进入 ,不带-c

(6)查询name值,无法获取,因为值在7002端口的redis上

(7)我们以7002端口进入,获取name值发现是可以获取的,而以其它端口进入均不能获取

2 SpringDataRedis连接Redis集群

修改品优购工程 在pinyougou-common工程添加spring 配置文件

applicationContext-redis-cluster.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:properties/redis-cluster-config.properties" /> <bean id="redis-clusterConfiguration" class="org.springframework.data.redis.connection.redis-clusterConfiguration"> <property name="maxRedirects" value="${redis.maxRedirects}"></property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.redis-clusterNode"> <constructor-arg name="host" value="${redis.host1}"></constructor-arg> <constructor-arg name="port" value="${redis.port1}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.redis-clusterNode"> <constructor-arg name="host" value="${redis.host2}"></constructor-arg> <constructor-arg name="port" value="${redis.port2}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.redis-clusterNode"> <constructor-arg name="host" value="${redis.host3}"></constructor-arg> <constructor-arg name="port" value="${redis.port3}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.redis-clusterNode"> <constructor-arg name="host" value="${redis.host4}"></constructor-arg> <constructor-arg name="port" value="${redis.port4}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.redis-clusterNode"> <constructor-arg name="host" value="${redis.host5}"></constructor-arg> <constructor-arg name="port" value="${redis.port5}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.redis-clusterNode"> <constructor-arg name="host" value="${redis.host6}"></constructor-arg> <constructor-arg name="port" value="${redis.port6}"></constructor-arg> </bean> </set> </property> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> </bean> <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <constructor-arg ref="redis-clusterConfiguration" /> <constructor-arg ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jeidsConnectionFactory" /> </bean> </beans>

添加属性文件redis-cluster-config.properties

#cluster configuration redis.host1=192.168.25.140 redis.port1=7001 redis.host2=192.168.25.140 redis.port2=7002 redis.host3=192.168.25.140 redis.port3=7003 redis.host4=192.168.25.140 redis.port4=7004 redis.host5=192.168.25.140 redis.port5=7005 redis.host6=192.168.25.140 redis.port6=7006 redis.maxRedirects=3 redis.maxIdle=100 redis.maxTotal=600

模拟集群异常测试

关闭节点命令

./redis-cli -p 端口 shutdown

(1)测试关闭7001 和7004, 看看会发生什么。

(2)测试关闭7001、7002、7003 会发生什么。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 一点博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Redis-Cluster简介
    • 什么是Redis-Cluster
      • 分布存储机制-槽
        • 容错机制-投票
        • 2搭建Redis-Cluster
          • 1搭建要求
            • 2准备工作
              • 3配置集群
              • 连接Redis-Cluster
                • 1客户端工具连接
                  • 2 SpringDataRedis连接Redis集群
                  • 模拟集群异常测试
                  相关产品与服务
                  云数据库 Redis
                  腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档