puzhibing
2023-07-05 35c07cedf67346e9d79449ed185af39a567fa60b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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(gettCoursePackagePaymentTableRuleConfiguration());
        Properties props1 = new Properties();
        props1.setProperty("algorithm-expression", "t_course_package_payment$->{appUserId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_course_package_payment-inline", new AlgorithmConfiguration("INLINE", props1));
        result1.getKeyGenerators().put("t_course_package_payment-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
 
        //分片规则配置
        result1.getTables().add(getCoursePackageStudentTableRuleConfiguration());
        Properties props2 = new Properties();
        props2.setProperty("algorithm-expression", "t_course_package_student$->{appUserId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_course_package_student-inline", new AlgorithmConfiguration("INLINE", props2));
        result1.getKeyGenerators().put("t_course_package_student-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
 
        linkedList.add(result1);
 
        return linkedList;
    }
 
    /**
     * 分片算法配置
     * @return
     */
    private ShardingTableRuleConfiguration gettCoursePackagePaymentTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_course_package_payment", "m$->{0}.t_course_package_payment$->{1..5}");//50
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("appUserId", "t_course_package_payment-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_course_package_payment-snowflake"));
        return result;
    }
 
 
 
    /**
     * 分片算法配置
     * @return
     */
    private ShardingTableRuleConfiguration getCoursePackageStudentTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_course_package_student", "m$->{0}.t_course_package_student$->{1..5}");//30
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("appUserId", "t_course_package_student-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_course_package_student-snowflake"));
        return result;
    }
 
}