软件编程
位置:首页>> 软件编程>> java编程>> Redis集群原理详细分析

Redis集群原理详细分析

作者:上后左爱  发布时间:2021-08-06 07:34:00 

标签:Redis,集群

一致性哈希

节点的增加和减少,大部分节点的 Hash一致

Redis集群原理详细分析

package consistenthash
import (
"hash/crc32"
"sort"
)
// HashFunc defines function to generate hash code
type HashFunc func(data []byte) uint32
// NodeMap consistent hash hashFunc and you can pick node form NodeMap
type NodeMap struct {
hashFUnc    HashFunc
nodeHashs   []int          // sorted
nodehashMap map[int]string // 通过节点 找到具体的hash 值
}
func NewNodeMap(fn HashFunc) *NodeMap {
nodeMap := &NodeMap{
hashFUnc:    fn,
nodehashMap: make(map[int]string),
}
// default func
if nodeMap.hashFUnc == nil {
nodeMap.hashFUnc = crc32.ChecksumIEEE
}
return nodeMap
}
func (m *NodeMap) IsEmpty() bool {
return len(m.nodeHashs) == 0
}
func (m *NodeMap) AddNode(keys ...string) {
for _, key := range keys {
if key == "" {
continue
}
hash := int(m.hashFUnc([]byte(key)))
m.nodeHashs = append(m.nodeHashs, hash)
m.nodehashMap[hash] = key
}
sort.Ints(m.nodeHashs)
}
func (m *NodeMap) PickNode(key string) string {
if m.IsEmpty() {
return ""
}
hash := int(m.hashFUnc([]byte(key)))
// Binary Search sorted hash
idx := sort.Search(len(m.nodeHashs), func(i int) bool {
return m.nodeHashs[i] >= hash
})
if idx == len(m.nodeHashs) {
idx = 0
}
return m.nodehashMap[m.nodeHashs[idx]]
}

Redis 集群

每个单机版本redis 有 standalone_database, 在其上在封装一个 cluster_database ,cluster_database 主要的功能是进行 集群之间的通信。

当某个节点被推选出 主节点后,需要将信息转发给其副的节点,在转发的过程中涉及到cluster的池化

Redis集群原理详细分析

go-commons-pool比较好使用的go的池化工具

各种命令方式

ping 不需要转发

get/set 通过一致性hash 转发到其他节点

flushdb: 全发方式 所有节点数据清空

来源:https://blog.csdn.net/qq_27217017/article/details/126915758

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com