Redis集群搭建


10台

250G/台

160節點

16節點/台

1. 安裝依賴

按照"redis集群所需要的依賴"中的解決方案,安裝依賴即可。

redis5中不再需要這些依賴!

2. 集群介紹

redis3.x 后的集群 ,是多主多從的集群,多個主機組成一個集群的主體,共同負責對外提供讀寫服務。

每個主機都有自己的從機,從機的主要工作是:1. 同步主機數據 2.當主機宕機時頂替主機。

3.集群搭建

注意,集群的搭建,至少需要6台虛擬機,因為至少3台主機才能形成集群,每個主機至少有一個從機。

3.1 搭建配置

# 當前虛擬機的Redis,開啟cluster支持
cluster-enabled yes
# 當前虛擬機的Redis 用來保存集群信息的文件,保存位置在工作目錄下
cluster-config-file nodes-9001.conf
# bind配置中不配置127.0.0.1,否則redis會自動以127.0.0.1作為ip,使得無法通過java遠程連接集群
bind 192.168.110.135

如上為兩個核心配置,完整配置文件,詳見 "rediss"目錄,

將此目錄直接上傳給linux,放在 "/usr/local"目錄即可。

如上配置目錄中,定義了7份配置,用來啟動7個redis服務(模擬7台虛擬機)

3.2 啟動redis

# 在/usr/local/rediss目錄下執行,啟動6個redis服務
# 注意是否有執行權限
./startall.sh
# 查看redis服務
ps -ef | grep redis
root       4042      1  0 10:12 ?        00:00:00 redis-server zhj:9001 [cluster]
root       4044      1  0 10:12 ?        00:00:00 redis-server zhj:9002 [cluster]
root       4049      1  0 10:12 ?        00:00:00 redis-server zhj:9003 [cluster]
root       4054      1  0 10:12 ?        00:00:00 redis-server zhj:9004 [cluster]
root       4059      1  0 10:12 ?        00:00:00 redis-server zhj:9005 [cluster]
root       4064      1  0 10:12 ?        00:00:00 redis-server zhj:9006 [cluster]

3.3 創建集群

# 在 /usr/local/redis-4.0.14/src 目錄下執行,開始創建集群
./redis-trib.rb create --replicas 1 192.168.110.135:9001  192.168.110.135:9002  192.168.110.135:9003 192.168.110.135:9004 192.168.110.135:9005 192.168.110.135:9006

如下,最后輸入 “yes”

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.110.135:9001
192.168.110.135:9002
192.168.110.135:9003
Adding replica 192.168.110.135:9005 to 192.168.110.135:9001
Adding replica 192.168.110.135:9006 to 192.168.110.135:9002
Adding replica 192.168.110.135:9004 to 192.168.110.135:9003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: c3e026595c56e989cbcef7bcb6ac868b458141df 192.168.110.135:9001
   slots:0-5460 (5461 slots) master
M: 29fe6ceb1d7b698bfe161f8d8e15f9ee8d735527 192.168.110.135:9002
   slots:5461-10922 (5462 slots) master
M: 67e692825590b41b5017e93c9090eed82fc9f3b1 192.168.110.135:9003
   slots:10923-16383 (5461 slots) master
S: 27ecc39812069e764d880002d67bd252c78b051d 192.168.110.135:9004
   replicates 29fe6ceb1d7b698bfe161f8d8e15f9ee8d735527
S: 721c200f8e5cff46ab5b3332d9be01c988e37c9e 192.168.110.135:9005
   replicates 67e692825590b41b5017e93c9090eed82fc9f3b1
S: 07401d81b187002cbca95ec979580436a14e0d5d 192.168.110.135:9006
   replicates c3e026595c56e989cbcef7bcb6ac868b458141df
Can I set the above configuration? (type 'yes' to accept): yes

見到如下輸出,集群即創建完畢:

M: c3e026595c56e989cbcef7bcb6ac868b458141df 192.168.110.135:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 67e692825590b41b5017e93c9090eed82fc9f3b1 127.0.0.1:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 721c200f8e5cff46ab5b3332d9be01c988e37c9e 127.0.0.1:9005
   slots: (0 slots) slave
   replicates 67e692825590b41b5017e93c9090eed82fc9f3b1
S: 27ecc39812069e764d880002d67bd252c78b051d 127.0.0.1:9004
   slots: (0 slots) slave
   replicates 29fe6ceb1d7b698bfe161f8d8e15f9ee8d735527
M: 29fe6ceb1d7b698bfe161f8d8e15f9ee8d735527 127.0.0.1:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 07401d81b187002cbca95ec979580436a14e0d5d 127.0.0.1:9006
   slots: (0 slots) slave
   replicates c3e026595c56e989cbcef7bcb6ac868b458141df
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

4. 槽

slot : 槽,集群共有16384個slot,索引為 0-16383。如下為【主機1,2,3】【從機a,b,c】

M: c3e026595c56e989cbcef7bcb6ac868b458141df 192.168.110.135:9001 【主機1】
   slots:0-5460 (5461 slots) master 【主機1】
   1 additional replica(s) 【主機1有1個從機】
M: 67e692825590b41b5017e93c9090eed82fc9f3b1 127.0.0.1:9003 【主機2】
   slots:10923-16383 (5461 slots) master 【主機2】
   1 additional replica(s) 【主機2有1個從機】
S: 721c200f8e5cff46ab5b3332d9be01c988e37c9e 127.0.0.1:9005 【從機a】
   slots: (0 slots) slave 【從機a】
   replicates 67e692825590b41b5017e93c9090eed82fc9f3b1 【從機a的主機的id,則從機a是屬於主機2的】
S: 27ecc39812069e764d880002d67bd252c78b051d 127.0.0.1:9004 【從機b】
   slots: (0 slots) slave 【從機b】
   replicates 29fe6ceb1d7b698bfe161f8d8e15f9ee8d735527  【從機b的主機的id,則從機b是屬於主機3的】
M: 29fe6ceb1d7b698bfe161f8d8e15f9ee8d735527 127.0.0.1:9002 【主機3】
   slots:5461-10922 (5462 slots) master 【主機3】
   1 additional replica(s) 【主機3有1個從機】
S: 07401d81b187002cbca95ec979580436a14e0d5d 127.0.0.1:9006 【從機c】
   slots: (0 slots) slave 【從機c】
   replicates c3e026595c56e989cbcef7bcb6ac868b458141df  【從機c的主機的id,則從機c是屬於主機1的】
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

如上是集群中的3主 3從,主機1持有槽【0-5460】 主機2持有槽【10923-16383】 主機3持有槽【5461-10922】。

注意:只有主機才占有槽,槽並不是一個存儲單位。槽是redis集群做負載均衡的計量。

要進一步理解槽,需要看完 5,6兩個章節

5. 連接集群

 # -c 即代表要連接集群
 redis-cli -c -p 9001 -h 192.168.110.135

6. 集群操作

192.168.110.135:9001> set name zhj
-> Redirected to slot [5798] located at 127.0.0.1:9002 【經計算 name應該由9002存儲】
OK

192.168.110.135:9002> set gender true
-> Redirected to slot [15355] located at 127.0.0.1:9003 【經計算 gender應該由9003存儲】
OK

192.168.110.135:9003> set age 19
-> Redirected to slot [741] located at 127.0.0.1:9001  【經計算 age應該由9001存儲】
OK

所謂的計算,是對讀/寫 操作中的 key 做一致性hash運算,得到一個數字 num, 然后 num%16384

得到 一個 [0--16383]區間的數字

如 name 的運算結果為 5798 gender的運算結果為15355 age的運算結果為741

5798,15355,741是槽的下標,則對應的key由對應主機存儲

7.連接集群-java

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!-- 最多空閑連接數 -->
    <property name="maxIdle" value="1" />
    <!-- 最多有多少連接 -->
    <property name="maxTotal" value="5" />
    <property name="minIdle" value="1"></property>
    <!-- 連接數用完時,是否阻塞,阻塞超過maxWaitMillis會拋出異常 -->
    <property name="blockWhenExhausted" value="true" />
    <!-- 檢出連接時,最大等待時長 -->
    <property name="maxWaitMillis" value="30000" />
    <!-- 在檢出時是否檢測 -->
    <property name="testOnBorrow" value="false" />
    <!-- 空閑時是否檢測連接是否可用 -->
    <property name="testWhileIdle" value="false"></property>
    <!-- Evict=驅逐  連接至少要空閑多少時間才會成為可以被驅逐線程掃描並移除 -->
    <property name="minEvictableIdleTimeMillis" value="60000"></property>
    <!-- 驅逐線程 兩次驅逐之間要sleep的時間 如果小於0,則不會有驅逐線程,則minEvictableIdleTimeMillis無效-->
    <property name="timeBetweenEvictionRunsMillis" value="30000"></property>
    <!-- 驅逐線程每次最多掃描幾個連接 -->
    <property name="numTestsPerEvictionRun" value="3"></property>
    <!-- last in first out 檢出(獲取連接)策略 后入先出  或 先入先出 -->
    <property name="lifo" value="true"></property>
</bean>
<!-- 為 redisClusterConfiguration定制參數配置-->
<bean id="propertyPropertySource" class="org.springframework.core.env.PropertiesPropertySource">
    <constructor-arg index="0" type="java.lang.String" value="redisParam1911"></constructor-arg>
    <constructor-arg index="1" type="java.util.Properties">
        <props>
            <!-- 集群中的所有或部分節點ip:port -->
            <prop key="spring.redis.cluster.nodes">192.168.110.135:9006,192.168.110.135:9007</prop>
            <!--
                    默認為5,連接不到集群時,重試次數
                  -->
            <prop key="spring.redis.cluster.max-redirects">5</prop>
        </props>
    </constructor-arg>
</bean>
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
    <constructor-arg name="propertySource" ref="propertyPropertySource"/>
</bean>
<bean id="jedisConnectionFactory"
      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"></constructor-arg>
    <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<!-- redis template definition -->
<bean id="ss" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="fast" class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"></bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
      p:connection-factory-ref="jedisConnectionFactory"
      p:keySerializer-ref="ss"
      p:stringSerializer-ref="ss"
      p:valueSerializer-ref="fast"/>

無論連接的是 單點redis,還是集群的redis,Spring-Data-Redis的所有API不變

8. 添加主節點

8.1 添加新節點

增加一套配置,並以此配置啟動redis實例。並執行如下指令;

新機器的ip:port      集群某個成員的ip:port
[root@zhj04 src]# ./redis-trib.rb add-node 192.168.79.128:9007 192.168.79.128:9006

如果成功,會有如下輸出
>>> Adding node 192.168.79.128:9007 to cluster 192.168.79.128:9006
>>> Performing Cluster Check (using node 192.168.79.128:9006)
S: f6eb31694591a3d7d4d63a13d6775d0397ccabcc 192.168.79.128:9006
   slots: (0 slots) slave
   replicates 9a4d54b36e525118b1b9c0f94c65320e5584feb7
S: 3ababf9bd48fab51f0e786ba36dfbdab43e31741 192.168.79.128:9005
   slots: (0 slots) slave
   replicates 8c8acf426bc93340b01942b8567230db7bd82577
M: 8c8acf426bc93340b01942b8567230db7bd82577 192.168.79.128:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9b7d58f0be57d54e6044dc1a8101f12e0fddc849 192.168.79.128:9004
   slots: (0 slots) slave
   replicates 798e705d85c0e6bd432e7103a6b5ccad0f30f29c
M: 9a4d54b36e525118b1b9c0f94c65320e5584feb7 192.168.79.128:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 798e705d85c0e6bd432e7103a6b5ccad0f30f29c 192.168.79.128:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.79.128:9007 to make it join the cluster.
[OK] New node added correctly.

8.2 重新分片

新加的主機沒有slot,需要將已有主機的slot分出部分給它。最終保證所有主機的slot個數是均衡的。

執行如下指令,開始重新分片:

新機器的ip:port
[root@zhj04 src]# ./redis-trib.rb reshard 192.168.79.128:9008

下一步,會有如下輸出
How many slots do you want to move (from 1 to 16384)? [在此輸入要給新機器分配的槽的數量]

下一步,會有如下輸出
What is the receiving node ID? [在此輸入新機器的節點id]

下一步,會有如下輸出
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:[再次輸入"all"]

下一步,會有如下輸出
Do you want to proceed with the proposed reshard plan (yes/no)? [在此輸入”yes“]
之后便會進行分片操作

# 分片后,執行如下指令檢查 集群狀態
[root@zhj04 src]# ./redis-trib.rb check 192.168.79.128:9002

9.添加從節點

9.1 添加新節點

此過程和 8.1章節一樣,先復制一套配置,然后開啟新的redis節點.

然后,執行如下指令:

從機             主機id
[root@zhj04 src]# ./redis-trib.rb  add-node --slave --master-id ad27f044c31b8c3e0e43a5b1dff080144d9ca85f 192.168.79.128:9008 192.168.79.128:9006
新機器ip:port        已有及其ip:port


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM