若依框架 Sharding-JDBC 分库分表配置教程
若依框架是一款基于 SpringBoot 的快速开发平台,支持多数据源配置和分库分表 (sharding)。以下是在若依框架中配置 sharding 数据源的步骤:
- 添加依赖
在 pom.xml 文件中添加 sharding-jdbc 和 mysql-connector-java 的依赖,如下所示:
<!-- sharding-jdbc -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>5.0.0-alpha</version>
</dependency>
<!-- mysql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
- 创建 sharding 数据源配置文件
在 resources 目录下创建一个 sharding-jdbc.yml 文件,用于配置 sharding 数据源的相关信息。
# 数据源列表
dataSources:
# 数据源1
ds0:
url: jdbc:mysql://localhost:3306/db0?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# 数据源2
ds1:
url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# 分库分表规则
shardingRule:
# 分库算法
default-database-strategy:
standard:
sharding-column: user_id
precise-algorithm-class-name: com.example.sharding.algorithm.DatabaseShardingAlgorithm
# 分表算法
default-table-strategy:
standard:
sharding-column: order_id
precise-algorithm-class-name: com.example.sharding.algorithm.TableShardingAlgorithm
# 数据源列表
data-sources:
ds0:
database-strategy:
inline:
sharding-column: user_id
algorithm-expression: ds0_${user_id % 2}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_${order_id % 2}
table-mapping: t_order_0,t_order_1
ds1:
database-strategy:
inline:
sharding-column: user_id
algorithm-expression: ds1_${user_id % 2}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: t_order_${order_id % 2}
table-mapping: t_order_0,t_order_1
上述配置文件中定义了两个数据源 (ds0 和 ds1),并配置了分库分表的相关规则。其中,DatabaseShardingAlgorithm 和 TableShardingAlgorithm 是自定义的分库分表算法类,用于根据指定的分片键 (user_id 和 order_id) 计算出对应的数据源和数据表。
- 创建数据库和数据表
在 MySQL 数据库中创建两个数据库 (db0 和 db1),以及两张数据表 (t_order_0 和 t_order_1)。其中,t_order_0 和 t_order_1 分别对应两个数据源 (ds0 和 ds1) 中的数据表。
- 在配置文件中添加 sharding 数据源
在 application.yml 文件中添加 sharding 数据源的相关配置:
# 多数据源配置
spring:
datasource:
dynamic:
# 默认数据源
primary: ds0
# 数据源列表
datasource:
# 数据源1
ds0:
url: jdbc:mysql://localhost:3306/db0?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# 数据源2
ds1:
url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# Sharding JDBC配置
sharding:
# 数据源配置文件
config-file: classpath:sharding-jdbc.yml
- 测试 sharding 数据源
在代码中通过 @Autowired 注入 DynamicDataSource,并使用 JdbcTemplate 来执行 SQL 语句。当执行的 SQL 语句中涉及到分库分表的情况时,sharding-jdbc 会根据配置的分片规则自动路由到对应的数据源和数据表。
@RestController
public class UserController {
@Autowired
private DynamicDataSource dynamicDataSource;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Long id) {
String sql = "SELECT * FROM user WHERE id=?";
return new JdbcTemplate(dynamicDataSource).queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
@GetMapping("/order/{id}")
public Order getOrderById(@PathVariable("id") Long id) {
String sql = "SELECT * FROM `order` WHERE id=?";
return new JdbcTemplate(dynamicDataSource).queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Order.class));
}
}
以上就是在若依框架中配置 sharding 数据源的详细步骤。
原文地址: https://www.cveoy.top/t/topic/n9ZH 著作权归作者所有。请勿转载和采集!