sharding-sphere 3.1.0 的簡單使用

最開始倒騰sharding-sphere的時候,在網上找了半天,下了源碼看了半天(想用@Configuration 註解的方式,而不是去寫配置文件,我以爲配置文件太多了,弄的晃眼),也沒有弄明白個因此然。java

後面直接在官網找到了比較齊全的答案,我最開始還覺得必需要配置配置文件啥的,其實徹底不須要。算法

這個是官網地址:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-java/spring

下面這個是咱們系統的一些配置:因爲咱們系統小,因此只用到了分表,沒有用到分庫和分片和主從,可是配置這些也很容易。sql

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()); // 若是有多個表,能夠用逗號「,」分隔 ,好比user_info, //shardingRuleConfig.getTableRuleConfigs().add(getTerminalRecordTableRuleConfiguration());
        shardingRuleConfig.getBindingTableGroups().add("tb_user,t_terminal_record"); // 設置分片策略,自定義算法來實現分片規則 //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()); } /** * 配置表規則 * * @return
     */ TableRuleConfiguration getTestTableRuleConfiguration() { TableRuleConfiguration test = new TableRuleConfiguration(); // 配置表名
        test.setLogicTable("tb_user"); // 配置真實的數據節點,即數據庫中真實存在的節點,由數據源名 + 表名組成
        test.setActualDataNodes("ds_0.tb_user_${0..1}");// user_${0..1}分庫,user_info_${0..1}分表 // 主鍵生成列,默認的主鍵生成算法是snowflake
        test.setKeyGeneratorColumnName("user_id"); IPKeyGenerator.initWorkerId(); return test; } /** * 建立數據源 * * @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>

TimeShardingTableAlgorithm這個類我複製的另一我的寫的,apache

ParaseShardingKeyTool.getYearAndMonth(shardingValue.getValue())這個只是獲取到了日期如:201808(這個就是表名)
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(); } }

這個是我數據庫的表名,裏面的字段很簡單,就一個user_id 和name,咱們這邊用的是spring +mybatis 。api

相關文章
相關標籤/搜索