最开始倒腾sharding-sphere的时候,在网上找了半天,下了源码看了半天(想用@Configuration 注解的方式,而不是去写配置文件,我觉得配置文件太多了,弄的晃眼),也没有弄明白个所以然。
后面直接在官网找到了比较齐全的答案,我最开始还以为必须要配置配置文件啥的,其实完全不需要。
这个是官网地址:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-java/
下面这个是我们系统的一些配置:由于我们系统小,所以只用到了分表,没有用到分库和分片和主从,但是配置这些也很容易。
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 MapcreateDataSourceMap() { Map result = new HashMap<>(); result.put("ds_0", datasource); return result; } }
maven:
io.shardingsphere sharding-jdbc-core 3.1.0
TimeShardingTableAlgorithm这个类我复制的另外一个人写的,
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{ @Override public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) { StringBuffer tableName = new StringBuffer(); tableName.append(shardingValue.getLogicTableName()).append("_").append(ParaseShardingKeyTool.getYearAndMonth(shardingValue.getValue())); return tableName.toString(); } }
这个是我数据库的表名,里面的字段很简单,就一个user_id 和name,我们这边用的是spring +mybatis 。