Implementing Consistent Hashing in Kotlin
Java Kotlin实现的一致性哈希工具
val a = HostPortPhysicalNode("A", "192.169.1.1", 8080)
val b = HostPortPhysicalNode("B", "192.169.1.2", 8080)
val c = HostPortPhysicalNode("C", "192.168.1.13", 8888)
val consistentHash = ConsistentHashHelper.create<HostPortPhysicalNode>()
.withNodes(listOf(a, b))
.build()
consistantHash.getNode("manerfan")
consistantHash.getNode("linxi")
consistentHash.add(c)
consistantHash.getNode("linxi")
consistantHash.remove(a.hashKey())
consistantHash.getNode("manerfan")
实现PhysicalNode
接口及hashKey
方法,ConsistantHash将通过hashKey的返回计算节点的哈希值
实现HashFunc
接口及hash
方法
使用ConsistentHashHelper
快速构造ConsistentHash
withHash(Hash)
方法用于指定自定义哈希函数
withNodes(Collection<PhysicalNode>[, ReplicasNum])
用于在构造ConsistentHash
时初始化节点,同时可指定每个节点的副本个数
val a = MyPhysicalNode("192.169.1.1", 8080)
val b = MyPhysicalNode("192.169.1.2", 8080)
val consistentHash = ConsistentHashHelper.create<MyPhysicalNode>()
.withHash(customHash)
.withNodes(listOf(a, b))
.build()
add(PhysicalNode[, ReplicasNum])
动态增加节点,同时可指定副本个数
remove(key)
动态删除节点
getNode(key)
计算该key应该落到的物理节点