在工程的application中做sharding-jdbc的分库分表配置,代码如下:
sharding.jdbc.datasource.names=ds-master-0,ds-master-1,ds-master-0-slave-0,ds-master-0-slave-1,ds-master-1-slave-0,ds-master-1-slave-1
sharding.jdbc.datasource.ds-master-0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds-master-0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-master-0.url=jdbc:mysql://10.0.0.3:3306/cool?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
sharding.jdbc.datasource.ds-master-0.username=root
sharding.jdbc.datasource.ds-master-0.password=
sharding.jdbc.datasource.ds-master-0-slave-0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds-master-0-slave-0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-master-0-slave-0.url=jdbc:mysql://10.0.0.13:3306/cool?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
sharding.jdbc.datasource.ds-master-0-slave-0.username=root
sharding.jdbc.datasource.ds-master-0-slave-0.password=
sharding.jdbc.datasource.ds-master-0-slave-1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds-master-0-slave-1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-master-0-slave-1.url=jdbc:mysql://10.0.0.17:3306/cool?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
sharding.jdbc.datasource.ds-master-0-slave-1.username=root
sharding.jdbc.datasource.ds-master-0-slave-1.password=
sharding.jdbc.datasource.ds-master-1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds-master-1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-master-1.url=jdbc:mysql://10.0.0.3:3306/cool2?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
sharding.jdbc.datasource.ds-master-1.username=root
sharding.jdbc.datasource.ds-master-1.password=
sharding.jdbc.datasource.ds-master-1-slave-0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds-master-1-slave-0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-master-1-slave-0.url=jdbc:mysql://10.0.0.13:3306/cool2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
sharding.jdbc.datasource.ds-master-1-slave-0.username=root
sharding.jdbc.datasource.ds-master-1-slave-0.password=
sharding.jdbc.datasource.ds-master-1-slave-1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds-master-1-slave-1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-master-1-slave-1.url=jdbc:mysql://10.0.0.17:3306/cool2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
sharding.jdbc.datasource.ds-master-1-slave-1.username=root
sharding.jdbc.datasource.ds-master-1-slave-1.password=
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds_$->{id % 2}
sharding.jdbc.config.sharding.tables.user.actual-data-nodes=ds_$->{0..1}.user_$->{0..4}
sharding.jdbc.config.sharding.tables.user.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 5}
sharding.jdbc.config.sharding.tables.user.key-generator-column-name=id
sharding.jdbc.config.sharding.master-slave-rules.ds_0.master-data-source-name=ds-master-0
sharding.jdbc.config.sharding.master-slave-rules.ds_0.slave-data-source-names=ds-master-0-slave-0, ds-master-0-slave-1
sharding.jdbc.config.sharding.master-slave-rules.ds_1.master-data-source-name=ds-master-1
sharding.jdbc.config.sharding.master-slave-rules.ds_1.slave-data-source-names=ds-master-1-slave-0, ds-master-1-slave-1
在上面的配置中,其中sharding.jdbc.datasource部分是配置数据库的信息,配置了6个数据库。
sharding.jdbc.config.sharding.master-slave-rules.ds_0.master-data-source-name配置的是ds_0区的的主库名称,同理ds_1。
sharding.jdbc.config.sharding.master-slave-rules.ds_0.slave-data-source-names配置的是ds_0区的的从库名称,同理ds_1。
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column配置的分库的字段,本案例是根据id进行分。
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression配置的分库的逻辑,根据id%2进行分。
sharding.jdbc.config.sharding.tables.user.actual-data-nodes配置的是user表在真实数据库中的位置,ds_KaTeX parse error: Expected group after '_' at position 14: ->{0..1}.user_̲->{0…4}表示
数据在ds_0和ds_1中的user_0、user_1、user_2、user_3、user_4中。
sharding.jdbc.config.sharding.tables.user.table-strategy.inline.sharding-column,配置user表数据切分的字段
sharding.jdbc.config.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 5},配置user表数据切分的策略。
sharding.jdbc.config.sharding.tables.user.key-generator-column-name=id 自动生成id。
然后在Spring Boot启动类的注解@SpringBootApplication,加上exclude={DataSourceAutoConfiguration.class},代码如下:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableConfigurationProperties
public class ShardingJdbcDbMsTblApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingJdbcDbMsTblApplication.class, args);
}
}
标签:分库,slave,sharding,JDBC,master,datasource,Sharding,jdbc,ds
From: https://blog.51cto.com/u_16274468/7755830