如何使用 ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, props);在springboot项目启动时候进行数据源初始化,举例
时间: 2024-03-01 21:51:51 浏览: 101
好的,我会尽力回答你的问题。
首先,需要在 pom.xml 文件中添加 sharding-jdbc-spring-boot-starter 依赖,示例代码如下:
```xml
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
```
接下来,在 application.properties 文件中配置数据源和分片规则,示例代码如下:
```properties
# 配置主数据源
spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/db_master?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.master.username=root
spring.datasource.master.password=root
# 配置从数据源
spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/db_slave?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
spring.datasource.slave.username=root
spring.datasource.slave.password=root
# 配置分片规则
spring.shardingsphere.sharding.tables.user.actualDataNodes=ds${0..1}.user${0..1}
spring.shardingsphere.sharding.tables.user.tableStrategy.standard.shardingColumn=user_id
spring.shardingsphere.sharding.tables.user.tableStrategy.standard.preciseAlgorithmClassName=org.apache.shardingsphere.shardingalgorithm.sharding.standard.PreciseShardingAlgorithm
spring.shardingsphere.sharding.tables.user.tableStrategy.standard.rangeAlgorithmClassName=org.apache.shardingsphere.shardingalgorithm.sharding.standard.RangeShardingAlgorithm
spring.shardingsphere.sharding.tables.user.keyGeneratorColumnName=user_id
spring.shardingsphere.sharding.tables.user.keyGenerator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.user.keyGenerator.column=user_id
```
最后,在 SpringBoot 启动类中添加 ShardingDataSourceFactory.createDataSource() 方法,示例代码如下:
```java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public DataSource dataSource() throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("ds_master", DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/db_master?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8")
.username("root")
.password("root")
.build());
dataSourceMap.put("ds_slave0", DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/db_slave0?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8")
.username("root")
.password("root")
.build());
dataSourceMap.put("ds_slave1", DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/db_slave1?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8")
.username("root")
.password("root")
.build());
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getUserTableRuleConfiguration());
Properties props = new Properties();
props.setProperty("sql.show", "true");
return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, props);
}
private TableRuleConfiguration getUserTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("user");
result.setActualDataNodes("ds${0..1}.user${0..1}");
result.setKeyGeneratorColumnName("user_id");
result.setKeyGenerator(new KeyGeneratorConfiguration("SNOWFLAKE", "user_id"));
return result;
}
}
```
以上代码中,我们创建了 3 个数据源,分别对应主库和两个从库;然后定义了一个分片规则,用于对 user 表进行分片;最后,在 SpringBoot 启动时,调用 ShardingDataSourceFactory.createDataSource() 方法,创建数据源。
希望这个例子能帮助你更好地理解如何使用 ShardingSphere。
阅读全文