shardingsphere 4.x(二)Sharding-Proxy使用
定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供 MySQL/PostgreSQL 版本,它可以使用任何兼容 MySQL/PostgreSQL 协议的访问客户端 (如:MySQL Command Client, MySQL Workbench, Navicat 等) 操作数据,对 DBA 更加友好。下载地址 (opens new window)
- 向应用程序完全透明,可直接当做 MySQL/PostgreSQL 使用。
- 适用于任何兼容 MySQL/PostgreSQL 协议的的客户端。
下载的文件 lib 下有几个文件不是.jar 结尾,需要改成.jar 结尾,否则会报找不到类文件
下载的 Sharding-Proxy 默认是没有 mysql 驱动的,需要自己找到版本往 lib 目录下丢
# 分表
# server.yaml 基本信息配置
authentication:
users:
# 账号 root
root:
# 密码 root
password: root
sharding:
password: sharding
# 逻辑库
authorizedSchemas: sharding_db
props:
max.connections.size.per.query: 1
acceptor.size: 16 # The default value is available processors count * 2.
executor.size: 16 # Infinite by default.
proxy.frontend.flush.threshold: 128 # The default value is 128.
# LOCAL: Proxy will run with LOCAL transaction.
# XA: Proxy will run with XA transaction.
# BASE: Proxy will run with B.A.S.E transaction.
proxy.transaction.type: LOCAL
proxy.opentracing.enabled: false
query.with.cipher.column: true
sql.show: false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# config-sharding.yaml 分表配置
# 逻辑库
schemaName: sharding_db
#数据源
dataSources:
ds_0:
url: jdbc:mysql://192.168.81.104:3306/test_jdbc?serverTimezone=GMT%2B8
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#分片规则
shardingRule:
tables:
# 逻辑表名
test_standard:
actualDataNodes: ds_0.test_standard_${1..2}
tableStrategy:
# 规则
inline:
# 注定分片的列
shardingColumn: id
# 注定分片的算法
algorithmExpression: test_standard_${id % 2 + 1}
# 主键生成策略
keyGenerator:
type: SNOWFLAKE
column: id
bindingTables:
- test_standard
defaultTableStrategy:
none:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 连接
启动后默认端口 3307,然后可以使用 mysql 连接工具进行连接
对逻辑库创建表等操作,都会映射到物理库中。
# 分库分表
修改 config-sharding.yaml
# 逻辑库
schemaName: sharding_db
#数据源
dataSources:
ds_0:
url: jdbc:mysql://192.168.81.104:3306/test_jdbc1?serverTimezone=GMT%2B8
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://192.168.81.104:3306/test_jdbc2?serverTimezone=GMT%2B8
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#分片规则
shardingRule:
tables:
# 逻辑表名
test_standard:
actualDataNodes: ds_${0..1}.test_standard_${1..2}
tableStrategy:
# 规则
inline:
# 注定分片的列
shardingColumn: id
# 注定分片的算法
algorithmExpression: test_standard_${id % 2 + 1}
# 主键生成策略
keyGenerator:
type: SNOWFLAKE
column: id
bindingTables:
- test_standard
defaultDatabaseStrategy:
# 分库策略
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
创建表只能通过在配置中,把表提前预定义好,才可以创建。否则报错 Cannot find table rule and default data source with logic table
# 读写分离
修改 config-master_slave.yaml
schemaName: master_slave_db
dataSources:
master_ds:
url: jdbc:mysql://192.168.81.104:3306/user_db?serverTimezone=UTC&useSSL=false
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_0:
url: jdbc:mysql://192.168.81.104:3306/user_db?serverTimezone=UTC&useSSL=false
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds
masterDataSourceName: master_ds
slaveDataSourceNames:
- slave_ds_0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上次更新: 4/1/2025, 5:03:02 PM