Skip to content

Commit b12b167

Browse files
wbtlbwangbo
andauthored
feat(docs): update v0.3.2 docs (#385)
* feat(doc): update database sharding docs Signed-off-by: wangbo <wangbo@sphere-ex.com> * delete(charts): delete unuse file Signed-off-by: wangbo <wangbo@sphere-ex.com> * chore(docs): update readme Signed-off-by: wangbo <wangbo@sphere-ex.com> * feat(docs): update docs Signed-off-by: wangbo <wangbo@sphere-ex.com> Signed-off-by: wangbo <wangbo@sphere-ex.com> Co-authored-by: wangbo <wangbo@sphere-ex.com>
1 parent 6fab062 commit b12b167

27 files changed

+2610
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Pisanix now supports `TrafficStrategy` of [Database Mesh Specificiation](https:/
3838
- [x] Sharding with keys
3939
- [x] Single Database Sharding Tables
4040
- [x] Sharding Databases
41-
- [ ] Sharding Databases with Sharding Tables
41+
- [x] Sharding Databases with Sharding Tables
4242
- AuditRequest
4343
- [ ] Audit with AWS
4444
- AccessControl

docs/docs/Features/sharding.md

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ SELECT AVG(price) FROM t_order WHERE user_id = 1;
7272
```
7373
SELECT COUNT(price) AS AVG_DERIVED_COUNT_00000, SUM(price) AS AVG_DERIVED_SUM_00000 FROM t_order_00000 WHERE user_id = 1;
7474
```
75+
7576
# 支持特性
7677
#### 特性
7778
- 基于单 shard-key 的静态分片规则
7879
- 分片算法:crc32mod 和 mod
7980
- 单库水平分表
8081
- 分库
82+
- 基于分片键的分库分表
8183
- 基于分片的查询,更新,删除,修改
8284

8385
#### 使用限制
@@ -87,7 +89,10 @@ SELECT COUNT(price) AS AVG_DERIVED_COUNT_00000, SUM(price) AS AVG_DERIVED_SUM_00
8789
- 不支持基于表达式配置的分片规则
8890
- 不支持跨库 join 查询
8991

92+
9093
# 配置说明
94+
1. 配置字段
95+
9196
| 属性 | CRD 字段 | 值类型 | 是否依赖 | 默认值 | 含义 |
9297
|-----|----------|-------|---------|-------|-----|
9398
| table_name |rules.tableName|String|||分片表名|
@@ -100,9 +105,16 @@ SELECT COUNT(price) AS AVG_DERIVED_COUNT_00000, SUM(price) AS AVG_DERIVED_SUM_00
100105
|database_sharding_algorithm_name|rules.databaseStrategy.databaseShardingAlgorithmName|enum|||分片算法|
101106
|database_sharding_column|rules.databaseStrategy.databaseShardingColumn|String|||分片键|
102107

108+
2. 配置说明
109+
110+
- 在分库场景中,分片由 Pisa-Proxy 根据所配置 `actual_datanodes` 参数自动计算分片路由,分片数为 `actual_datanodes` 节点个数。
111+
- 在分表中,分片数由 `sharding_count` 参数决定,表示在单库中对应表的数量,Pisa-Proxy 会根据分片算法和指定的分片键计算出数据最终路由到哪个分片中。
112+
- 在分库分表中,需要分别配置分库策略和分表策略的分片键、分片算法。`sharding_count` 在此场景中对应在每个库中都需要创建表的数量。
113+
103114
## CRD 配置示例
104115

105-
1. 单库分表场景
116+
1. DataShard 配置示例
117+
- 单库分表 DataShard 配置
106118
```yaml
107119
apiVersion: core.database-mesh.io/v1alpha1
108120
kind: DataShard
@@ -124,7 +136,7 @@ spec:
124136
- value: "ds001"
125137
```
126138
127-
2. 分库场景
139+
- 分库 DataShard 配置
128140
```yaml
129141
apiVersion: core.database-mesh.io/v1alpha1
130142
kind: DataShard
@@ -144,3 +156,134 @@ spec:
144156
nodes:
145157
- value: "ds001"
146158
```
159+
160+
- 分库分表 DataShard 配置
161+
```yaml
162+
apiVersion: core.database-mesh.io/v1alpha1
163+
kind: DataShard
164+
metadata:
165+
name: catalogue
166+
namespace: demotest
167+
labels:
168+
source: catalogue
169+
spec:
170+
rules:
171+
- tableName: "t_order"
172+
databaseTableStrategy:
173+
databaseShardingAlgorithmName: mod
174+
databaseShardingColumn: t_order_id
175+
shardingCount: 4
176+
tableShardingAlgorithmName: t_order_item_id
177+
tableShardingColumn: u
178+
actualDatanodes:
179+
valueSource:
180+
nodes:
181+
- value: "ds001"
182+
- value: "ds002"
183+
184+
```
185+
186+
2. 以分库分表为例,完整配置示例如下
187+
- VirtualDatabase
188+
```yaml
189+
apiVersion: core.database-mesh.io/v1alpha1
190+
kind: VirtualDatabase
191+
metadata:
192+
name: catalogue
193+
namespace: demotest
194+
spec:
195+
services:
196+
- name: "mysql"
197+
databaseMySQL:
198+
host: "127.0.0.1"
199+
port: 3306
200+
user: "catalogue_user"
201+
password: "default_password"
202+
db: "socksdb"
203+
trafficStrategy: "catalogue"
204+
```
205+
- DatabaseEndpoint
206+
207+
database 001
208+
209+
```yaml
210+
apiVersion: core.database-mesh.io/v1alpha1
211+
kind: DatabaseEndpoint
212+
metadata:
213+
name: catalogue-db001
214+
namespace: demotest
215+
labels:
216+
source: catalogue
217+
spec:
218+
database:
219+
MySQL:
220+
host: "catalogue-db.demotest"
221+
port: 3306
222+
user: "root"
223+
password: "fake_password"
224+
db: "socksdb"
225+
```
226+
227+
database002
228+
229+
```yaml
230+
apiVersion: core.database-mesh.io/v1alpha1
231+
kind: DatabaseEndpoint
232+
metadata:
233+
name: catalogue-db002
234+
namespace: demotest
235+
labels:
236+
source: catalogue
237+
spec:
238+
database:
239+
MySQL:
240+
host: "catalogue-db.demotest"
241+
port: 3306
242+
user: "root"
243+
password: "fake_password"
244+
db: "socksdb"
245+
```
246+
247+
- TrafficStrategy
248+
```yaml
249+
apiVersion: core.database-mesh.io/v1alpha1
250+
kind: TrafficStrategy
251+
metadata:
252+
name: catalogue
253+
namespace: demotest
254+
spec:
255+
selector:
256+
matchLabels:
257+
source: catalogue
258+
loadBalance:
259+
simpleLoadBalance:
260+
kind: "random"
261+
```
262+
- DataShardStrategy
263+
```yaml
264+
apiVersion: core.database-mesh.io/v1alpha1
265+
kind: DataShard
266+
metadata:
267+
name: test
268+
namespace: default
269+
labels:
270+
source: test
271+
spec:
272+
rules:
273+
- tableName: "t_order"
274+
databaseTableStrategy:
275+
databaseShardingAlgorithmName: mod
276+
databaseShardingColumn: t_order_id
277+
shardingCount: 4
278+
tableShardingAlgorithmName: mod
279+
tableShardingColumn: t_order_item_id
280+
actualDatanodes:
281+
valueSource:
282+
nodes:
283+
- value: "catalogue-db001"
284+
- value: "catalogue-db002"
285+
286+
```
287+
288+
289+
1. 更多完整示例参考:https://github.com/database-mesh/pisanix/tree/master/example/sharding

docs/docs/UseCases/standalone.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,103 @@ weight = 1
541541
role = "read"
542542
```
543543

544+
#### 基于分片键的分片配置,分库分表场景
545+
```
546+
# api 配置块,对应命令行参数和环境变量
547+
[admin]
548+
# api 地址
549+
host = "0.0.0.0"
550+
# api 端口
551+
port = 8082
552+
# 日志级别
553+
log_level = "INFO"
554+
555+
# pisa-proxy 代理配置块
556+
[proxy]
557+
# config a proxy
558+
[[proxy.config]]
559+
# proxy 代理地址
560+
listen_addr = "0.0.0.0:9088"
561+
# proxy 认证用户名
562+
user = "root"
563+
# proxy 认证密码
564+
password = "12345678"
565+
# proxy schema
566+
db = "test_shard"
567+
# 配置后端数据源类型
568+
backend_type = "mysql"
569+
# proxy 与后端数据库建连连接池大小,值范围:1 ~ 255, 默认值:64
570+
pool_size = 3
571+
# 服务端版本
572+
server_version = ""
573+
574+
[proxy.config.simple_loadbalance]
575+
balance_type = "random"
576+
nodes = ["ds001", "ds002"]
577+
578+
# 分片配置
579+
[[proxy.config.sharding]]
580+
# 逻辑表名
581+
table_name = "t_order"
582+
# 后端节点, 对应 mysql.node 的 name
583+
actual_datanodes = ["ds001"]
584+
# 暂不支持
585+
binding_tables = []
586+
# 暂不支持
587+
broadcast_tables = []
588+
589+
# 分库分表配置
590+
[proxy.config.sharding.database_table_strategy]
591+
# 分表算法
592+
table_sharding_algorithm_name = "mod"
593+
# 分表分片键
594+
table_sharding_column = "t_order_id"
595+
# 分片数
596+
sharding_count = 4
597+
# 分库算法
598+
database_sharding_algorithm_name = "mod"
599+
# 分库分片键
600+
database_sharding_column = "t_order_item_id"
601+
602+
# 分片配置
603+
[mysql]
604+
[[mysql.node]]
605+
# 数据源 name
606+
name = "ds001"
607+
# database name
608+
db = "test_shard"
609+
# 数据库 user
610+
user = "root"
611+
# 数据库 password
612+
password = "12345678"
613+
# 数据库地址
614+
host = "127.0.0.1"
615+
# 数据库端口
616+
port = 3306
617+
# 负载均衡节点权重
618+
weight = 1
619+
# 后端数据源角色
620+
role = "read"
621+
622+
[[mysql.node]]
623+
# 数据源 name
624+
name = "ds002"
625+
# database name
626+
db = "test_shard"
627+
# 数据库 user
628+
user = "root"
629+
# 数据库 password
630+
password = "12345678"
631+
# 数据库地址
632+
host = "127.0.0.1"
633+
# 数据库端口
634+
port = 3308
635+
# 负载均衡节点权重
636+
weight = 1
637+
# 后端数据源角色
638+
role = "read"
639+
```
640+
544641
#### 启动 Pisa-Proxy
545642

546643
这里假设配置文件存放路径为 `examples/example-config.toml`,最后使用如下命令即可完成启动。
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "特性",
3+
"position": 3
4+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# 负载均衡
6+
7+
负载均衡模块为 Pisa-Proxy 代理后端节点时,新建链接时对后端节点选取策略的实现。
8+
9+
## 已支持负载均衡策略
10+
- [x] Random
11+
- [x] RoundRobin
12+
13+
## 配置示例
14+
15+
若需要在代理中启用负载均衡,则需要配置 `simple_loadbalance` 块.
16+
```
17+
[proxy.config.simple_loadbalance]
18+
balance_type = "roundrobin"
19+
nodes = ["ds001","ds002"]
20+
```
21+
其中 balance_type: 可选值为 random,roundrobin,默认值为 random;nodes: 选取后端数据源中定义的 `name`.
22+
23+
后端数据源配置:
24+
```
25+
[[mysql.node]]
26+
name = "ds001"
27+
user = "root"
28+
password = "12345678"
29+
db = "test"
30+
host = "127.0.0.1"
31+
port = 3306
32+
33+
[[mysql.node]]
34+
name = "ds002"
35+
user = "root"
36+
password = "12345678"
37+
db = "test"
38+
host = "127.0.0.1"
39+
port = 3307
40+
```
41+
42+
## 模块设计
43+
44+
该模块定义了一个负载均衡的 Trait,封装了在 Pisa-Proxy 中对于负载均衡的构建方式。以及定义了 Random 和 RoundRobin 两种负载均衡策略所要实现的具体方法。
45+
46+
### 代码结构
47+
FILES IN THIS DIRECTORY (loadbalance/src)
48+
balance.rs - 负载均衡 Trait,定义了负载均衡方法和构建负载均衡模块
49+
random_weighted.rs - random weighted 负载均衡策略
50+
roundrobin_weighted.rs - roundrobin weighted 负载均衡策略
51+

0 commit comments

Comments
 (0)