ruoyi框架中使用Sharding分表怎么切换数据源
在Ruoyi框架中使用Sharding分表,可以通过配置动态数据源来实现切换数据源。
具体步骤如下:
-
在application.yml文件中配置多个数据源,包括主数据源和分表数据源。
-
在DynamicDataSourceConfig配置类中,根据需要动态切换数据源,可以通过AOP拦截数据源注解来实现。
-
在ShardingSphereConfig配置类中,配置ShardingSphere的数据源、分片规则等信息。
-
在需要切换数据源的地方,使用@DS注解指定需要使用的数据源。
示例代码:
- application.yml配置文件
spring:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/master?serverTimezone=UTC
username: root
password: root
slave0:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/slave0?serverTimezone=UTC
username: root
password: root
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/slave1?serverTimezone=UTC
username: root
password: root
sharding:
datasource:
names: master,slave0,slave1
master:
jdbc-url: jdbc:mysql://localhost:3306/master?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
slave0:
jdbc-url: jdbc:mysql://localhost:3306/slave0?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
slave1:
jdbc-url: jdbc:mysql://localhost:3306/slave1?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
sharding:
tables:
order:
actual-data-nodes: master.order_${0..1},slave0.order_${0..1},slave1.order_${0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: order_${order_id % 2}
- DynamicDataSourceConfig配置类
@Configuration
public class DynamicDataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.slave0")
public DataSource slave0DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.slave1")
public DataSource slave1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slave0DataSource") DataSource slave0DataSource,
@Qualifier("slave1DataSource") DataSource slave1DataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
targetDataSources.put(DataSourceType.SLAVE0.name(), slave0DataSource);
targetDataSources.put(DataSourceType.SLAVE1.name(), slave1DataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
@Bean
public DataSourceTransactionManager transactionManager(DynamicDataSource dynamicDataSource) {
return new DataSourceTransactionManager(dynamicDataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dynamicDataSource);
return sessionFactory.getObject();
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.example.demo.dao");
scannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return scannerConfigurer;
}
@Bean
public DynamicDataSourceAspect dynamicDataSourceAspect() {
return new DynamicDataSourceAspect();
}
}
- ShardingSphereConfig配置类
@Configuration
public class ShardingSphereConfig {
@Autowired
private DataSource dataSource;
@Bean
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
@Bean
public DataSource dataSource() {
return ShardingDataSourceFactory.createDataSource(createShardingSphereDataSource(), createShardingSphereProperties());
}
@Bean
public ShardingRuleConfiguration shardingRuleConfiguration() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
return shardingRuleConfig;
}
private TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("order", "master.order_${0..1},slave0.order_${0..1},slave1.order_${0..1}");
result.setKeyGeneratorColumnName("order_id");
result.setKeyGenerator(keyGenerator());
return result;
}
private Properties createShardingSphereProperties() {
Properties properties = new Properties();
properties.setProperty(ShardingPropertiesConstant.SQL_SHOW.getKey(), Boolean.TRUE.toString());
return properties;
}
private Map<String, DataSource> createShardingSphereDataSource() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("master", DataSourceType.MASTER.getDataSource());
dataSourceMap.put("slave0", DataSourceType.SLAVE0.getDataSource());
dataSourceMap.put("slave1", DataSourceType.SLAVE1.getDataSource());
return dataSourceMap;
}
}
- 在需要切换数据源的地方使用@DS注解
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@DS(DataSourceType.MASTER)
@Override
public int insertOrder(Order order) {
return orderMapper.insertOrder(order);
}
@DS(DataSourceType.SLAVE0)
@Override
public List<Order> selectOrderByUserId(long userId) {
return orderMapper.selectOrderByUserId(userId);
}
@DS(DataSourceType.SLAVE1)
@Override
public List<Order> selectAllOrders() {
return orderMapper.selectAllOrders();
}
}
``
原文地址: http://www.cveoy.top/t/topic/fhyh 著作权归作者所有。请勿转载和采集!