|
|
@@ -0,0 +1,331 @@
|
|
|
+package com.txz.mall.configurer;
|
|
|
+
|
|
|
+import com.zaxxer.hikari.HikariConfig;
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
+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.ComplexShardingStrategyConfiguration;
|
|
|
+import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
|
|
|
+import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
+import org.springframework.context.annotation.*;
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class ShardingJdbcConfig {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置数据源
|
|
|
+ */
|
|
|
+// @Bean("shardingDataSource")
|
|
|
+// @Primary
|
|
|
+// public DataSource dataSource() throws SQLException {
|
|
|
+// // 配置分片规则
|
|
|
+// ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
|
|
|
+//
|
|
|
+// // 配置m_store_order表的分片规则
|
|
|
+// TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("m_store_order",
|
|
|
+// "mall_${0..1}.m_store_order_${0..1}");
|
|
|
+//
|
|
|
+// // 设置分库策略
|
|
|
+// orderTableRuleConfig.setDatabaseShardingStrategyConfig(
|
|
|
+// new StandardShardingStrategyConfiguration("uid", new UidModuloDatabaseShardingAlgorithm()));
|
|
|
+//
|
|
|
+// // 设置分表策略
|
|
|
+// orderTableRuleConfig.setTableShardingStrategyConfig(
|
|
|
+// new StandardShardingStrategyConfiguration("uid", new UidModuloTableShardingAlgorithm()));
|
|
|
+//
|
|
|
+// shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
|
|
|
+//
|
|
|
+// // 获取数据源映射
|
|
|
+// Map<String, DataSource> dataSources = createDataSources();
|
|
|
+//
|
|
|
+// // 创建并返回分片数据源
|
|
|
+// return ShardingDataSourceFactory.createDataSource(dataSources, shardingRuleConfig, getProperties());
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 创建数据源映射
|
|
|
+// */
|
|
|
+// private Map<String, DataSource> createDataSources() {
|
|
|
+// Map<String, DataSource> dataSources = new HashMap<>(2);
|
|
|
+//
|
|
|
+// // 配置第一个数据库
|
|
|
+// dataSources.put("mall_0", createHikariDataSource(
|
|
|
+// "jdbc:mysql://124.222.152.234:3306/mall_0?useUnicode=true&characterEncoding=utf-8&useSSL=false",
|
|
|
+// "root",
|
|
|
+// "hy123456"
|
|
|
+// ));
|
|
|
+//
|
|
|
+// // 配置第二个数据库
|
|
|
+// dataSources.put("mall_1", createHikariDataSource(
|
|
|
+// "jdbc:mysql://124.222.152.234:3306/mall_1?useUnicode=true&characterEncoding=utf-8&useSSL=false",
|
|
|
+// "root",
|
|
|
+// "hy123456"
|
|
|
+// ));
|
|
|
+//
|
|
|
+// return dataSources;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 创建Hikari数据源
|
|
|
+// */
|
|
|
+// private DataSource createHikariDataSource(String jdbcUrl, String username, String password) {
|
|
|
+// HikariConfig config = new HikariConfig();
|
|
|
+// config.setJdbcUrl(jdbcUrl);
|
|
|
+// config.setUsername(username);
|
|
|
+// config.setPassword(password);
|
|
|
+// config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
|
|
+// // 可以根据需要配置其他Hikari参数
|
|
|
+// config.setMaximumPoolSize(10);
|
|
|
+// config.setMinimumIdle(5);
|
|
|
+// return new HikariDataSource(config);
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 设置Sharding-JDBC的其他属性
|
|
|
+// */
|
|
|
+// private Properties getProperties() {
|
|
|
+// Properties properties = new Properties();
|
|
|
+// // 打印SQL,方便调试
|
|
|
+// properties.setProperty("sql.show", "true");
|
|
|
+// // 执行线程池大小
|
|
|
+// properties.setProperty("executor.size", "10");
|
|
|
+// return properties;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置数据源
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public DataSource dataSource() throws SQLException {
|
|
|
+
|
|
|
+ // 1. 配置多个数据源
|
|
|
+ Map<String, DataSource> dataSources = new HashMap<>();
|
|
|
+ dataSources.put("ds0", createDataSource("jdbc:mysql://124.222.152.234:3306/mall_0?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false", "root", "hy123456","ds0"));
|
|
|
+ dataSources.put("ds1", createDataSource("jdbc:mysql://124.222.152.234:3306/mall_1?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false", "root", "hy123456","ds1"));
|
|
|
+ dataSources.put("ds2", createDataSource("jdbc:mysql://124.222.152.234:3306/mall_2?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false", "root", "hy123456","ds2"));
|
|
|
+ dataSources.put("ds3", createDataSource("jdbc:mysql://124.222.152.234:3306/mall_3?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false", "root", "hy123456","ds3"));
|
|
|
+ // 添加默认数据库(非分库分表的表使用)
|
|
|
+ dataSources.put("default_ds", createDataSource("jdbc:mysql://124.222.152.234:3306/mall?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false", "root", "hy123456","ds4"));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 4. 配置分表策略(用户ID取模)
|
|
|
+// orderTableRule.setTableShardingStrategyConfig(
|
|
|
+// new StandardShardingStrategyConfiguration("uid,order_id", )
|
|
|
+// );
|
|
|
+
|
|
|
+ // 5. 配置分片规则
|
|
|
+ ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
|
|
|
+ Collection<TableRuleConfiguration> tableRuleConfigs = shardingRuleConfig.getTableRuleConfigs();
|
|
|
+
|
|
|
+ TableRuleConfiguration storeOrderTableRule = storeOrderTableRuleConfiguration();
|
|
|
+ tableRuleConfigs.add(storeOrderTableRule);
|
|
|
+
|
|
|
+
|
|
|
+ TableRuleConfiguration storePinkTableRuleConfiguration = storePinkTableRuleConfiguration();
|
|
|
+ tableRuleConfigs.add(storePinkTableRuleConfiguration);
|
|
|
+
|
|
|
+
|
|
|
+ TableRuleConfiguration storeOrderInfoTableRuleConfiguration = storeOrderInfoTableRuleConfiguration();
|
|
|
+ tableRuleConfigs.add(storeOrderInfoTableRuleConfiguration);
|
|
|
+
|
|
|
+
|
|
|
+ TableRuleConfiguration storeOrderStatusTableRuleConfiguration = storeOrderStatusTableRuleConfiguration();
|
|
|
+ tableRuleConfigs.add(storeOrderStatusTableRuleConfiguration);
|
|
|
+
|
|
|
+ // 这个表分起来有点麻烦,不如不分 查询sql 用的是 cid(活动商品id) 或者 create_time(定时关单) 可以采用定时清理无效数据的方式处理。一般这个数据只在活动持续时间内有效
|
|
|
+// TableRuleConfiguration storePinkSummaryTableRuleConfiguration = storePinkSummaryTableRuleConfiguration();
|
|
|
+// tableRuleConfigs.add(storePinkSummaryTableRuleConfiguration);
|
|
|
+
|
|
|
+
|
|
|
+ TableRuleConfiguration userSignTableRuleConfiguration = userSignTableRuleConfiguration();
|
|
|
+ tableRuleConfigs.add(userSignTableRuleConfiguration);
|
|
|
+
|
|
|
+ // 设置默认数据源(非分片表使用)
|
|
|
+ shardingRuleConfig.setDefaultDataSourceName("default_ds");
|
|
|
+
|
|
|
+ Properties props = new Properties();
|
|
|
+ props.setProperty("sql.show", "true");
|
|
|
+ // 允许缺少分片键的查询执行全表扫描(默认false,会报错)
|
|
|
+ props.setProperty("shardingsphere.sharding.allow.full.table.scan", "true");
|
|
|
+
|
|
|
+ // 6. 创建并返回分片数据源
|
|
|
+ return ShardingDataSourceFactory.createDataSource(dataSources, shardingRuleConfig, props);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public PlatformTransactionManager transactionManager(DataSource dataSource) {
|
|
|
+ return new DataSourceTransactionManager(dataSource);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建数据源工具方法
|
|
|
+ */
|
|
|
+ private DataSource createDataSource(String url, String username, String password,String dataSourceName) {
|
|
|
+
|
|
|
+ com.zaxxer.hikari.HikariConfig config = new com.zaxxer.hikari.HikariConfig();
|
|
|
+ config.setJdbcUrl(url);
|
|
|
+ config.setUsername(username);
|
|
|
+ config.setPassword(password);
|
|
|
+ config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
|
|
+ config.setPoolName("dataSourceName");
|
|
|
+ HikariDataSource hikariDataSource = new HikariDataSource(config);
|
|
|
+ try {
|
|
|
+ hikariDataSource.getConnection().close();
|
|
|
+ System.out.println("数据源 " + dataSourceName + " 初始化成功");
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return hikariDataSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ private TableRuleConfiguration storeOrderTableRuleConfiguration(){
|
|
|
+ // 2. 配置订单表分库分表规则
|
|
|
+ TableRuleConfiguration orderTableRule = new TableRuleConfiguration("m_store_order", "ds${0..3}.m_store_order_${0..4}");
|
|
|
+ // 3. 配置分库策略(时间+用户ID)
|
|
|
+
|
|
|
+ orderTableRule.setDatabaseShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "create_time,uid,order_id", // 多分片键
|
|
|
+ new DatabaseShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+ orderTableRule.setTableShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "uid,order_id", // 多分片键
|
|
|
+ new TableShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return orderTableRule;
|
|
|
+ }
|
|
|
+
|
|
|
+ private TableRuleConfiguration storePinkTableRuleConfiguration(){
|
|
|
+ // 2. 配置订单表分库分表规则
|
|
|
+ TableRuleConfiguration orderTableRule = new TableRuleConfiguration("m_store_pink", "ds${0..3}.m_store_pink_${0..4}");
|
|
|
+ // 3. 配置分库策略(时间+用户ID)
|
|
|
+
|
|
|
+ orderTableRule.setDatabaseShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "create_time,uid,order_id", // 多分片键
|
|
|
+ new DatabaseShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+ orderTableRule.setTableShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "uid,order_id", // 多分片键
|
|
|
+ new TableShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return orderTableRule;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private TableRuleConfiguration storeOrderInfoTableRuleConfiguration(){
|
|
|
+ // 2. 配置订单表分库分表规则
|
|
|
+ TableRuleConfiguration orderTableRule = new TableRuleConfiguration("m_store_order_info", "ds${0..3}.m_store_order_info_${0..4}");
|
|
|
+ // 3. 配置分库策略(时间+用户ID)
|
|
|
+
|
|
|
+ orderTableRule.setDatabaseShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "order_no", // 多分片键
|
|
|
+ new StoreOrderInfoDatabaseShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+ orderTableRule.setTableShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "order_no", // 多分片键
|
|
|
+ new StoreOrderInfoTableShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return orderTableRule;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private TableRuleConfiguration storeOrderStatusTableRuleConfiguration(){
|
|
|
+ // 2. 配置订单表分库分表规则
|
|
|
+ TableRuleConfiguration orderTableRule = new TableRuleConfiguration("m_store_order_status", "ds${0..3}.m_store_order_status_${0..4}");
|
|
|
+ // 3. 配置分库策略(时间+用户ID)
|
|
|
+
|
|
|
+ orderTableRule.setDatabaseShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "order_id", // 多分片键
|
|
|
+ new StoreOrderStatusDatabaseShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+ orderTableRule.setTableShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "order_id", // 多分片键
|
|
|
+ new StoreOrderStatusTableShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return orderTableRule;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// private TableRuleConfiguration storePinkSummaryTableRuleConfiguration(){
|
|
|
+// // 2. 配置订单表分库分表规则
|
|
|
+// TableRuleConfiguration orderTableRule = new TableRuleConfiguration("m_store_pink_summary", "ds${0..3}.m_store_pink_summary_${0..4}");
|
|
|
+// // 3. 配置分库策略(时间+用户ID)
|
|
|
+//
|
|
|
+// orderTableRule.setDatabaseShardingStrategyConfig(
|
|
|
+// new ComplexShardingStrategyConfiguration(
|
|
|
+// "cid", // 多分片键
|
|
|
+// new StorePinkSummaryDatabaseShardingAlgorithm() // 复合分片算法
|
|
|
+// )
|
|
|
+// );
|
|
|
+// orderTableRule.setTableShardingStrategyConfig(
|
|
|
+// new ComplexShardingStrategyConfiguration(
|
|
|
+// "cid", // 多分片键
|
|
|
+// new StorePinkSummaryTableShardingAlgorithm() // 复合分片算法
|
|
|
+// )
|
|
|
+// );
|
|
|
+//
|
|
|
+// return orderTableRule;
|
|
|
+// }
|
|
|
+
|
|
|
+ private TableRuleConfiguration userSignTableRuleConfiguration(){
|
|
|
+ // 2. 配置订单表分库分表规则
|
|
|
+ TableRuleConfiguration orderTableRule = new TableRuleConfiguration("m_user_sign", "ds${0..3}.m_user_sign_${0..4}");
|
|
|
+ // 3. 配置分库策略(时间+用户ID)
|
|
|
+
|
|
|
+ orderTableRule.setDatabaseShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "uid,create_day", // 多分片键
|
|
|
+ new UserSignDatabaseShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+ orderTableRule.setTableShardingStrategyConfig(
|
|
|
+ new ComplexShardingStrategyConfiguration(
|
|
|
+ "uid,create_day", // 多分片键
|
|
|
+ new UserSignTableShardingAlgorithm() // 复合分片算法
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return orderTableRule;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|