Compare commits

...

3 Commits

Author SHA1 Message Date
huangkanghui e4e5cd6019 hash-ring 2021-10-21 17:11:38 +08:00
huangkanghui f228712526 hash环构建 2021-10-21 16:53:15 +08:00
bandl b54f72c575 feat(ring-hash): add hash ring interface 2021-10-17 13:29:56 +08:00
3 changed files with 127 additions and 0 deletions

63
pkg/hash-ring/define.go Normal file
View File

@ -0,0 +1,63 @@
package hashring
import (
"sort"
)
type RingInterface interface {
// 获取一个 远程连接用的目标
GetTarget(key string) (*Target, error)
// 获取下一个目标
NextTarget(*Target) (*Target, error)
// 获取上一个目标
PreTarget(*Target) (*Target, error)
}
/**
获取一个 远程连接用的目标
*/
func GetTarget(key string) string {
var clusterHash map[int]string = createClusterHashRing()
/*
对集群key值从小到大排序
*/
var sortHash []int
for hash := range clusterHash {
sortHash = append(sortHash, hash)
}
/**
放入一个值
*/
var keys = key
var hashKey int = int(getHash(keys))
/**
建立映射关系
*/
var hashValue = make(map[int]string)
for value := range clusterHash {
hashValue[value-hashKey] = clusterHash[value]
}
var hashMap []int
for k := range hashValue {
hashMap = append(hashMap, k)
}
sort.Ints(hashMap)
var ip string
var flag int = 0
for hash := range hashMap {
if hashMap[hash] > 0 {
ip = hashValue[hashMap[hash]]
return ip
}
flag++
}
/**
若找不到离最近的环则返回第一个环
*/
if flag == len(hashMap) {
ip = clusterHash[sortHash[0]]
}
return ip
}

View File

@ -0,0 +1,12 @@
package hashring
import (
"fmt"
"testing"
)
func TestTarget_GetAddrString(t *testing.T) {
var addrss = []string{"195","444"}
target := addAddrString(addrss)
fmt.Println(target)
}

52
pkg/hash-ring/load.go Normal file
View File

@ -0,0 +1,52 @@
package hashring
type Target struct {
address []string
}
func (t *Target) GetAddrString() []string {
// TODO 获取负载机器地址
return t.address
}
func addAddrString(addrss []string) *Target {
return &Target{
address: addrss,
}
}
/**
构建hash环
*/
func createClusterHashRing() map[int]string {
var servers = [...]string{"192.168.0.0:111", "192.168.0.1:111",
"192.168.0.2:111", "192.168.0.3:111", "192.168.0.4:111"}
var clusterHash map[int]string
clusterHash = make(map[int]string)
for i := 0; i < len(servers); i++ {
var hash = int(getHash(servers[i]))
clusterHash[hash] = servers[i]
}
return clusterHash
}
/**
使用FNV1_32_HASH算法计算服务器的Hash值
*/
func getHash(key string) int32 {
var p int32 = 16777619
var hash int32 = -2128831035
for i := 0; i < len(key); i++ {
hash = (hash ^ int32(key[i])) * p
}
hash += hash << 13
hash ^= hash >> 7
hash += hash << 3
hash ^= hash >> 17
hash += hash << 5
if hash < 0 {
return -hash
} else {
return hash
}
}