获取
配置
解压后,配置conf/config-*.yaml、 server.yaml文件;
- 几种config-*.yaml文件的区别:
- 一个yaml配置文件对应一个功能。可以叠加使用,要哪个功能就要哪个功能的配置文件中配置。
- 数据分片、读写分离、数据加密、影子库等功能
- config-encrypt.yaml: 数据加密
- config-replica-query.yaml: 读写分离
- config-shadow.yaml:影子库
- config-sharding.yaml: 数据分片
配置数据分片
- 配置config-sharding.yaml 场景:单数据源、 按id取模分表;
- 详细的各个配置信息或需要更详细的配置信息参考官网:https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-proxy/configuration/
######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################
#sharding逻辑库
schemaName: sharding_db
#要分片shardingTest数据库信息
dataSourceCommon:
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
dataSources:
# ds_0数据源名称及url,可配多个数据源,按数据源实现分库分表,现在只配一个
ds_0:
url: jdbc:mysql://127.0.0.1:3306/shardingTest?serverTimezone=UTC&useSSL=false
# ds_1:
# url: jdbc:mysql://127.0.0.1:3306/shardingTest?serverTimezone=UTC&useSSL=false
#分片规则
rules:
#- !SHARDING 一定要加!,ShardingRuleAlgorithmProviderConfigurationYamlSwapper以这个标识配置文件配置的类型
- !SHARDING
tables:
t_order:
#该配置可配多个数据源下的多个表, 实际数据源只配了 一个, 即一个数据源下的0123四个表;
actualDataNodes: ds_${0}.t_order_${0..3}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_inline
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
t_order_item:
actualDataNodes: ds_${0}.t_order_item_${0..3}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_item_inline
keyGenerateStrategy:
column: order_item_id
keyGeneratorName: snowflake
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_inline
defaultTableStrategy:
none:
shardingAlgorithms:
# 数据源选择规则:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${0}
# algorithm-expression: ds_${user_id % 2} 该配置按用户id取模,选择user_id % 2 的数据源; 但实际本配置只有一个数据源 ds_0
#数据库分表规则: 按id 取模, 存到对应表上
t_order_inline:
type: INLINE
props:
algorithm-expression: t_order_${order_id % 4}
t_order_item_inline:
type: INLINE
props:
algorithm-expression: t_order_item_${order_id % 4}
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 1
- 配置 server.yaml
#sharing连接的权限认证
authentication:
users:
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
proxy-hint-enabled: false
query-with-cipher-column: true
sql-show: true
check-table-metadata-enabled: false
启动
- 启动前准备:
- 数据源配置的数据库shardingTest一定要存在
- bin/start.bat; linux系统用start.sh
- 黑窗口 [INFO ] 13:38:22.733 [main] o.a.s.p.frontend.ShardingSphereProxy - ShardingSphere-Proxy start success.说明启动成功。
- 启动后的端口是 3307,使用navicat11可连接成功, 网上很多说nacavit12连接有是否问题的,没有验证。
验证
- 启动后
- 在sharding_db中执行建表t_order可以发现, shardingtest生成了t_order0~3,这个规律刚好对应config-sharding配置的分片规则。
测试语句:
CREATE TABLE `t_order` ( `order_id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `status` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, PRIMARY KEY (`order_id`) USING BTREE ); SET FOREIGN_KEY_CHECKS = 1;
- 向sharding_db中插入一条数据, 按配置的分片规则, order_id % 4 = 2,可得,这条数据应被分到shardingtest.t_order_2数据库。
insert into t_order (order_id,user_id,status) VALUES (2,10,10)