Simple Use of Sharing-sphere 3.1.0

Keywords: Java Database JDBC SQL

At the beginning, I spent half a day searching for sharding-sphere on the Internet and reading the source code for half a day (trying to use the @Configuration annotation instead of writing configuration files, I think there are too many configuration files, which are dazzling), and I didn't understand why.

Later, I found a more complete answer directly on the official website. At first, I thought I had to configure the configuration file or something. In fact, it was not necessary at all.

This is the official address: https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-java/

Here are some configurations of our system: because our system is small, we only use sub-tables, not sub-libraries and fragmentation and master-slave, but it is also easy to configure these.

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.oftoo.datasource.config.DynamicDataSource;
import com.oftoo.datasource.database.IPKeyGenerator;
import com.oftoo.datasource.database.TimeShardingTableAlgorithm;

import io.shardingsphere.api.config.rule.ShardingRuleConfiguration;
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
import io.shardingsphere.api.config.strategy.StandardShardingStrategyConfiguration;
import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;

@Configuration
public class DataSourceConfig {

    @Autowired
    DynamicDataSource datasource;

    @Bean(name = "shardingDataSource")
    @Primary
    DataSource getShardingDataSource() throws SQLException {
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(getTestTableRuleConfiguration()); // If there are multiple tables, commas can be used“,"Separation, for example user_info,
        //shardingRuleConfig.getTableRuleConfigs().add(getTerminalRecordTableRuleConfiguration());
        shardingRuleConfig.getBindingTableGroups().add("tb_user,t_terminal_record");
        // Setting up fragmentation strategy and customizing algorithm to implement fragmentation rules //shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new
        // StandardShardingStrategyConfiguration("user_id", new DemoDatabaseShardingAlgorithm()));
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(//
                new StandardShardingStrategyConfiguration("user_id", new TimeShardingTableAlgorithm()));

        return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, //
                new ConcurrentHashMap(), new Properties());
    }

    /**
     * Configuration table rules
     * 
     * @return
     */
    TableRuleConfiguration getTestTableRuleConfiguration() {
        TableRuleConfiguration test = new TableRuleConfiguration(); // Configuration table name
        test.setLogicTable("tb_user"); // Configure the real data node, that is, the real existing node in the database, by the name of the data source + Table name composition
        test.setActualDataNodes("ds_0.tb_user_${0..1}");// user_${0..1}Sub library, user_info_${0..1}Sub table
        // Primary key generation column. The default primary key generation algorithm is snowflake
        test.setKeyGeneratorColumnName("user_id");
        IPKeyGenerator.initWorkerId();
        return test;
    }

    /**
     * create data source
     * 
     * @return
     */
    private Map<String, DataSource> createDataSourceMap() {
        Map<String, DataSource> result = new HashMap<>();
        result.put("ds_0", datasource);
        return result;
    }


}

maven:

        <dependency>
            <groupId>io.shardingsphere</groupId>
            <artifactId>sharding-jdbc-core</artifactId>
            <version>3.1.0</version>
        </dependency>

TimeSharing TableAlgorithm is a class that I copied from another person.

ParaseShardingKeyTool.getYearAndMonth(shardingValue.getValue()) This is just a date obtained such as 201808 (this is the table name)
import java.util.Collection;

import com.oftoo.common.utils.ParaseShardingKeyTool;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

public class TimeShardingTableAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        StringBuffer tableName = new StringBuffer();
        tableName.append(shardingValue.getLogicTableName()).append("_").append(ParaseShardingKeyTool.getYearAndMonth(shardingValue.getValue()));

        return tableName.toString();
    }
}

This is the table name of my database. The fields in it are very simple, just a user_id and name. We use spring +mybatis here.

Posted by movieflick on Thu, 03 Oct 2019 21:47:23 -0700