注意:shardingjdbc分库分表需要库、表提前手动创建,即只实现表的路由,不会自动创建表。
一、配置文件:
1、数据分片:
spring.shardingsphere.datasource.names= #数据源名称,多数据源以逗号分隔
spring.shardingsphere.datasource.<data-source-name>.type= #数据库连接池类名称
spring.shardingsphere.datasource.<data-source-name>.driver-class-name= #数据库驱动类名
spring.shardingsphere.datasource.<data-source-name>.url= #数据库url连接
spring.shardingsphere.datasource.<data-source-name>.username= #数据库用户名
spring.shardingsphere.datasource.<data-source-name>.password= #数据库密码
spring.shardingsphere.datasource.<data-source-name>.xxx= #数据库连接池的其它属性
spring.shardingsphere.sharding.tables.<logic-table-name>.actual-data-nodes= #由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点。用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
#分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
#用于单分片键的标准分片场景
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.standard.sharding-column= #分片列名称
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.standard.precise-algorithm-class-name= #精确分片算法类名称,用于=和IN。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.standard.range-algorithm-class-name= #范围分片算法类名称,用于BETWEEN,可选。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器
#用于多分片键的复合分片场景
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.complex.sharding-columns= #分片列名称,多个列以逗号分隔
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.complex.algorithm-class-name= #复合分片算法类名称。该类需实现ComplexKeysShardingAlgorithm接口并提供无参数的构造器
#行表达式分片策略
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.inline.sharding-column= #分片列名称
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.inline.algorithm-expression= #分片算法行表达式,需符合groovy语法
#Hint分片策略
spring.shardingsphere.sharding.tables.<logic-table-name>.database-strategy.hint.algorithm-class-name= #Hint分片算法类名称。该类需实现HintShardingAlgorithm接口并提供无参数的构造器
#分表策略,同分库策略
spring.shardingsphere.sharding.tables.<logic-table-name>.table-strategy.xxx= #省略
spring.shardingsphere.sharding.tables.<logic-table-name>.key-generator.column= #自增列名称,缺省表示不使用自增主键生成器
spring.shardingsphere.sharding.tables.<logic-table-name>.key-generator.type= #自增列值生成器类型,缺省表示使用默认自增列值生成器。可使用用户自定义的列值生成器或选择内置类型:SNOWFLAKE/UUID/LEAF_SEGMENT
spring.shardingsphere.sharding.tables.<logic-table-name>.key-generator.props.<property-name>= #属性配置, 注意:使用SNOWFLAKE算法,需要配置worker.id与max.tolerate.time.difference.milliseconds属性。若使用此算法生成值作分片值,建议配置max.vibration.offset属性
spring.shardingsphere.sharding.binding-tables[0]= #绑定表规则列表
spring.shardingsphere.sharding.binding-tables[1]= #绑定表规则列表
spring.shardingsphere.sharding.binding-tables[x]= #绑定表规则列表
spring.shardingsphere.sharding.broadcast-tables[0]= #广播表规则列表
spring.shardingsphere.sharding.broadcast-tables[1]= #广播表规则列表
spring.shardingsphere.sharding.broadcast-tables[x]= #广播表规则列表
spring.shardingsphere.sharding.default-data-source-name= #未配置分片规则的表将通过默认数据源定位
spring.shardingsphere.sharding.default-database-strategy.xxx= #默认数据库分片策略,同分库策略
spring.shardingsphere.sharding.default-table-strategy.xxx= #默认表分片策略,同分表策略
spring.shardingsphere.sharding.default-key-generator.type= #默认自增列值生成器类型,缺省将使用org.apache.shardingsphere.core.keygen.generator.impl.SnowflakeKeyGenerator。可使用用户自定义的列值生成器或选择内置类型:SNOWFLAKE/UUID/LEAF_SEGMENT
spring.shardingsphere.sharding.default-key-generator.props.<property-name>= #自增列值生成器属性配置, 比如SNOWFLAKE算法的worker.id与max.tolerate.time.difference.milliseconds
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.master-data-source-name= #详见读写分离部分
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[0]= #详见读写分离部分
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[1]= #详见读写分离部分
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[x]= #详见读写分离部分
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-class-name= #详见读写分离部分
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-type= #详见读写分离部分
spring.shardingsphere.props.sql.show= #是否开启SQL显示,默认值: false
spring.shardingsphere.props.executor.size= #工作线程数量,默认值: CPU核数
2、读写分离:
#省略数据源配置,与数据分片一致
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.master-data-source-name= #主库数据源名称
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[0]= #从库数据源名称列表
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[1]= #从库数据源名称列表
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[x]= #从库数据源名称列表
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-class-name= #从库负载均衡算法类名称。该类需实现MasterSlaveLoadBalanceAlgorithm接口且提供无参数构造器
spring.shardingsphere.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-type= #从库负载均衡算法类型,可选值:ROUND_ROBIN,RANDOM。若`load-balance-algorithm-class-name`存在则忽略该配置
spring.shardingsphere.props.sql.show= #是否开启SQL显示,默认值: false
spring.shardingsphere.props.executor.size= #工作线程数量,默认值: CPU核数
spring.shardingsphere.props.check.table.metadata.enabled= #是否在启动时检查分表元数据一致性,默认值: false
3、数据脱敏:
#省略数据源配置,与数据分片一致
spring.shardingsphere.encrypt.encryptors.<encryptor-name>.type= #加解密器类型,可自定义或选择内置类型:MD5/AES
spring.shardingsphere.encrypt.encryptors.<encryptor-name>.props.<property-name>= #属性配置, 注意:使用AES加密器,需要配置AES加密器的KEY属性:aes.key.value
spring.shardingsphere.encrypt.tables.<table-name>.columns.<logic-column-name>.plainColumn= #存储明文的字段
spring.shardingsphere.encrypt.tables.<table-name>.columns.<logic-column-name>.cipherColumn= #存储密文的字段
spring.shardingsphere.encrypt.tables.<table-name>.columns.<logic-column-name>.assistedQueryColumn= #辅助查询字段,针对ShardingQueryAssistedEncryptor类型的加解密器进行辅助查询
spring.shardingsphere.encrypt.tables.<table-name>.columns.<logic-column-name>.encryptor= #加密器名字
4、治理:
#省略数据源、数据分片、读写分离和数据脱敏配置
spring.shardingsphere.orchestration.name= #治理实例名称
spring.shardingsphere.orchestration.overwrite= #本地配置是否覆盖注册中心配置。如果可覆盖,每次启动都以本地配置为准
spring.shardingsphere.orchestration.registry.type= #配置中心类型。如:zookeeper
spring.shardingsphere.orchestration.registry.server-lists= #连接注册中心服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
spring.shardingsphere.orchestration.registry.namespace= #注册中心的命名空间
spring.shardingsphere.orchestration.registry.digest= #连接注册中心的权限令牌。缺省为不需要权限验证
spring.shardingsphere.orchestration.registry.operation-timeout-milliseconds= #操作超时的毫秒数,默认500毫秒
spring.shardingsphere.orchestration.registry.max-retries= #连接失败后的最大重试次数,默认3次
spring.shardingsphere.orchestration.registry.retry-interval-milliseconds= #重试间隔毫秒数,默认500毫秒
spring.shardingsphere.orchestration.registry.time-to-live-seconds= #临时节点存活秒数,默认60秒
spring.shardingsphere.orchestration.registry.props= #配置中心其它属性
二、配置文件注意事项:
1、默认数据库:shardingjdbc配置文件中,需要分片(分库、分表)的表自行配置,未配置的tablename都认为不需要分片,其中:
(1)同库分表:实际执行的SQL自动转换为datadource.tablename。
如:只配置了t_user表的切分
#mybatis xml目录配置
mybatis.mapper-locations=classpath*:Mapper/*Mapper.xml
# 配置Sharding-JDBC的分片策略
spring.shardingsphere.datasource.names=db1
spring.shardingsphere.datasource.db1.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db1.url=jdbc:mysql://localhost:3306/sharding-demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=wtyy
# 配置表的分布,表的策略
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=db1.t_user_$->{1..3}
# 指定user表 主键id 生成策略为 SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
# 指定分片策略 约定id值是偶数添加到user_1表,如果id是奇数添加到user_2表
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user_$->{id % 2+1}
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
操作其他表如t_test
<insert id="add" parameterType="com.demo.model.SystemConfigDTO">
insert into system_config(config_key,cocnfig_value) values (#{configKey},#{configValue})
</insert>
日志打印:
2021-12-29 15:11:14.810 INFO 2596 --- [nio-9999-exec-6] ShardingSphere-SQL : Rule Type: sharding
2021-12-29 15:11:14.810 INFO 2596 --- [nio-9999-exec-6] ShardingSphere-SQL : Logic SQL: insert into system_config(config_key,cocnfig_value) values (?,?)
2021-12-29 15:11:14.810 INFO 2596 --- [nio-9999-exec-6] ShardingSphere-SQL : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=system_config, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=system_config, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=25)], parametersIndex=2, logicSQL=insert into system_config(config_key,cocnfig_value) values (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[config_key, cocnfig_value], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@5db8a1dd, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@225e3bac])])
2021-12-29 15:11:14.811 INFO 2596 --- [nio-9999-exec-6] ShardingSphere-SQL : Actual SQL: db1 ::: insert into system_config (config_key, cocnfig_value) VALUES (?, ?) ::: [aaa, aaa值2]
(2)分库分表:因为有多个数据库,可以使用spring.shardingsphere.sharding.default-data-source-name指定默认数据库,如这里配置了t_user表的切分和system_config公共表的分布,其他表都默认在db1上:
mybatis.mapper-locations=classpath*:Mapper/*Mapper.xml
# 配置数据源,(db3以及t_user_3实际上均无数据,这里测试是否为全路由)
spring.shardingsphere.datasource.names=db1,db2,db3
#配置db1
spring.shardingsphere.datasource.db1.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db1.url=jdbc:mysql://localhost:3306/sharding_demo_1?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=wtyy
#配置db2
spring.shardingsphere.datasource.db2.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db2.url=jdbc:mysql://localhost:3306/sharding_demo_2?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db2.username=root
spring.shardingsphere.datasource.db2.password=wtyy
#配置db3
spring.shardingsphere.datasource.db3.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db3.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.db3.url=jdbc:mysql://localhost:3306/sharding_demo_3?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.db3.username=root
spring.shardingsphere.datasource.db3.password=wtyy
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
#配置t_user表的分布,配置主键id为雪花算法自增长
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=db$->{1..3}.t_user_$->{1..3}
spring.shardingsphere.sharding.tables.t_user.key-generator.column=id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
#配置t_user表的分库策略,age为分片键,为偶数在db1库,奇数在db2库
spring.shardingsphere.sharding.tables.t_user.database-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.tables.t_user.database-strategy.inline.algorithm-expression=db$->{age % 2 + 1}
#配置t_user表的分表策略,id为分片键,为偶数在t_user1表,奇数在t_user2表
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user_$->{id % 2+1}
#配置三个库均存在的公共表system_config表
spring.shardingsphere.sharding.tables.system_config.actual-data-nodes=db$->{1..3}.system_config
#配置其他都放在db1中的表
spring.shardingsphere.sharding.default-data-source-name=db1
操作其他表如 t_test
<insert id="addTest" parameterType="com.demo.model.TestDTO">
insert into t_test(test_key,test_name) values (#{configKey},#{configValue})
</insert>
后台日志打印:
2021-12-30 09:46:43.933 INFO 10512 --- [nio-7777-exec-2] ShardingSphere-SQL : Logic SQL: insert into t_test(test_key,test_name) values (?,?)
2021-12-30 09:46:43.933 INFO 10512 --- [nio-7777-exec-2] ShardingSphere-SQL : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=18)], parametersIndex=2, logicSQL=insert into t_test(test_key,test_name) values (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[test_key, test_name], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@340abceb, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@10d7f68e])])
2021-12-30 09:46:43.934 INFO 10512 --- [nio-7777-exec-2] ShardingSphere-SQL : Actual SQL: db1 ::: insert into t_test (test_key, test_name) VALUES (?, ?) ::: [aaa, bbb]
(3)读写分离:无分库分表,所有表读自动走从库,写自动走主库;
mybatis.mapper-locations=classpath*:Mapper/*Mapper.xml
spring.shardingsphere.datasource.names = master,slave1,slave2
#配置master
spring.shardingsphere.datasource.master.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/sharding_demo_master?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=wtyy
spring.shardingsphere.datasource.master.maxPoolSize = 100
spring.shardingsphere.datasource.master.minPoolSize = 5
#配置slave1
spring.shardingsphere.datasource.slave1.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave1.url=jdbc:mysql://localhost:3306/sharding_demo_slave1?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=wtyy
spring.shardingsphere.datasource.slave1.maxPoolSize = 100
spring.shardingsphere.datasource.slave1.minPoolSize = 5
#配置slave2
spring.shardingsphere.datasource.slave2.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave2.url=jdbc:mysql://localhost:3306/sharding_demo_slave2?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.slave2.username=root
spring.shardingsphere.datasource.slave2.password=wtyy
spring.shardingsphere.datasource.slave2.maxPoolSize = 100
spring.shardingsphere.datasource.slave2.minPoolSize = 5
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
#配置读写分离
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave2,slave1
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
#默认
spring.shardingsphere.sharding.default-data-source-name=master
spring.shardingsphere.masterslave.name=ms
(3)读写分离+分库分表:
mybatis.mapper-locations=classpath*:Mapper/*Mapper.xml
# 配置数据源
spring.shardingsphere.datasource.names=master0,master1,slave0,slave1,sharding_demo_user
#配置master0数据源,localhost的order_ms_2020库
spring.shardingsphere.datasource.master0.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3308/order_ms_2020?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=wtyy
spring.shardingsphere.datasource.master0.validationQuery = SELECT 1
#配置slave0数据源,32的order_ms_2020库
spring.shardingsphere.datasource.slave0.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://xxx.xx.xxx.32:23306/order_ms_2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=******
spring.shardingsphere.datasource.slave0.validationQuery = SELECT 1
#配置master1数据源,localhost的order_ms_2021库
spring.shardingsphere.datasource.master1.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3308/order_ms_2021?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.master1.username=root
spring.shardingsphere.datasource.master1.password=wtyy
spring.shardingsphere.datasource.master1.validationQuery = SELECT 1
#配置slave1数据源,32的order_ms_2021库
spring.shardingsphere.datasource.slave1.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave1.url=jdbc:mysql://xxx.xx.xxx.32:23306/order_ms_2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=******
spring.shardingsphere.datasource.slave1.validationQuery = SELECT 1
#配置sharding_demo_user数据源,localhost的sharding_demo_user库
spring.shardingsphere.datasource.sharding_demo_user.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding_demo_user.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding_demo_user.url=jdbc:mysql://localhost:3308/sharding_demo_user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.sharding_demo_user.username=root
spring.shardingsphere.datasource.sharding_demo_user.password=wtyy
spring.shardingsphere.datasource.sharding_demo_user.validationQuery = SELECT 1
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
#配置t_order库、表的分布,配置主键id为雪花算法自增长
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=order_ms_$->{2020..2021}.t_order_$->{1..12}
spring.shardingsphere.sharding.tables.t_order.key-generator.column=id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
#配置t_order表的分库策略,按年分库
spring.shardingsphere.sharding.tables.t_order.database-strategy.standard.preciseAlgorithmClassName=com.demo.config.OrderDbPreciseShardingConfig
spring.shardingsphere.sharding.tables.t_order.database-strategy.standard.rangeAlgorithmClassName=com.demo.config.OrderDbRangeShardingConfig
spring.shardingsphere.sharding.tables.t_order.database-strategy.standard.sharding-column=order_date
#配置t_order表的分表策略,按月分表
spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.preciseAlgorithmClassName=com.demo.config.OrderTablePreciseShardingConfig
spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.rangeAlgorithmClassName=com.demo.config.OrderTableRangeShardingConfig
spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.sharding-column=order_date
#配置垂直分库t_user的策略
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=sharding_demo_user.t_user
spring.shardingsphere.sharding.tables.t_user.key-generator.column=id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=id
#由于只有一张表,因此在此直接写表明,不需要像水平分多个表那样写策略
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user
#配置读写分离
spring.shardingsphere.sharding.master-slave-rules.order_ms_2020.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.order_ms_2020.slave-data-source-names=slave0
spring.shardingsphere.sharding.master-slave-rules.order_ms_2021.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.order_ms_2021.slave-data-source-names=slave1
其他没有配置并且不做切分的表,如果也不做主从分离,使用spring.shardingsphere.sharding.default-data-source-name指定一个无主从配置的表,如:
spring.shardingsphere.sharding.default-data-source-name=sharding_demo_user
多次读写,后台日志打印:
2021-12-30 14:14:59.985 INFO 11200 --- [nio-6666-exec-9] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:14:59.985 INFO 11200 --- [nio-6666-exec-9] ShardingSphere-SQL : Logic SQL: insert into t_test(test_key,test_name) values (?,?)
2021-12-30 14:14:59.985 INFO 11200 --- [nio-6666-exec-9] ShardingSphere-SQL : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=18)], parametersIndex=2, logicSQL=insert into t_test(test_key,test_name) values (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[test_key, test_name], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@46b0d81c, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@6621fdfe])])
2021-12-30 14:14:59.985 INFO 11200 --- [nio-6666-exec-9] ShardingSphere-SQL : Actual SQL: sharding_demo_user ::: insert into t_test (test_key, test_name) VALUES (?, ?) ::: [aaa, bbb]
2021-12-30 14:15:01.606 INFO 11200 --- [io-6666-exec-10] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:15:01.606 INFO 11200 --- [io-6666-exec-10] ShardingSphere-SQL : Logic SQL: select test_key configKey,test_name configValue from t_test where test_key = ?
2021-12-30 14:15:01.606 INFO 11200 --- [io-6666-exec-10] ShardingSphere-SQL : SQLStatement: SelectStatement(super=DQLStatement(super=AbstractSQLStatement(type=DQL, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0)], parametersIndex=1, logicSQL=select test_key configKey,test_name configValue from t_test where test_key = ?)), containStar=false, firstSelectItemStartIndex=7, selectListStopIndex=47, groupByLastIndex=0, items=[CommonSelectItem(expression=test_key, alias=Optional.of(configKey)), CommonSelectItem(expression=test_name, alias=Optional.of(configValue))], groupByItems=[], orderByItems=[], limit=null, subqueryStatement=null, subqueryStatements=[], subqueryConditions=[])
2021-12-30 14:15:01.606 INFO 11200 --- [io-6666-exec-10] ShardingSphere-SQL : Actual SQL: sharding_demo_user ::: select test_key configKey,test_name configValue from t_test where test_key = ? ::: [aaa]
2021-12-30 14:15:08.459 INFO 11200 --- [nio-6666-exec-1] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:15:08.459 INFO 11200 --- [nio-6666-exec-1] ShardingSphere-SQL : Logic SQL: insert into t_test(test_key,test_name) values (?,?)
2021-12-30 14:15:08.459 INFO 11200 --- [nio-6666-exec-1] ShardingSphere-SQL : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=18)], parametersIndex=2, logicSQL=insert into t_test(test_key,test_name) values (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[test_key, test_name], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@46b0d81c, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@6621fdfe])])
2021-12-30 14:15:08.459 INFO 11200 --- [nio-6666-exec-1] ShardingSphere-SQL : Actual SQL: sharding_demo_user ::: insert into t_test (test_key, test_name) VALUES (?, ?) ::: [aaa1, bbb1]
2021-12-30 14:15:10.126 INFO 11200 --- [nio-6666-exec-2] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:15:10.126 INFO 11200 --- [nio-6666-exec-2] ShardingSphere-SQL : Logic SQL: select test_key configKey,test_name configValue from t_test where test_key = ?
2021-12-30 14:15:10.126 INFO 11200 --- [nio-6666-exec-2] ShardingSphere-SQL : SQLStatement: SelectStatement(super=DQLStatement(super=AbstractSQLStatement(type=DQL, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0)], parametersIndex=1, logicSQL=select test_key configKey,test_name configValue from t_test where test_key = ?)), containStar=false, firstSelectItemStartIndex=7, selectListStopIndex=47, groupByLastIndex=0, items=[CommonSelectItem(expression=test_key, alias=Optional.of(configKey)), CommonSelectItem(expression=test_name, alias=Optional.of(configValue))], groupByItems=[], orderByItems=[], limit=null, subqueryStatement=null, subqueryStatements=[], subqueryConditions=[])
2021-12-30 14:15:10.126 INFO 11200 --- [nio-6666-exec-2] ShardingSphere-SQL : Actual SQL: sharding_demo_user ::: select test_key configKey,test_name configValue from t_test where test_key = ? ::: [aaa]
如果其他表不做切分,但是需要做主从分离,使用pring.shardingsphere.sharding.default-data-source-name指定一个有主从配置的表,如:
spring.shardingsphere.sharding.default-data-source-name=order_ms_2021
多次读写 ,后台日志打印:
2021-12-30 14:10:30.852 INFO 7360 --- [nio-6666-exec-2] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:10:30.853 INFO 7360 --- [nio-6666-exec-2] ShardingSphere-SQL : Logic SQL: insert into t_test(test_key,test_name) values (?,?)
2021-12-30 14:10:30.853 INFO 7360 --- [nio-6666-exec-2] ShardingSphere-SQL : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=18)], parametersIndex=2, logicSQL=insert into t_test(test_key,test_name) values (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[test_key, test_name], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@3218b240, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@691f8b9e])])
2021-12-30 14:10:30.853 INFO 7360 --- [nio-6666-exec-2] ShardingSphere-SQL : Actual SQL: master1 ::: insert into t_test (test_key, test_name) VALUES (?, ?) ::: [aaa, bbb]
2021-12-30 14:10:43.798 INFO 7360 --- [nio-6666-exec-3] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:10:43.798 INFO 7360 --- [nio-6666-exec-3] ShardingSphere-SQL : Logic SQL: select test_key configKey,test_name configValue from t_test where test_key = ?
2021-12-30 14:10:43.798 INFO 7360 --- [nio-6666-exec-3] ShardingSphere-SQL : SQLStatement: SelectStatement(super=DQLStatement(super=AbstractSQLStatement(type=DQL, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0)], parametersIndex=1, logicSQL=select test_key configKey,test_name configValue from t_test where test_key = ?)), containStar=false, firstSelectItemStartIndex=7, selectListStopIndex=47, groupByLastIndex=0, items=[CommonSelectItem(expression=test_key, alias=Optional.of(configKey)), CommonSelectItem(expression=test_name, alias=Optional.of(configValue))], groupByItems=[], orderByItems=[], limit=null, subqueryStatement=null, subqueryStatements=[], subqueryConditions=[])
2021-12-30 14:10:43.798 INFO 7360 --- [nio-6666-exec-3] ShardingSphere-SQL : Actual SQL: slave1 ::: select test_key configKey,test_name configValue from t_test where test_key = ? ::: [aaa]
2021-12-30 14:11:49.477 INFO 7360 --- [nio-6666-exec-6] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:11:49.478 INFO 7360 --- [nio-6666-exec-6] ShardingSphere-SQL : Logic SQL: select test_key configKey,test_name configValue from t_test where test_key = ?
2021-12-30 14:11:49.478 INFO 7360 --- [nio-6666-exec-6] ShardingSphere-SQL : SQLStatement: SelectStatement(super=DQLStatement(super=AbstractSQLStatement(type=DQL, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0)], parametersIndex=1, logicSQL=select test_key configKey,test_name configValue from t_test where test_key = ?)), containStar=false, firstSelectItemStartIndex=7, selectListStopIndex=47, groupByLastIndex=0, items=[CommonSelectItem(expression=test_key, alias=Optional.of(configKey)), CommonSelectItem(expression=test_name, alias=Optional.of(configValue))], groupByItems=[], orderByItems=[], limit=null, subqueryStatement=null, subqueryStatements=[], subqueryConditions=[])
2021-12-30 14:11:49.478 INFO 7360 --- [nio-6666-exec-6] ShardingSphere-SQL : Actual SQL: slave1 ::: select test_key configKey,test_name configValue from t_test where test_key = ? ::: [aaa]
2021-12-30 14:12:15.825 INFO 7360 --- [nio-6666-exec-7] ShardingSphere-SQL : Rule Type: sharding
2021-12-30 14:12:15.825 INFO 7360 --- [nio-6666-exec-7] ShardingSphere-SQL : Logic SQL: insert into t_test(test_key,test_name) values (?,?)
2021-12-30 14:12:15.825 INFO 7360 --- [nio-6666-exec-7] ShardingSphere-SQL : SQLStatement: InsertStatement(super=DMLStatement(super=AbstractSQLStatement(type=DML, tables=Tables(tables=[Table(name=t_test, alias=Optional.absent())]), routeConditions=Conditions(orCondition=OrCondition(andConditions=[AndCondition(conditions=[])])), encryptConditions=Conditions(orCondition=OrCondition(andConditions=[])), sqlTokens=[TableToken(tableName=t_test, quoteCharacter=NONE, schemaNameLength=0), SQLToken(startIndex=18)], parametersIndex=2, logicSQL=insert into t_test(test_key,test_name) values (?,?)), deleteStatement=false, updateTableAlias={}, updateColumnValues={}, whereStartIndex=0, whereStopIndex=0, whereParameterStartIndex=0, whereParameterEndIndex=0), columnNames=[test_key, test_name], values=[InsertValue(columnValues=[org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@3218b240, org.apache.shardingsphere.core.parse.old.parser.expression.SQLPlaceholderExpression@691f8b9e])])
2021-12-30 14:12:15.825 INFO 7360 --- [nio-6666-exec-7] ShardingSphere-SQL : Actual SQL: master1 ::: insert into t_test (test_key, test_name) VALUES (?, ?) ::: [aaa, bbb]
2、集成数据库连接池:引入shardingjdbc后,集成数据库连接池,只需要在配置文件中spring.shardingsphere.datasource后加上各连接池自己的属性名即可,不需要在其他地方申明了。如集成Druid:
spring.shardingsphere.datasource.dbname.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.dbname.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.dbname.url=url
spring.shardingsphere.datasource.dbname.username=username
spring.shardingsphere.datasource.dbname.password=password
spring.shardingsphere.datasource.dbname.validationQuery = SELECT 1
项目启动后可以看到日志
INFO 14912 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited