package com.cnpc.datasources.sharding;
import java.sql.SQLException;
import java.util.*;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.cnpc.datasources.DataSourceNames;
import com.cnpc.datasources.DataSourceProperties;
import com.cnpc.datasources.DynamicDataSource;
import com.cnpc.datasources.DynamicDataSourceConfig;
import lombok.SneakyThrows;
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.api.config.sharding.strategy.ShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import com.google.common.collect.Lists;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
/**
* sharding 数据源
*
* @author zhaolei
* @date 2020年12月3日
*
*/
@Configuration
@Order(3)
public class ShardingDataSourceConfig {
String dbNames = DataSourceNames.FIRST;
@Autowired
private DataSourceProperties properties;
@Autowired
private Environment env;
public static Map<String, DataSource> dataSourceMap = new HashMap<>();
@Bean
@Primary
public DynamicDataSource dataSource() {
System.out.println("多数据源初始化......");
Map<Object, Object> targetDataSources = new HashMap<>(16);
// targetDataSources.put(DataSourceNames.FIRST, ShardingDataSourceConfig.dataSourceMap.get(DataSourceNames.FIRST));
targetDataSources.put(DataSourceNames.FIRST, buildDataSource());
targetDataSources.put(DataSourceNames.SECOND, properties.getSecond());
targetDataSources.put(DataSourceNames.THIRD, properties.getThird());
targetDataSources.put(DataSourceNames.FOURTH, properties.getFourth());
targetDataSources.put(DataSourceNames.FIFTH, properties.getFifth());
return new DynamicDataSource(buildDataSource(), targetDataSources);
}
@SneakyThrows
private DataSource buildDataSource() {
dataSourceMap.put(DataSourceNames.FIRST, properties.getFirst());
// initDataSourceMap();
String[] split = dbNames.split(",");
// 具体分库分表策略,按什么规则来分
ShardingRuleConfiguration conf = new ShardingRuleConfiguration();
// table rule
TableRuleConfiguration tableRule = new TableRuleConfiguration("t_wx_push_info", split[0] + ".t_wx_push_info$->{1..8}");
// key生成规则
KeyGeneratorConfiguration keyGen = new KeyGeneratorConfiguration("PUSHINFO", "id");
tableRule.setKeyGeneratorConfig(keyGen);
// 分表策略
ShardingStrategyConfiguration tableShardingStrategyConfig = new InlineShardingStrategyConfiguration("id", "t_wx_push_info$->{id % 8 + 1}");
tableRule.setTableShardingStrategyConfig(tableShardingStrategyConfig);
// table rule
TableRuleConfiguration table2Rule = new TableRuleConfiguration("t_wx_push_info_details", split[0] + ".t_wx_push_info_details$->{1..8}");
// key生成规则
KeyGeneratorConfiguration key2Gen = new KeyGeneratorConfiguration("PUSHINFODETAIL", "id");
table2Rule.setKeyGeneratorConfig(key2Gen);
// 分表策略
ShardingStrategyConfiguration tableSharding2StrategyConfig = new InlineShardingStrategyConfiguration("info_type", "t_wx_push_info_details$->{info_type % 8 + 1}");
table2Rule.setTableShardingStrategyConfig(tableSharding2StrategyConfig);
conf.setTableRuleConfigs(Lists.newArrayList(tableRule, table2Rule));
Properties props = new Properties();
props.put("sql.show", true);
DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, conf, props);
return dataSource;
}
}