Sharding-JDBC分库不分表、分库分表,主从分库分表
分库不分表、分库分表,主从分库分表
分库不分表
server:
port: 8800
mybatis:
configuration:
map-underscore-to-camel-case: true
use-generated-keys: true
spring:
shardingsphere:
datasource:
names: ds0,ds1
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds0?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds1?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
sharding:
default-database-strategy:
inline:
sharding-column: order_id
algorithm-expression: ds$->{ order_id % 2}
tables:
t_order:
actual-data-nodes: ds$->{ 0..1}.t_order
key-generator:
column: order_id
type: SNOWFLAKE
props:
worker:
id: 123
props:
sql:
show: true # 打印sql
分库分表
server:
port: 8800
mybatis:
configuration:
map-underscore-to-camel-case: true
use-generated-keys: true
spring:
shardingsphere:
datasource:
names: ds0,ds1
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds0?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds1?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
sharding:
default-database-strategy:
inline:
sharding-column: order_id
algorithm-expression: ds${ order_id % 2}
tables:
t_order:
actual-data-nodes: ds${ 0..1}.t_order_${ 0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_${ (order_id % 5) % 2}
key-generator:
column: order_id
type: SNOWFLAKE
props:
worker:
id: 123
props:
sql:
show: true # 打印sql
分表的策略不能跟分库一样,比如都用order_id取模的话,那么就会出现每个数据库中都有一半的数据表没有数据,所以这里分表采取了t_order_${(order_id % 5) % 2}
这种先对一个基数取模来解决这个问题。
主从分库分表
server:
port: 8800
mybatis:
configuration:
map-underscore-to-camel-case: true
use-generated-keys: true
spring:
shardingsphere:
datasource:
names: ds0master,ds0slave,ds1master,ds1slave
ds0master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds0master?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
ds0slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds0slave?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
ds1master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds1master?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
ds1slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds1slave?useUnicode=true&allowMultiQueries=true&characterEncoding=UTF-8&useFastDateParsing=false&zeroDateTimeBehavior=convertToNull
username: xxx
password: xxx
sharding:
default-database-strategy:
inline:
sharding-column: order_id
algorithm-expression: ds${ order_id % 2}
tables:
t_order:
actual-data-nodes: ds${ 0..1}.t_order_${ 0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_${ (order_id % 5) % 2}
key-generator:
column: order_id
type: SNOWFLAKE
props:
worker:
id: 123
master-slave-rules:
ds0:
master-data-source-name: ds0master
slave-data-source-names: ds0slave
ds1:
master-data-source-name: ds1master
slave-data-source-names: ds1slave
props:
sql:
show: true # 打印sql
还没有评论,来说两句吧...