一、前言
1、版本
apache-shardingsphere-5.0.0-shardingsphere-proxy-bin
二、快速开始
1、配置文件
分表需要配置两个文件(server.yaml、config-sharding.yaml)
server.yaml
rules:
- !AUTHORITY
users:
- root@%:root
- sharding@:sharding
provider:
type: ALL_PRIVILEGES_PERMITTED
- !TRANSACTION
defaultType: XA
providerType: Atomikos
props:
max-connections-size-per-query: 1
kernel-executor-size: 16 # Infinite by default.
proxy-frontend-flush-threshold: 128 # The default value is 128.
proxy-opentracing-enabled: false
proxy-hint-enabled: false
sql-show: false
check-table-metadata-enabled: false
show-process-list-enabled: false
# Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
# The default value is -1, which means set the minimum value for different JDBC drivers.
proxy-backend-query-fetch-size: -1
check-duplicate-table-enabled: false
sql-comment-parse-enabled: false
proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
# Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
# if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
proxy-backend-executor-suitable: OLAP
proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
sql-federation-enabled: false
config-sharding.yaml
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://localhost:3306/smiler_user?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: 123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !SHARDING
tables:
smiler_user:
actualDataNodes: ds_0.smiler_user_$->{1..2}
tableStrategy:
standard:
shardingColumn: id
shardingAlgorithmName: smiler-user-inline
shardingAlgorithms:
smiler-user-inline:
type: INLINE
props:
algorithm-expression: smiler_user_${id % 2 + 1}
2、启动服务
执行如下命令,则可以启动
./start.sh
启动成功后看下输出的日志(在 logs 文件夹中)
端口默认是 3307
3、java 连接 proxy
https://github.com/supkingx/shardingjdbcTest/blob/master/src/main/resources/application.properties
4、客户端连接 proxy
这里不需要使用实际表(smiler_user_1 或者 smiler_user_2),而是可以使用 smiler_user,proxy会自动进行路由分片