Skip to content

Commit bc9c5ae

Browse files
committed
change readme.md
change readme.md
1 parent c6b5160 commit bc9c5ae

File tree

2 files changed

+111
-8
lines changed

2 files changed

+111
-8
lines changed

README-CN.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
<div align='center'>
3+
<a href="https://github.com/werbenhu/chash/actions"><img src="https://github.com/werbenhu/chash/workflows/Go/badge.svg"></a>
4+
<a href="https://coveralls.io/github/werbenhu/chash?branch=main"><img src="https://coveralls.io/repos/github/werbenhu/chash/badge.svg?branch=main"></a>
5+
<a href="https://github.com/werbenhu/chash"><img src="https://img.shields.io/github/license/mashape/apistatus.svg"></a>
6+
<a href="https://pkg.go.dev/github.com/werbenhu/chash"><img src="https://pkg.go.dev/badge/github.com/werbenhu/chash.svg"></a>
7+
</div>
8+
9+
[English](README.md) | [简体中文](README-CN.md)
10+
11+
# chash
12+
**用Go写的一致性哈希算法**
13+
14+
## 什么是一致性哈希
15+
16+
一致性哈希(Consistent Hash)是为了解决由于分布式系统中节点的增加或减少而带来的大量失效问题,它可以有效地降低这种失效影响,从而提高分布式系统的性能和可用性。
17+
18+
### 普通哈希的问题
19+
20+
普通哈希函数是 key % n,其中 n 是服务器数量。它有两个主要缺点:
21+
1. 不能水平扩展,或者换句话说,不具备分区容错性。当添加新服务器时,所有现有的映射都会被破坏。这可能会引入痛苦的维护工作和系统停机时间。
22+
2. 可能不能实现负载均衡。如果数据不是均匀分布的,这可能会导致一些服务器过热饱和,而其他服务器则处于空闲状态并几乎为空。
23+
24+
问题2可以通过先对键进行哈希,然后哈希(key) % n,以便哈希键更有可能被均匀分布来解决。但是,这不能解决问题1。我们需要找到一个可以分配key并且不依赖于n的解决方案。
25+
26+
### 一致性哈希
27+
28+
关于环,添加删除节点,还有虚拟节点等,参考:[一致性哈希的简单认识](https://baijiahao.baidu.com/s?id=1735480432495470467&wfr=spider&for=pc)
29+
30+
## 入门指南
31+
32+
使用Go模块支持,只需添加以下导入即可
33+
34+
`import "github.com/werbenhu/chash"`
35+
36+
37+
## 简单用法
38+
39+
### 创建一个组
40+
```
41+
// 创建db组,通过chash.CreateGroup()创建组,
42+
// 内部会有一个全局的单例chash对象管理组
43+
dbGroup, _ := chash.CreateGroup("db", 10000)
44+
45+
// 插入元素(多个myqsl服务器)
46+
dbGroup.Insert("192.168.1.100:3306", []byte("mysql0-info"))
47+
dbGroup.Insert("192.168.1.101:3306", []byte("mysql1-info"))
48+
49+
// 创建第二个组
50+
webGroup, _ := chash.CreateGroup("web", 10000)
51+
52+
// 插入元素(多个http服务器)
53+
webGroup.Insert("192.168.2.100:80", []byte("web0-info"))
54+
webGroup.Insert("192.168.2.101:80", []byte("web1-info"))
55+
```
56+
57+
```
58+
// 使用已经存在的组
59+
dbGroup, err := chash.GetGroup("db")
60+
if err != nil {
61+
log.Printf("ERROR get group failed, err:%s\n", err.Error())
62+
}
63+
64+
dbGroup.Insert("192.168.1.103:3306", []byte("mysql3-info"))
65+
host, info, err := dbGroup.Match("user-id")
66+
```
67+
68+
### 给某个用户ID,匹配一个mysql服务器
69+
```
70+
// 匹配哈希到环上的键的附近元素。
71+
host, info, err := dbGroup.Match("user-id")
72+
```
73+
74+
### 从组中删除元素
75+
```
76+
// 删除元素
77+
dbGroup.Delete("192.168.1.102:3306")
78+
```
79+
80+
### 获取组的所有元素
81+
```
82+
elements := dbGroup.GetElemens()
83+
```
84+
85+
### 也可以使用一个独立组
86+
```
87+
// chash.NewGrou()创建的组,不被全局的chash对象所管理
88+
// 如果有多个组,您需要自己管理组。
89+
group := chash.NewGroup("db", 10000)
90+
91+
group.Insert("192.168.1.100:3306", []byte("mysql0-info"))
92+
group.Insert("192.168.1.101:3306", []byte("mysql1-info"))
93+
host, info, err := group.Match("user-id")
94+
```
95+
96+
## 示例
97+
请参见 [example](example/main.go) .
98+
99+
## 贡献
100+
欢迎贡献和反馈!提出问题或提出功能请求请提 [issue](https://github.com/werbenhu/chash/issues) .

README.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<a href="https://pkg.go.dev/github.com/werbenhu/chash"><img src="https://pkg.go.dev/badge/github.com/werbenhu/chash.svg"></a>
77
</div>
88

9+
[English](README.md) | [简体中文](README-CN.md)
910
# chash
1011
**Consistent hashing written by Go**
1112

@@ -43,23 +44,24 @@ With Go module support, simply add the following import
4344

4445
### Create a group
4546
```
46-
// create db group
47+
// Create a "db" group using chash.CreateGroup(),
48+
// which internally manages the group using a global singleton chash object.
4749
dbGroup, _ := chash.CreateGroup("db", 10000)
4850
49-
// insert elements
51+
// Insert elements (multiple MySQL servers).
5052
dbGroup.Insert("192.168.1.100:3306", []byte("mysql0-info"))
5153
dbGroup.Insert("192.168.1.101:3306", []byte("mysql1-info"))
5254
53-
// create second group
55+
// Create a second group.
5456
webGroup, _ := chash.CreateGroup("web", 10000)
5557
56-
// insert elements
58+
// Insert elements (multiple HTTP servers).
5759
webGroup.Insert("192.168.2.100:80", []byte("web0-info"))
5860
webGroup.Insert("192.168.2.101:80", []byte("web1-info"))
5961
```
6062

6163
```
62-
// use existed group
64+
// Use an existing group.
6365
dbGroup, err := chash.GetGroup("db")
6466
if err != nil {
6567
log.Printf("ERROR get group failed, err:%s\n", err.Error())
@@ -69,7 +71,7 @@ dbGroup.Insert("192.168.1.103:3306", []byte("mysql3-info"))
6971
host, info, err := dbGroup.Match("user-id")
7072
```
7173

72-
### Match the element for a key
74+
### Match a MySQL server for a user ID
7375
```
7476
// match an element close to where key hashes to in the circle.
7577
host, info, err := dbGroup.Match("user-id")
@@ -86,9 +88,10 @@ dbGroup.Delete("192.168.1.102:3306")
8688
elements := dbGroup.GetElemens()
8789
```
8890

89-
### Single Group
91+
### Using an independent group
9092
```
91-
// you need to manager groups if there is more than one group.
93+
// A group created by chash.NewGrou() is not managed by the global chash object
94+
// You need to manage groups yourself if there are more than one group.
9295
group := chash.NewGroup("db", 10000)
9396
9497
group.Insert("192.168.1.100:3306", []byte("mysql0-info"))

0 commit comments

Comments
 (0)