删除有关SHARDING的内容
This commit is contained in:
parent
e328ba61e0
commit
509e771862
8
pom.xml
8
pom.xml
@ -19,7 +19,6 @@
|
|||||||
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
|
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
|
||||||
<spring-boot.version>3.2.5</spring-boot.version>
|
<spring-boot.version>3.2.5</spring-boot.version>
|
||||||
<druid.version>1.2.21</druid.version>
|
<druid.version>1.2.21</druid.version>
|
||||||
<shardingsphere.version>4.1.1</shardingsphere.version>
|
|
||||||
<dynamic.version>3.5.2</dynamic.version>
|
<dynamic.version>3.5.2</dynamic.version>
|
||||||
<transactions.version>6.0.0</transactions.version>
|
<transactions.version>6.0.0</transactions.version>
|
||||||
<jta.version>1.1</jta.version>
|
<jta.version>1.1</jta.version>
|
||||||
@ -112,13 +111,6 @@
|
|||||||
<version>${druid.version}</version>
|
<version>${druid.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- sharding-jdbc分库分表 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.shardingsphere</groupId>
|
|
||||||
<artifactId>sharding-jdbc-core</artifactId>
|
|
||||||
<version>${shardingsphere.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- 动态数据源 -->
|
<!-- 动态数据源 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
|
@ -16,19 +16,6 @@ spring:
|
|||||||
url:
|
url:
|
||||||
username:
|
username:
|
||||||
password:
|
password:
|
||||||
sharding:
|
|
||||||
# 分库分表开关/默认关闭
|
|
||||||
enabled: false
|
|
||||||
ruoyi1:
|
|
||||||
enabled: false
|
|
||||||
url:
|
|
||||||
username:
|
|
||||||
password:
|
|
||||||
ruoyi2:
|
|
||||||
enabled: false
|
|
||||||
url:
|
|
||||||
username:
|
|
||||||
password:
|
|
||||||
|
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initialSize: 5
|
initialSize: 5
|
||||||
|
@ -15,9 +15,4 @@ public enum DataSourceType {
|
|||||||
* 从库
|
* 从库
|
||||||
*/
|
*/
|
||||||
SLAVE,
|
SLAVE,
|
||||||
|
|
||||||
/**
|
|
||||||
* 分库分表
|
|
||||||
*/
|
|
||||||
SHARDING
|
|
||||||
}
|
}
|
||||||
|
@ -34,12 +34,6 @@
|
|||||||
<artifactId>druid-spring-boot-3-starter</artifactId>
|
<artifactId>druid-spring-boot-3-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- sharding-jdbc分库分表 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.shardingsphere</groupId>
|
|
||||||
<artifactId>sharding-jdbc-core</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- atomikos分布式事务 -->
|
<!-- atomikos分布式事务 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.atomikos</groupId>
|
<groupId>com.atomikos</groupId>
|
||||||
|
@ -106,7 +106,6 @@ public class DruidConfig {
|
|||||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||||
targetDataSources.put(MASTER, masterDataSource);
|
targetDataSources.put(MASTER, masterDataSource);
|
||||||
setDataSource(targetDataSources, SLAVE, "slaveDataSource");
|
setDataSource(targetDataSources, SLAVE, "slaveDataSource");
|
||||||
setDataSource(targetDataSources, DataSourceType.SHARDING.name(), "shardingDataSource");
|
|
||||||
return new DynamicDataSource(masterDataSource, targetDataSources);
|
return new DynamicDataSource(masterDataSource, targetDataSources);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,110 +0,0 @@
|
|||||||
package com.ruoyi.framework.config;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
|
|
||||||
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
|
|
||||||
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
|
|
||||||
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
|
|
||||||
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import com.alibaba.druid.pool.DruidDataSource;
|
|
||||||
import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceBuilder;
|
|
||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
||||||
import com.ruoyi.framework.config.properties.DruidProperties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sharding 配置信息
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding", name = "enabled", havingValue = "true")
|
|
||||||
public class ShardingDataSourceConfig {
|
|
||||||
|
|
||||||
Logger logger = LoggerFactory.getLogger(ShardingDataSourceConfig.class);
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConfigurationProperties("spring.datasource.druid.sharding.ruoyi1")
|
|
||||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding.ruoyi1", name = "enabled", havingValue = "true")
|
|
||||||
public DataSource ruoyi1DataSource(DruidProperties druidProperties) {
|
|
||||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
||||||
return druidProperties.dataSource(dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConfigurationProperties("spring.datasource.druid.sharding.ruoyi2")
|
|
||||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding.ruoyi2", name = "enabled", havingValue = "true")
|
|
||||||
public DataSource ruoyi2DataSource(DruidProperties druidProperties) {
|
|
||||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
||||||
return druidProperties.dataSource(dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置数据源
|
|
||||||
*
|
|
||||||
* @param targetDataSources 备选数据源集合
|
|
||||||
* @param sourceName 数据源名称
|
|
||||||
* @param beanName bean名称
|
|
||||||
*/
|
|
||||||
public void setDataSource(Map<String, DataSource> targetDataSources, String sourceName, String beanName) {
|
|
||||||
try {
|
|
||||||
DataSource dataSource = SpringUtils.getBean(beanName);
|
|
||||||
targetDataSources.put(sourceName, dataSource);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Failed to register a sharding data source:{}", beanName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(name = "shardingDataSource")
|
|
||||||
public DataSource shardingDataSource() throws SQLException {
|
|
||||||
Map<String, DataSource> dataSourceMap = new HashMap<>();
|
|
||||||
|
|
||||||
// 添加数据源
|
|
||||||
setDataSource(dataSourceMap, "ruoyi1", "ruoyi1DataSource");
|
|
||||||
setDataSource(dataSourceMap, "ruoyi2", "ruoyi2DataSource");
|
|
||||||
|
|
||||||
// 配置分片规则
|
|
||||||
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
|
|
||||||
// 表规则配置 示例:
|
|
||||||
// 添加order1.sys_order_0,order2.sys_order_0,order1.sys_order_1,order2.sys_order_1
|
|
||||||
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(
|
|
||||||
"sys_user", "ruoyi$->{1..2}.sys_user_$->{0..1}");
|
|
||||||
// 配置分库策略
|
|
||||||
// 示例: 根据user_id分库,user_id为单数去order1库,偶数去order2库
|
|
||||||
orderTableRuleConfig.setDatabaseShardingStrategyConfig(
|
|
||||||
new InlineShardingStrategyConfiguration("user_id", "ruoyi$->{user_id % 2 + 1}"));
|
|
||||||
// 配置分表策略
|
|
||||||
// 示例: 根据sex分表,sex为偶数分到sys_order_0表,奇数分到sys_order_1表
|
|
||||||
orderTableRuleConfig.setTableShardingStrategyConfig(
|
|
||||||
new InlineShardingStrategyConfiguration("sex", "sys_user_$->{sex % 2}"));
|
|
||||||
// 分布式主键
|
|
||||||
orderTableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "user_id"));
|
|
||||||
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
|
|
||||||
|
|
||||||
// 获取数据源对象
|
|
||||||
DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig,
|
|
||||||
getProperties());
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 系统参数配置
|
|
||||||
*/
|
|
||||||
private Properties getProperties() {
|
|
||||||
Properties shardingProperties = new Properties();
|
|
||||||
shardingProperties.put("sql.show", true);
|
|
||||||
return shardingProperties;
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,16 +4,13 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
import com.ruoyi.common.annotation.DataSource;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
import com.ruoyi.common.enums.DataSourceType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户表 数据层
|
* 用户表 数据层
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@DataSource(DataSourceType.SHARDING)
|
|
||||||
public interface SysUserMapper
|
public interface SysUserMapper
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user