44323
2023-11-27 aa925d851857f50eff0556411366690d9a78a0e5
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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(getCoursePackagePaymentTableRuleConfiguration());
        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()));
 
        //分片规则配置
        result1.getTables().add(getPostCourseVideoTableRuleConfiguration());
        Properties props3 = new Properties();
        props3.setProperty("algorithm-expression", "t_post_course_video$->{coursePackageId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_post_course_video-inline", new AlgorithmConfiguration("INLINE", props3));
        result1.getKeyGenerators().put("t_post_course_video-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
 
        //分片规则配置
        result1.getTables().add(getUserVideoDetailsTableRuleConfiguration());
        Properties props4 = new Properties();
        props4.setProperty("algorithm-expression", "t_user_video_details$->{appUserId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_user_video_details-inline", new AlgorithmConfiguration("INLINE", props4));
        result1.getKeyGenerators().put("t_user_video_details-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
 
        //分片规则配置
        result1.getTables().add(getCoursePackageSchedulingTableRuleConfiguration());
        Properties props5 = new Properties();
        props5.setProperty("algorithm-expression", "t_course_package_scheduling$->{coursePackageId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_course_package_scheduling-inline", new AlgorithmConfiguration("INLINE", props5));
        result1.getKeyGenerators().put("t_course_package_scheduling-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
 
        //分片规则配置
        result1.getTables().add(getCoursePackageOrderTableRuleConfiguration());
        Properties props6 = new Properties();
        props6.setProperty("algorithm-expression", "t_course_package_order$->{appUserId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_course_package_order-inline", new AlgorithmConfiguration("INLINE", props6));
        result1.getKeyGenerators().put("t_course_package_order-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
 
        linkedList.add(result1);
 
        return linkedList;
    }
 
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getCoursePackagePaymentTableRuleConfiguration() {
        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;
    }
 
 
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getPostCourseVideoTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_post_course_video", "m_$->{0}.t_post_course_video$->{1..5}");//30
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("coursePackageId", "t_post_course_video-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_post_course_video-snowflake"));
        return result;
    }
 
 
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getUserVideoDetailsTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_user_video_details", "m_$->{0}.t_user_video_details$->{1..5}");//30
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("appUserId", "t_user_video_details-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_user_video_details-snowflake"));
        return result;
    }
 
 
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getCoursePackageSchedulingTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_course_package_scheduling", "m_$->{0}.t_course_package_scheduling$->{1..5}");//30
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("coursePackageId", "t_course_package_scheduling-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_course_package_scheduling-snowflake"));
        return result;
    }
 
 
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getCoursePackageOrderTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_course_package_order", "m_$->{0}.t_course_package_order$->{1..5}");//30
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("appUserId", "t_course_package_order-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_course_package_order-snowflake"));
        return result;
    }
 
}