New file |
| | |
| | | package com.dsh.config.Sharding_jdbc; |
| | | |
| | | import com.alibaba.druid.pool.DruidDataSource; |
| | | import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory; |
| | | import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; |
| | | import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; |
| | | import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; |
| | | import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; |
| | | import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; |
| | | import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import javax.sql.DataSource; |
| | | import java.sql.SQLException; |
| | | import java.util.*; |
| | | |
| | | @Configuration |
| | | public class ShardingConfig { |
| | | |
| | | @Autowired |
| | | private Master0DataSource master0DataSource; |
| | | |
| | | |
| | | @Bean |
| | | public DataSource getDataSource() { |
| | | DataSource dataSource = null; |
| | | try { |
| | | Properties properties = new Properties(); |
| | | properties.setProperty("sql-show", "true"); |
| | | String databaseName = "m_0";//真实数据源名称,多个数据源用逗号区分 |
| | | dataSource = ShardingSphereDataSourceFactory.createDataSource(databaseName, createDataSourceMap(), createShardingRuleConfiguration(), properties); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return dataSource; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 配置多数据源 |
| | | * |
| | | * @return |
| | | */ |
| | | private Map<String, DataSource> createDataSourceMap() { |
| | | Map<String, DataSource> dataSourceMap = new HashMap<>(); |
| | | // 配置第 1 个数据源 |
| | | DruidDataSource dataSource1 = new DruidDataSource(); |
| | | dataSource1.setDriverClassName(master0DataSource.getDriverClassName()); |
| | | dataSource1.setUrl(master0DataSource.getUrl()); |
| | | dataSource1.setUsername(master0DataSource.getUsername()); |
| | | dataSource1.setPassword(master0DataSource.getPassword()); |
| | | dataSource1.setMaxActive(master0DataSource.getMaxActive()); |
| | | dataSource1.setMaxWait(master0DataSource.getMaxWait()); |
| | | dataSource1.setMinIdle(master0DataSource.getMinIdle()); |
| | | dataSource1.setInitialSize(master0DataSource.getInitialSize()); |
| | | dataSourceMap.put("m_0", dataSource1); |
| | | return dataSourceMap; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分片配置 |
| | | * |
| | | * @return |
| | | */ |
| | | private Collection<RuleConfiguration> createShardingRuleConfiguration() { |
| | | LinkedList<RuleConfiguration> linkedList = new LinkedList(); |
| | | |
| | | //分片规则配置 |
| | | ShardingRuleConfiguration result1 = new ShardingRuleConfiguration(); |
| | | result1.getTables().add(getWorldCupCompetitorTableRuleConfiguration()); |
| | | Properties props1 = new Properties(); |
| | | props1.setProperty("algorithm-expression", "t_world_cup_competitor$->{worldCupId % 5 + 1}"); |
| | | result1.getShardingAlgorithms().put("t_world_cup_competitor-inline", new AlgorithmConfiguration("INLINE", props1)); |
| | | result1.getKeyGenerators().put("t_world_cup_competitor-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); |
| | | |
| | | //分片规则配置 |
| | | result1.getTables().add(getWorldCupPaymentTableRuleConfiguration()); |
| | | Properties props2 = new Properties(); |
| | | props2.setProperty("algorithm-expression", "t_world_cup_payment$->{worldCupId % 5 + 1}"); |
| | | result1.getShardingAlgorithms().put("t_world_cup_payment-inline", new AlgorithmConfiguration("INLINE", props2)); |
| | | result1.getKeyGenerators().put("t_world_cup_payment-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); |
| | | |
| | | //分片规则配置 |
| | | result1.getTables().add(getWorldCupPaymentParticipantTableRuleConfiguration()); |
| | | Properties props3 = new Properties(); |
| | | props3.setProperty("algorithm-expression", "t_world_cup_payment_participant$->{worldCupId % 5 + 1}"); |
| | | result1.getShardingAlgorithms().put("t_world_cup_payment_participant-inline", new AlgorithmConfiguration("INLINE", props3)); |
| | | result1.getKeyGenerators().put("t_world_cup_payment_participant-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); |
| | | |
| | | linkedList.add(result1); |
| | | return linkedList; |
| | | } |
| | | |
| | | /** |
| | | * 分片算法配置 |
| | | * |
| | | * @return |
| | | */ |
| | | private ShardingTableRuleConfiguration getWorldCupCompetitorTableRuleConfiguration() { |
| | | ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_world_cup_competitor", "m_$->{0}.t_world_cup_competitor$->{1..5}");//50 |
| | | result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("worldCupId", "t_world_cup_competitor-inline")); |
| | | result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_world_cup_competitor-snowflake")); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分片算法配置 |
| | | * |
| | | * @return |
| | | */ |
| | | private ShardingTableRuleConfiguration getWorldCupPaymentTableRuleConfiguration() { |
| | | ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_world_cup_payment", "m_$->{0}.t_world_cup_payment$->{1..5}");//50 |
| | | result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("worldCupId", "t_world_cup_payment-inline")); |
| | | result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_world_cup_payment-snowflake")); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分片算法配置 |
| | | * |
| | | * @return |
| | | */ |
| | | private ShardingTableRuleConfiguration getWorldCupPaymentParticipantTableRuleConfiguration() { |
| | | ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_world_cup_payment_participant", "m_$->{0}.t_world_cup_payment_participant$->{1..5}");//50 |
| | | result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("worldCupId", "t_world_cup_payment_participant-inline")); |
| | | result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_world_cup_payment_participant-snowflake")); |
| | | return result; |
| | | } |
| | | } |