04_Redis集群

redis主题

01_Redis介绍和安装运行
02_Jedis的介绍和使用
03_Redis数据类型和数据操作的命令
04_Redis集群

Redis集群架构

  • redis-cluster架构图


  • redis-cluster投票-容错机制


  • 架构细节:
  • 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
  • 节点的fail是通过集群中超过半数的节点检测失效时才生效.
  • 客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
  • redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
  • Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点

Redis集群的搭建

Redis集群中至少应该有三个节点。要保证集群的高可用,需要每个节点有一个备份机。Redis集群至少需要6台服务器。
  搭建伪分布式。可以使用一台虚拟机运行6个redis实例。需要修改redis的端口号7001-7006

  • 集群搭建环境
    需要6台redis服务器。搭建伪分布式。
    需要6个redis实例。
    需要运行在不同的端口7001-7006

实践环境步骤

  • 配置redis
  • 创建目录、复制redis、删除原来数据
[root@training taotao-servers]# mkdir redis-cluster
[root@training taotao-servers]# cp -r redis/ redis-cluster/redis01
[root@training taotao-servers]# cd redis-cluster/redis01/
[root@training redis01]# rm -f dump.rdb 
[root@training redis01]# rm -f nodes.conf 
  • 编辑配置文件
vi redis.conf 
  • 修改端口为7001
port 7001
  • 启动集群模式,一般下面这行是注释掉的,我们打开注释就可以了
cluster-enabled yes
  • 保存退出
  • 启动6个redis
  • 复制6份redis01
[root@training redis01]# cd ../
[root@training redis-cluster]# cp -r redis01/ redis02
[root@training redis-cluster]# cp -r redis01/ redis03
[root@training redis-cluster]# cp -r redis01/ redis04
[root@training redis-cluster]# cp -r redis01/ redis05
[root@training redis-cluster]# cp -r redis01/ redis06
  • 端口分别修改为7001-7006
[root@training redis-cluster]# vi redis02/redis.conf
[root@training redis-cluster]# vi redis03/redis.conf 
[root@training redis-cluster]# vi redis04/redis.conf 
[root@training redis-cluster]# vi redis05/redis.conf 
[root@training redis-cluster]# vi redis06/redis.conf
  • 创建批处理脚本启动6个redis
[root@training redis-cluster]# vi redis-start-all.sh

添加如下:

cd redis01/
./redis-server redis.conf
cd ../
cd redis02/
./redis-server redis.conf
cd ../
cd redis03/
./redis-server redis.conf
cd ../
cd redis04/
./redis-server redis.conf
cd ../
cd redis05/
./redis-server redis.conf
cd ../
cd redis06/
./redis-server redis.conf
cd ../

保存退出

  • .sh脚本添加执行权限并执行.sh脚本,启动6个redis
[root@training redis-cluster]# chmod +x redis-start-all.sh 
[root@training redis-cluster]# ./redis-start-all.sh 
[root@training redis-cluster]# ps aux|grep redis
root      2654  0.1  0.1  33936  2052 ?        Ssl  20:20   0:00 ./redis-server *:7001 [cluster]
root      2656  0.1  0.1  33936  2052 ?        Ssl  20:20   0:00 ./redis-server *:7002 [cluster]
root      2658  0.1  0.1  33936  2060 ?        Ssl  20:20   0:00 ./redis-server *:7003 [cluster]
root      2666  0.1  0.1  33936  2056 ?        Ssl  20:20   0:00 ./redis-server *:7004 [cluster]
root      2670  0.1  0.1  33936  2052 ?        Ssl  20:20   0:00 ./redis-server *:7005 [cluster]
root      2672  0.1  0.1  33936  2060 ?        Ssl  20:20   0:00 ./redis-server *:7006 [cluster]
root      2678  0.0  0.0   4360   760 pts/0    S+   20:20   0:00 grep redis
[root@training redis-cluster]# 
  • 使用ruby脚本搭建集群。需要ruby的运行环境。安装ruby
yum install yum

安装ruby进度
  • 安装ruby脚本运行的依赖包
  • 上传redis-3.0.0.gem到linux,文件在电脑的G:\Java\JavaEE\10_淘淘商城\参考资料\redis\ruby和redis接口目录下
  • 安装gem:gem install redis-3.0.0.gem
[root@training redis-cluster]# 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...
  • /opt/redis/redis-3.0.0/src目录下,有redis-trib.rb文件,拷贝到集群的目录
[root@training src]# cp redis-trib.rb /usr/local/taotao-servers/redis-cluster/
[root@training src]# cd /usr/local/taotao-servers/redis-cluster/
[root@training redis-cluster]# ls -l
total 136
drwxr-xr-x. 2 root root  4096 May  6 20:26 redis01
drwxr-xr-x. 2 root root  4096 May  6 20:27 redis02
drwxr-xr-x. 2 root root  4096 May  6 20:28 redis03
drwxr-xr-x. 2 root root  4096 May  6 20:28 redis04
drwxr-xr-x. 2 root root  4096 May  6 20:28 redis05
drwxr-xr-x. 2 root root  4096 May  6 20:28 redis06
-rw-r--r--. 1 root root 57856 May  6 20:45 redis-3.0.0.gem
-rwxr-xr-x. 1 root root   270 May  6 20:14 redis-start-all.sh
-rwxr-xr-x. 1 root root 48141 May  6 20:54 redis-trib.rb
[root@training redis-cluster]# 
  • 执行命令,启动集群
./redis-trib.rb create --replicas 1 192.168.25.175:7001 192.168.25.175:7002 192.168.25.175:7003 192.168.25.175:7004 192.168.25.175:7005 192.168.25.175:7006

进度信息

[root@training redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.175:7001 192.168.25.175:7002 192.168.25.175:7003 192.168.25.175:7004 192.168.25.175:7005 192.168.25.175:7006
>>> Creating cluster
Connecting to node 192.168.25.175:7001: OK
Connecting to node 192.168.25.175:7002: OK
Connecting to node 192.168.25.175:7003: OK
Connecting to node 192.168.25.175:7004: OK
Connecting to node 192.168.25.175:7005: OK
Connecting to node 192.168.25.175:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.25.175:7001
192.168.25.175:7002
192.168.25.175:7003
Adding replica 192.168.25.175:7004 to 192.168.25.175:7001
Adding replica 192.168.25.175:7005 to 192.168.25.175:7002
Adding replica 192.168.25.175:7006 to 192.168.25.175:7003
M: 7ce324b94382d489ff18c053358854a440971b80 192.168.25.175:7001
   slots:0-5460 (5461 slots) master
M: 5f0d37c049a60c21931925da315507edcc7e26b6 192.168.25.175:7002
   slots:5461-10922 (5462 slots) master
M: 800e37dcf260acd7e66964af7dc3fae66a3e2ace 192.168.25.175:7003
   slots:10923-16383 (5461 slots) master
S: 7be793782772080d047e1d021be451d488880713 192.168.25.175:7004
   replicates 7ce324b94382d489ff18c053358854a440971b80
S: 0db5ae4287d7c689fb5d6d313c5235c3041814ed 192.168.25.175:7005
   replicates 5f0d37c049a60c21931925da315507edcc7e26b6
S: f8a73e8d2f0aa934aab4557654bd321d3bdff429 192.168.25.175:7006
   replicates 800e37dcf260acd7e66964af7dc3fae66a3e2ace
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.175:7001)
M: 7ce324b94382d489ff18c053358854a440971b80 192.168.25.175:7001
   slots:0-5460 (5461 slots) master
M: 5f0d37c049a60c21931925da315507edcc7e26b6 192.168.25.175:7002
   slots:5461-10922 (5462 slots) master
M: 800e37dcf260acd7e66964af7dc3fae66a3e2ace 192.168.25.175:7003
   slots:10923-16383 (5461 slots) master
M: 7be793782772080d047e1d021be451d488880713 192.168.25.175:7004
   slots: (0 slots) master
   replicates 7ce324b94382d489ff18c053358854a440971b80
M: 0db5ae4287d7c689fb5d6d313c5235c3041814ed 192.168.25.175:7005
   slots: (0 slots) master
   replicates 5f0d37c049a60c21931925da315507edcc7e26b6
M: f8a73e8d2f0aa934aab4557654bd321d3bdff429 192.168.25.175:7006
   slots: (0 slots) master
   replicates 800e37dcf260acd7e66964af7dc3fae66a3e2ace
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@training redis-cluster]# 

集群的使用方法

Redis-cli连接集群。

[root@localhost redis-cluster]# redis01/redis-cli -p 7002 -c

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

Jedis

需要把jedis依赖的jar包添加到工程中。Maven工程中需要把jedis的坐标添加到依赖。
推荐添加到服务层。Taotao-content-Service工程中。

  • 连接单机版
    第一步:创建一个Jedis对象。需要指定服务端的ip及端口。
    第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。
    第三步:打印结果。
    第四步:关闭Jedis
    @Test
    public void testJedis() throws Exception {
        // 第一步:创建一个Jedis对象。需要指定服务端的ip及端口。
        Jedis jedis = new Jedis("192.168.25.153", 6379);
        // 第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。
        String result = jedis.get("hello");
        // 第三步:打印结果。
        System.out.println(result);
        // 第四步:关闭Jedis
        jedis.close();
    }
  • 连接单机版使用连接池
    第一步:创建一个JedisPool对象。需要指定服务端的ip及端口。
    第二步:从JedisPool中获得Jedis对象。
    第三步:使用Jedis操作redis服务器。
    第四步:操作完毕后关闭jedis对象,连接池回收资源。
    第五步:关闭JedisPool对象。
@Test
    public void testJedisPool() throws Exception {
        // 第一步:创建一个JedisPool对象。需要指定服务端的ip及端口。
        JedisPool jedisPool = new JedisPool("192.168.25.153", 6379);
        // 第二步:从JedisPool中获得Jedis对象。
        Jedis jedis = jedisPool.getResource();
        // 第三步:使用Jedis操作redis服务器。
        jedis.set("jedis", "test");
        String result = jedis.get("jedis");
        System.out.println(result);
        // 第四步:操作完毕后关闭jedis对象,连接池回收资源。
        jedis.close();
        // 第五步:关闭JedisPool对象。
        jedisPool.close();
    }
  • 连接集群版
    第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。
    第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
    第三步:打印结果
    第四步:系统关闭前,关闭JedisCluster对象。
    @Test
    public void testJedisCluster() throws Exception {
        // 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.25.153", 7001));
        nodes.add(new HostAndPort("192.168.25.153", 7002));
        nodes.add(new HostAndPort("192.168.25.153", 7003));
        nodes.add(new HostAndPort("192.168.25.153", 7004));
        nodes.add(new HostAndPort("192.168.25.153", 7005));
        nodes.add(new HostAndPort("192.168.25.153", 7006));
        JedisCluster jedisCluster = new JedisCluster(nodes);
        // 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
        jedisCluster.set("hello", "100");
        String result = jedisCluster.get("hello");
        // 第三步:打印结果
        System.out.println(result);
        // 第四步:系统关闭前,关闭JedisCluster对象。
        jedisCluster.close();
    }

策略模式,连单机和集群

  • 添加spring配置文件applicationContext-jedis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
    
    <!-- 配置注解注入 -->
    <context:annotation-config/>
    
    <!-- redis单机版
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.25.175"/>
        <constructor-arg name="port" value="6379"/>
    </bean>
    <bean id="jedisClientPool" class="com.taotao.content.jedis.JedisClientPool"/>
     -->
     
    <!-- redis集群版 -->
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <constructor-arg>
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.25.175"/>
                    <constructor-arg name="port" value="7001"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.25.175"/>
                    <constructor-arg name="port" value="7002"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.25.175"/>
                    <constructor-arg name="port" value="7003"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.25.175"/>
                    <constructor-arg name="port" value="7004"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.25.175"/>
                    <constructor-arg name="port" value="7005"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.25.175"/>
                    <constructor-arg name="port" value="7006"/>
                </bean>
            </set>
        </constructor-arg>
    </bean>
    <bean id="jedisClientCluster" class="com.taotao.content.jedis.JedisClientCluster"></bean>
</beans>
  • junt测试类
    @Test
    public void testJedisClientPool() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-jedis.xml");
        JedisClient client = context.getBean(JedisClient.class);
        
        client.set("jedisclient", "jedisclient_test");
        String value = client.get("jedisclient");
        System.out.println(value);
    }
  • 在单机版和集群版切换时,我们只有修改Spring配置文件applicationContext-jedis.xml就可以,代码完全不需要修改
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容