1.增加pom坐标
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2.增加配置文件
#数据源
spring.shardingsphere.datasource.names=master0,slave0
#配置主数据库数据源
spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://192.168.101.24:3306/anyouliang_exchange
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=123456
#配置从数据库数据源
spring.shardingsphere.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://192.168.101.50:3306/anyouliang_exchange
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=123456
#配置主从逻辑数据源定义 ds0为 anyouliang_exchange
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave0
#配置指定表的数据节点
spring.shardingsphere.sharding.tables.vouchers_of_sale.actual-data-nodes=ds0.vouchers_of_sale_$->{1..2}
#主键生成策略
spring.shardingsphere.sharding.tables.vouchers_of_sale.key-generator.column=sale_id
spring.shardingsphere.sharding.tables.vouchers_of_sale.key-generator.type=SNOWFLAKE
#指定vouchers_of_sale表的分片策略,包括分片建表和分片算法
spring.shardingsphere.sharding.tables.vouchers_of_sale.table-strategy.inline.sharding-column=sale_id
spring.shardingsphere.sharding.tables.vouchers_of_sale.table-strategy.inline.algorithm-expression=vouchers_of_sale_$->{sale_id % 2+1}
spring.shardingsphere.props.sql.show = true
大功告成。项目中有其他数据源删除就好了。 其他正常运行