无关风月
2024-07-11 eb6b6dbb35a9f029e0b7d269773685c19fd40976
cloud-server-account/src/main/java/com/dsh/config/Sharding_jdbc/ShardingConfig.java
@@ -24,7 +24,7 @@
    @Bean
    public DataSource getDataSource(){
    public DataSource getDataSource() {
        DataSource dataSource = null;
        try {
            Properties properties = new Properties();
@@ -40,6 +40,7 @@
    /**
     * 配置多数据源
     *
     * @return
     */
    private Map<String, DataSource> createDataSourceMap() {
@@ -61,6 +62,7 @@
    /**
     * 分片配置
     *
     * @return
     */
    private Collection<RuleConfiguration> createShardingRuleConfiguration() {
@@ -73,44 +75,41 @@
        props1.setProperty("algorithm-expression", "t_payment_history$->{appUserId % 5 + 1}");
        result1.getShardingAlgorithms().put("t_payment_history-inline", new AlgorithmConfiguration("INLINE", props1));
        result1.getKeyGenerators().put("t_payment_history-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
        linkedList.add(result1);
        //分片规则配置
        ShardingRuleConfiguration result2 = new ShardingRuleConfiguration();
        result2.getTables().add(getRechargeRecordsTableRuleConfiguration());
        result1.getTables().add(getRechargeRecordsTableRuleConfiguration());
        Properties props2 = new Properties();
        props2.setProperty("algorithm-expression", "t_recharge_records$->{appUserId % 5 + 1}");
        result2.getShardingAlgorithms().put("t_recharge_records-inline", new AlgorithmConfiguration("INLINE", props2));
        result2.getKeyGenerators().put("t_recharge_records-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
        linkedList.add(result2);
        result1.getShardingAlgorithms().put("t_recharge_records-inline", new AlgorithmConfiguration("INLINE", props2));
        result1.getKeyGenerators().put("t_recharge_records-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
        //分片规则配置
        ShardingRuleConfiguration result3 = new ShardingRuleConfiguration();
        result3.getTables().add(getUserIntegralChangesTableRuleConfiguration());
        result1.getTables().add(getUserIntegralChangesTableRuleConfiguration());
        Properties props3 = new Properties();
        props3.setProperty("algorithm-expression", "t_user_integral_changes$->{appUserId % 5 + 1}");
        result3.getShardingAlgorithms().put("t_user_integral_changes-inline", new AlgorithmConfiguration("INLINE", props3));
        result3.getKeyGenerators().put("t_user_integral_changes-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
        linkedList.add(result3);
        result1.getShardingAlgorithms().put("t_user_integral_changes-inline", new AlgorithmConfiguration("INLINE", props3));
        result1.getKeyGenerators().put("t_user_integral_changes-snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
        linkedList.add(result1);
        return linkedList;
    }
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getPaymentHistoryTableRuleConfiguration() {
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_payment_history", "m_$->{0}.t_payment_history->{1..5}");//50
        ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_payment_history", "m_$->{0}.t_payment_history$->{1..5}");//50
        result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("appUserId", "t_payment_history-inline"));
        result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "t_payment_history-snowflake"));
        return result;
    }
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getRechargeRecordsTableRuleConfiguration() {
@@ -123,6 +122,7 @@
    /**
     * 分片算法配置
     *
     * @return
     */
    private ShardingTableRuleConfiguration getUserIntegralChangesTableRuleConfiguration() {