全局主从
spring:shardingsphere:datasource:ds0:type: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://localhost:3306/sharding?characterEncoding=utf-8username: rootpassword: rootds1:type: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://localhost:3306/sharding1?characterEncoding=utf-8username: rootpassword: rootnames: ds0,ds1masterslave:name: msload-balance-algorithm-type: round_robinmaster-data-source-name: ds0slave-data-source-names: ds1
注意配置全局主从后,分片规则将失效。添加、更新以及删除数据操作所使用的数据库,目前仅支持单主库。查询数据操作所使用的数据库,可支持多从库。
分片主从
spring:shardingsphere:datasource:master0:driver-class-name: com.mysql.jdbc.Driverpassword: ''type: org.apache.commons.dbcp.BasicDataSourceurl: jdbc:mysql://localhost:3306/master0username: rootmaster0slave0:driver-class-name: com.mysql.jdbc.Driverpassword: ''type: org.apache.commons.dbcp.BasicDataSourceurl: jdbc:mysql://localhost:3306/master0slave0username: rootmaster0slave1:driver-class-name: com.mysql.jdbc.Driverpassword: ''type: org.apache.commons.dbcp.BasicDataSourceurl: jdbc:mysql://localhost:3306/master0slave1username: rootmaster1:driver-class-name: com.mysql.jdbc.Driverpassword: ''type: org.apache.commons.dbcp.BasicDataSourceurl: jdbc:mysql://localhost:3306/master1username: rootmaster1slave0:driver-class-name: com.mysql.jdbc.Driverpassword: ''type: org.apache.commons.dbcp.BasicDataSourceurl: jdbc:mysql://localhost:3306/master1slave0username: rootmaster1slave1:driver-class-name: com.mysql.jdbc.Driverpassword: ''type: org.apache.commons.dbcp.BasicDataSourceurl: jdbc:mysql://localhost:3306/master1slave1username: rootnames: master0,master1,master0slave0,master0slave1,master1slave0,master1slave1sharding:binding-tables: t_order,t_order_itembroadcast-tables: t_configdefault-database-strategy:inline:algorithm-expression: master$->{user_id % 2}sharding-column: user_idmaster-slave-rules:ds0:master-data-source-name: master0slave-data-source-names: master0slave0, master0slave1ds1:master-data-source-name: master1slave-data-source-names: master1slave0, master1slave1tables:t_order:actual-data-nodes: ds$->{0..1}.t_order$->{0..1}key-generator:column: order_idtype: SNOWFLAKEtable-strategy:inline:algorithm-expression: t_order$->{order_id % 2}sharding-column: order_idt_order_item:actual-data-nodes: ds$->{0..1}.t_order_item$->{0..1}key-generator:column: order_item_idtype: SNOWFLAKEtable-strategy:inline:algorithm-expression: t_order_item$->{order_id % 2}sharding-column: order_id
