|
@@ -0,0 +1,161 @@
|
|
|
|
|
+package com.sckw.datasource.config;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
|
|
|
|
|
+import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
|
|
|
|
|
+import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
|
|
|
|
+import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
|
|
|
+import com.sckw.datasource.properties.ShardingJdbcProperties;
|
|
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
|
|
|
|
|
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
|
|
|
|
|
+import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
|
|
|
|
|
+import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration;
|
|
|
|
|
+import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlPersistRepositoryConfiguration;
|
|
|
|
|
+import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
|
|
|
|
|
+import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration;
|
|
|
|
|
+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.audit.ShardingAuditStrategyConfiguration;
|
|
|
|
|
+import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Properties;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @Author xucaiqin
|
|
|
|
|
+ * @date 2023-06-09 13:18:33
|
|
|
|
|
+ */
|
|
|
|
|
+@Configuration
|
|
|
|
|
+@EnableConfigurationProperties(ShardingJdbcProperties.class)
|
|
|
|
|
+public class ShardingJdbcAutoConfig {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 动态数据源提供方,添加sharding-jdbc数据源
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param dynamicDataSourceProperties
|
|
|
|
|
+ * @param shardingDataSource
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ @ConditionalOnProperty(name = "spring.sharding.enable", havingValue = "true")
|
|
|
|
|
+ public DynamicDataSourceProvider dynamicDataSourceProvider(DynamicDataSourceProperties dynamicDataSourceProperties, DataSource shardingDataSource) {
|
|
|
|
|
+ Map<String, DataSourceProperty> datasourceMap = dynamicDataSourceProperties.getDatasource();
|
|
|
|
|
+ return new AbstractDataSourceProvider() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Map<String, DataSource> loadDataSources() {
|
|
|
|
|
+ Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
|
|
|
|
|
+ dataSourceMap.put("sharding", shardingDataSource);
|
|
|
|
|
+ return dataSourceMap;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * sharding-jdbc数据源
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param shardingJdbcProperties
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws SQLException
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public DataSource shardingDataSource(ShardingJdbcProperties shardingJdbcProperties) throws SQLException {
|
|
|
|
|
+ /*构建运行模式*/
|
|
|
|
|
+ YamlModeConfiguration modeConfig = shardingJdbcProperties.getMode();
|
|
|
|
|
+ ModeConfiguration modeConfiguration;
|
|
|
|
|
+ if (StringUtils.equals(modeConfig.getType(), "Standalone")) {
|
|
|
|
|
+ YamlPersistRepositoryConfiguration repository = modeConfig.getRepository();
|
|
|
|
|
+ modeConfiguration = new ModeConfiguration(modeConfig.getType(), new StandalonePersistRepositoryConfiguration(repository.getType(), repository.getProps()));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ YamlPersistRepositoryConfiguration repository = modeConfig.getRepository();
|
|
|
|
|
+ Properties props = repository.getProps();
|
|
|
|
|
+ modeConfiguration = new ModeConfiguration(modeConfig.getType(), new ClusterPersistRepositoryConfiguration(repository.getType(), props.getProperty("namespace"), props.getProperty("server-lists"), props));
|
|
|
|
|
+ }
|
|
|
|
|
+ /*数据源*/
|
|
|
|
|
+ Map<String, DataSource> dataSourceMap = createDataSources(shardingJdbcProperties);
|
|
|
|
|
+ /*构建属性配置*/
|
|
|
|
|
+ Properties props = shardingJdbcProperties.getProps();
|
|
|
|
|
+ return ShardingSphereDataSourceFactory.createDataSource(/*shardingJdbcProperties.getDatabaseName(),*/ modeConfiguration, dataSourceMap, Collections.singleton(createShardingRuleConfiguration(shardingJdbcProperties)), props);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 构建具体规则
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param shardingJdbcProperties
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private ShardingRuleConfiguration createShardingRuleConfiguration(ShardingJdbcProperties shardingJdbcProperties) {
|
|
|
|
|
+ ShardingRuleConfiguration result = new ShardingRuleConfiguration();
|
|
|
|
|
+
|
|
|
|
|
+// RuleConfiguration rules = shardingJdbcProperties.getRules();
|
|
|
|
|
+// /*tables*/
|
|
|
|
|
+// Map<String, ShardingTableRuleConfiguration> tables = rules.getTables();
|
|
|
|
|
+// if (!CollectionUtils.isEmpty(tables)) {
|
|
|
|
|
+// for (Map.Entry<String, ShardingTableRuleConfiguration> one : tables.entrySet()) {
|
|
|
|
|
+// ShardingTableRuleConfiguration value = one.getValue();
|
|
|
|
|
+// ShardingTableRuleConfiguration shardingTableRuleConfiguration = new ShardingTableRuleConfiguration(one.getKey(), value.getActualDataNodes());
|
|
|
|
|
+// shardingTableRuleConfiguration.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
|
|
|
|
|
+// shardingTableRuleConfiguration.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true));
|
|
|
|
|
+// result.getTables().add(shardingTableRuleConfiguration);
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ result.getTables().add(getOrderTableRuleConfiguration());
|
|
|
|
|
+// result.getTables().add(getOrderItemTableRuleConfiguration());
|
|
|
|
|
+// result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("a",new ShardingTableRuleConfiguration("",new Object())));
|
|
|
|
|
+// result.getBroadcastTables().add("t_address");
|
|
|
|
|
+// result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("id", "t_student_inline"));
|
|
|
|
|
+// Properties props = new Properties();
|
|
|
|
|
+// props.setProperty("algorithm-expression", "t_student_{id % 2}");
|
|
|
|
|
+// result.getShardingAlgorithms().put("t_student_inline", new AlgorithmConfiguration("INLINE", props));
|
|
|
|
|
+ result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
|
|
|
|
|
+ result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
|
|
|
|
|
+ ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_student", "t_student_${[0, 1]}");
|
|
|
|
|
+ result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "snowflake"));
|
|
|
|
|
+ result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true));
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ShardingTableRuleConfiguration getOrderItemTableRuleConfiguration() {
|
|
|
|
|
+ ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_teacher", "test{0..1}.t_teacher_${[0, 1]}");
|
|
|
|
|
+ result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "snowflake"));
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 数据源
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param shardingJdbcProperties
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<String, DataSource> createDataSources(ShardingJdbcProperties shardingJdbcProperties) {
|
|
|
|
|
+ Map<String, Map<String, String>> dataSources = shardingJdbcProperties.getDataSources();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(dataSources)) {
|
|
|
|
|
+ Map<String, DataSource> dataSourceMap = new HashMap<>();
|
|
|
|
|
+ for (Map.Entry<String, Map<String, String>> stringMapEntry : dataSources.entrySet()) {
|
|
|
|
|
+ Map<String, String> value = stringMapEntry.getValue();
|
|
|
|
|
+ HikariDataSource dataSource = new HikariDataSource();
|
|
|
|
|
+ dataSource.setDriverClassName(StringUtils.isNotBlank(value.get("driverClassName")) ? value.get("driverClassName") : "com.mysql.cj.jdbc.Driver");
|
|
|
|
|
+ dataSource.setJdbcUrl(value.get("jdbcUrl"));
|
|
|
|
|
+ dataSource.setUsername(value.get("username"));
|
|
|
|
|
+ dataSource.setPassword(value.get("password"));
|
|
|
|
|
+ dataSourceMap.put(stringMapEntry.getKey(), dataSource);
|
|
|
|
|
+ }
|
|
|
|
|
+ return dataSourceMap;
|
|
|
|
|
+ }
|
|
|
|
|
+ return new HashMap<>();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|