04_Redis集群

redis主題

01_Redis介紹和安裝運(yùn)行
02_Jedis的介紹和使用
03_Redis數(shù)據(jù)類型和數(shù)據(jù)操作的命令
04_Redis集群

Redis集群架構(gòu)

  • redis-cluster架構(gòu)圖


  • redis-cluster投票-容錯(cuò)機(jī)制


  • 架構(gòu)細(xì)節(jié):
  • 所有的redis節(jié)點(diǎn)彼此互聯(lián)(PING-PONG機(jī)制),內(nèi)部使用二進(jìn)制協(xié)議優(yōu)化傳輸速度和帶寬.
  • 節(jié)點(diǎn)的fail是通過集群中超過半數(shù)的節(jié)點(diǎn)檢測(cè)失效時(shí)才生效.
  • 客戶端與redis節(jié)點(diǎn)直連,不需要中間proxy層.客戶端不需要連接集群所有節(jié)點(diǎn),連接集群中任何一個(gè)可用節(jié)點(diǎn)即可
  • redis-cluster把所有的物理節(jié)點(diǎn)映射到[0-16383]slot上,cluster 負(fù)責(zé)維護(hù)node<->slot<->value
  • Redis 集群中內(nèi)置了 16384 個(gè)哈希槽,當(dāng)需要在 Redis 集群中放置一個(gè) key-value 時(shí),redis 先對(duì) key 使用 crc16 算法算出一個(gè)結(jié)果,然后把結(jié)果對(duì) 16384 求余數(shù),這樣每個(gè) key 都會(huì)對(duì)應(yīng)一個(gè)編號(hào)在 0-16383 之間的哈希槽,redis 會(huì)根據(jù)節(jié)點(diǎn)數(shù)量大致均等的將哈希槽映射到不同的節(jié)點(diǎn)

Redis集群的搭建

Redis集群中至少應(yīng)該有三個(gè)節(jié)點(diǎn)。要保證集群的高可用,需要每個(gè)節(jié)點(diǎn)有一個(gè)備份機(jī)。Redis集群至少需要6臺(tái)服務(wù)器。
  搭建偽分布式??梢允褂靡慌_(tái)虛擬機(jī)運(yùn)行6個(gè)redis實(shí)例。需要修改redis的端口號(hào)7001-7006

  • 集群搭建環(huán)境
    需要6臺(tái)redis服務(wù)器。搭建偽分布式。
    需要6個(gè)redis實(shí)例。
    需要運(yùn)行在不同的端口7001-7006

實(shí)踐環(huán)境步驟

  • 配置redis
  • 創(chuàng)建目錄、復(fù)制redis、刪除原來數(shù)據(jù)
[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
  • 啟動(dòng)集群模式,一般下面這行是注釋掉的,我們打開注釋就可以了
cluster-enabled yes
  • 保存退出
  • 啟動(dòng)6個(gè)redis
  • 復(fù)制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
  • 創(chuàng)建批處理腳本啟動(dòng)6個(gè)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腳本添加執(zhí)行權(quán)限并執(zhí)行.sh腳本,啟動(dòng)6個(gè)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的運(yùn)行環(huán)境。安裝ruby
yum install yum

安裝ruby進(jìn)度
  • 安裝ruby腳本運(yùn)行的依賴包
  • 上傳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]# 
  • 執(zhí)行命令,啟動(dòng)集群
./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

進(jìn)度信息

[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的坐標(biāo)添加到依賴。
推薦添加到服務(wù)層。Taotao-content-Service工程中。

  • 連接單機(jī)版
    第一步:創(chuàng)建一個(gè)Jedis對(duì)象。需要指定服務(wù)端的ip及端口。
    第二步:使用Jedis對(duì)象操作數(shù)據(jù)庫,每個(gè)redis命令對(duì)應(yīng)一個(gè)方法。
    第三步:打印結(jié)果。
    第四步:關(guān)閉Jedis
    @Test
    public void testJedis() throws Exception {
        // 第一步:創(chuàng)建一個(gè)Jedis對(duì)象。需要指定服務(wù)端的ip及端口。
        Jedis jedis = new Jedis("192.168.25.153", 6379);
        // 第二步:使用Jedis對(duì)象操作數(shù)據(jù)庫,每個(gè)redis命令對(duì)應(yīng)一個(gè)方法。
        String result = jedis.get("hello");
        // 第三步:打印結(jié)果。
        System.out.println(result);
        // 第四步:關(guān)閉Jedis
        jedis.close();
    }
  • 連接單機(jī)版使用連接池
    第一步:創(chuàng)建一個(gè)JedisPool對(duì)象。需要指定服務(wù)端的ip及端口。
    第二步:從JedisPool中獲得Jedis對(duì)象。
    第三步:使用Jedis操作redis服務(wù)器。
    第四步:操作完畢后關(guān)閉jedis對(duì)象,連接池回收資源。
    第五步:關(guān)閉JedisPool對(duì)象。
@Test
    public void testJedisPool() throws Exception {
        // 第一步:創(chuàng)建一個(gè)JedisPool對(duì)象。需要指定服務(wù)端的ip及端口。
        JedisPool jedisPool = new JedisPool("192.168.25.153", 6379);
        // 第二步:從JedisPool中獲得Jedis對(duì)象。
        Jedis jedis = jedisPool.getResource();
        // 第三步:使用Jedis操作redis服務(wù)器。
        jedis.set("jedis", "test");
        String result = jedis.get("jedis");
        System.out.println(result);
        // 第四步:操作完畢后關(guān)閉jedis對(duì)象,連接池回收資源。
        jedis.close();
        // 第五步:關(guān)閉JedisPool對(duì)象。
        jedisPool.close();
    }
  • 連接集群版
    第一步:使用JedisCluster對(duì)象。需要一個(gè)Set<HostAndPort>參數(shù)。Redis節(jié)點(diǎn)的列表。
    第二步:直接使用JedisCluster對(duì)象操作redis。在系統(tǒng)中單例存在。
    第三步:打印結(jié)果
    第四步:系統(tǒng)關(guān)閉前,關(guān)閉JedisCluster對(duì)象。
    @Test
    public void testJedisCluster() throws Exception {
        // 第一步:使用JedisCluster對(duì)象。需要一個(gè)Set<HostAndPort>參數(shù)。Redis節(jié)點(diǎn)的列表。
        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對(duì)象操作redis。在系統(tǒng)中單例存在。
        jedisCluster.set("hello", "100");
        String result = jedisCluster.get("hello");
        // 第三步:打印結(jié)果
        System.out.println(result);
        // 第四步:系統(tǒng)關(guān)閉前,關(guān)閉JedisCluster對(duì)象。
        jedisCluster.close();
    }

策略模式,連單機(jī)和集群

  • 添加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單機(jī)版
    <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測(cè)試類
    @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);
    }
  • 在單機(jī)版和集群版切換時(shí),我們只有修改Spring配置文件applicationContext-jedis.xml就可以,代碼完全不需要修改
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容